DEV Community

Mritunjay Singh
Mritunjay Singh

Posted on

Deep dive into Linux

What is Linux

Linux is an open-source operating system modeled on UNIX. It was created by Linus Torvalds in 1991 and has grown to become one of the most popular operating systems worldwide, especially for servers and development environments.

Windows vs Linux

Linux is known for its stability, security, and open-source nature, while Windows is more user-friendly with a large variety of software and hardware support. Linux is preferred in server environments and by developers, while Windows is common on desktops.

What is Kernel

The kernel is the core part of the operating system that manages hardware resources and facilitates communication between software and hardware.

Details of Basic to Advance commands

What is Shebang (#!)?

Shebang is a special character in Linux used to specify which shell to use to execute the script. It is placed in the very first line of the script, followed by the absolute path of the interpreter that will be used to execute the script.

Example:

#!/bin/bash
Enter fullscreen mode Exit fullscreen mode

Types of Shells

  1. Bash
  2. Sh
  3. Ksh

How to check your current shell?

To check the current shell, run the following command:

echo $SHELL
Enter fullscreen mode Exit fullscreen mode

To Change Shell

To change the shell:

chsh -s /bin/bash
Enter fullscreen mode Exit fullscreen mode

What Does ./ Do?

The ./ tells Linux to look in the current directory. Without it, Linux only looks for commands in predefined system paths ($PATH).

Why is ./ Needed to Run Scripts?

By default, Linux does not execute files from the current directory unless you explicitly tell it to.

Example: Trying to run a script without ./:

script.sh
Enter fullscreen mode Exit fullscreen mode

❌ Error: Command not found

bash: script.sh: command not found
Enter fullscreen mode Exit fullscreen mode

What If I Don’t Use ./?

  1. Option 1: Use Full Path
/home/user/myscript.sh
Enter fullscreen mode Exit fullscreen mode
  1. Option 2: Add the Script to $PATH If you want to run myscript.sh without ./, move it to /usr/local/bin:
sudo mv myscript.sh /usr/local/bin/myscript
chmod +x /usr/local/bin/myscript
Enter fullscreen mode Exit fullscreen mode

Now, you can run:

myscript
Enter fullscreen mode Exit fullscreen mode

from anywhere without ./.

What Does "Adding a Script to $PATH" Mean?

The $PATH variable is a list of directories where Linux looks for executable commands.

Why Add a Script to $PATH?

If you place your script in one of these directories, you can run it from anywhere without using ./.

Example:

  1. Move your script to /usr/local/bin/:
sudo mv myscript.sh /usr/local/bin/myscript
Enter fullscreen mode Exit fullscreen mode
  1. Make it executable:
chmod +x /usr/local/bin/myscript
Enter fullscreen mode Exit fullscreen mode
  1. Now you can run it from anywhere:
myscript
Enter fullscreen mode Exit fullscreen mode

How Linux Finds and Executes Commands

Example 1: Running ls Command

When you type ls, Linux needs to find the ls executable file and run it.

  1. Check if ls is a built-in command (e.g., cd is a built-in shell command).
  2. Check $PATH directories for ls.

To check where ls is located:

which ls
Enter fullscreen mode Exit fullscreen mode

Output:

/usr/bin/ls
Enter fullscreen mode Exit fullscreen mode

$PATH and Why It Matters

The $PATH variable contains a list of directories separated by : where Linux searches for commands.

Example output:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Enter fullscreen mode Exit fullscreen mode

Finding Where a Command is Located

Use the following commands:

  • which → Shows the full path of an executable
which vim
Enter fullscreen mode Exit fullscreen mode

Output:

/usr/bin/vim
Enter fullscreen mode Exit fullscreen mode
  • type → Tells if a command is built-in or external
type cd
Enter fullscreen mode Exit fullscreen mode

Output:

cd is a shell builtin
Enter fullscreen mode Exit fullscreen mode
  • command -v → Also finds the location of a command
command -v node
Enter fullscreen mode Exit fullscreen mode

Output:

/home/mritunjay/.nvm/versions/node/v20.18.2/bin/node
Enter fullscreen mode Exit fullscreen mode

How to Check If a Script is in $PATH

Use which to check:

which myscript
Enter fullscreen mode Exit fullscreen mode

If no output is returned, the script is not in a $PATH directory.

History Command in Linux – Deep Dive with Practical Examples

Basic Usage of history

Simply type:

history
Enter fullscreen mode Exit fullscreen mode

Output:

1  ls
2  cd /var/log
3  cat syslog
4  nano file.txt
5  history
Enter fullscreen mode Exit fullscreen mode

Search Command History

Press Ctrl + R to search interactively:

  • Press Ctrl + R
  • Start typing a command (e.g., nano)
  • It will show the most recent match.
  • Press Enter to execute it or Esc to edit.

How to Write a Simple Shell Script?

  1. Create a file:
vim sample-script.sh
Enter fullscreen mode Exit fullscreen mode
  1. Add the following script:
#!/bin/bash
# Create a folder
mkdir learning-shell-script
cd learning-shell-script
# Create two files
touch f1 f2
Enter fullscreen mode Exit fullscreen mode
  1. Save and exit:
:wq!
Enter fullscreen mode Exit fullscreen mode
  1. Make the script executable:
chmod 777 sample-script.sh
Enter fullscreen mode Exit fullscreen mode
  1. Run the script:
./sample-script.sh
Enter fullscreen mode Exit fullscreen mode

You will see the folder learning-shell-script containing f1 and f2.

!q vs !wq

  • !q: Quits without saving.
  • !wq: Saves and quits.

Other System-Related Commands

  • nproc: Shows the number of processing units available.
  • free: Displays memory usage.
  • top: Displays system tasks and resource usage.

Role of Shell Scripting in DevOps

Shell scripting plays a crucial role in DevOps automation, allowing developers to write scripts for tasks such as deployment, monitoring, backups, and configuration management.

Top comments (0)