Comprehensive Guide to Essential Shell Scripting Commands

Table of contents

No heading

No headings in the article.

This documentation serves as a comprehensive guide to essential shell scripting commands, providing step-by-step explanations and practical examples for each command.

  1. ls: Lists directory contents.

    • Explanation: The ls command is used to list the files and directories within a specified directory.

    • Example:

         ls
      

      This command will list all the files and directories in the current directory.

  2. pwd: Prints the current working directory.

    • Explanation: pwd stands for "print working directory." It displays the full path of the current working directory.

    • Example:

          pwd
      

Running this command will print the current directory's absolute path.

  1. cd: Changes the current directory.

    • Explanation: The cd command allows you to change the current working directory to the specified directory.

    • Example:

          cd /path/to/directory
      

      This command will change the current directory to the one specified.

  2. cd .. : Moves up one directory level in the directory tree.

    • Explanation: cd .. allows you to move one level up in the directory structure.(It will go back from one folder)

    • Example:

          cd ..
      

      Running this command will move you up one directory level.

  3. cd ../..: Moves up two directory levels in the directory tree.

    • Explanation: This command moves you up two levels in the directory structure.(It will go back from 2 folders)

    • Example:

        cd ../..
      

      Running this command will move you up two directory levels.

  4. cd folder1/folder2: Changes to a directory within the current directory.

    • Explanation: You can change to a directory within the current directory by specifying its path.

    • Example:

          cd documents/scripts
      

      This command will change the current directory to "scripts" within the "documents" directory.

  5. ls -ltr: Lists files in long format sorted by modification time.

    • Explanation: The -ltr option displays files in long format, sorted by modification time in reverse order.

    • It provides the information about that file or folder along with timestamp

    • if its starting with ‘d’ then it is directory or else file

    • Example:

          ls -ltr
      

      Running this command will list files in long format sorted by modification time, with the newest files appearing last.

  6. vi: Opens the vi text editor.

    • Explanation: vi is a text editor used to create or edit files.

    • Example:

          vi filename.txt
      

      This command will open the vi text editor and create/edit the file named "filename.txt".

  7. cat: Concatenates and displays file content.

    • Explanation: cat is used to display the contents of one or more files.

    • Example:

          cat filename.txt
      

      This command will display the contents of the file named "filename.txt".

  8. mkdir: Creates a new directory.

    • Explanation: mkdir is used to create a new directory.

    • Example:

          mkdir new_directory
      
  9. rm filename: Removes a file.

    • Explanation: rm is used to remove/delete a file.

    • Example:

          rm file.txt
      

      This command will remove the file named "file.txt".

  10. rm -r foldername: Removes a directory recursively.

    • Explanation: rm -r is used to remove a directory and its contents recursively.

    • Example:

          rm -r directory
      

      This command will remove the directory named "directory" and all its contents.

  11. nproc: Prints the number of processing units available.

    • Explanation: nproc displays the number of processing units (CPU cores) available on the system.

    • Example:

          nproc
      

      This command will print the number of CPU cores available.

  12. free -g: Displays the amount of free and used memory in gigabytes.

    • Explanation: free -g shows the system's memory usage in gigabytes.

    • Example:

          free -g
      

      This command will display memory usage in gigabytes.

  13. df -h: Displays disk space usage in a human-readable format.

    • Explanation: df -h shows disk space usage in a human-readable format.

    • Example:

          df -h
      

      This command will display disk space usage in a human-readable format.

  14. top: Displays real-time system information.

    • Explanation: top provides real-time information about system processes, CPU usage, and memory usage.

    • Example:

        top
      

This command will display real-time system information

  1. chmod: Changes file permissions.

    • Explanation: chmod is used to change the permissions of a file or directory.

    • Example:

        chmod 755 filename.txt
      

This command will change the permissions of the file "filename.txt" to 755

  1. stat -c %a filename: Displays file access rights in octal format.

    • Explanation: stat -c %a displays file access rights in octal format.

    • Example:

        stat -c %a filename.txt
      

This command will display the access rights of the file "filename.txt" in octal format.

  1. man: Displays the manual of a command.

    • Explanation: man displays the manual pages for a specified command.

    • Example:

        man ls
      

This command will display the manual page for the ls command.

  1. grep: Searches for patterns in files.

    • Explanation: grep is a command-line utility for searching plain-text data sets for lines that match a regular expression.

    • Example:

        grep "pattern" filename.txt
      

This command will search for lines containing the specified pattern in the file "filename.txt".

  1. curl: Transfers data to or from a server.

    • Explanation: curl is a command-line tool for transferring data with URL syntax. It supports various protocols including HTTP, HTTPS, FTP, etc.

    • Using the curl command we can get the information from the api’s as well just like in python we are doing with the requests module

    • Example:

        curl https://example.com
      

This command will retrieve the contents of the specified URL.

  1. wget: Downloads files from the internet.

    • Explanation: wget is a command-line utility for downloading files from the web using HTTP, HTTPS, or FTP protocols.

    • Using Wget command it will download that information and save it in the logfile locally

    • Example:

        wget https://example.com/file.txt
      

      This command will download the file "file.txt" from the specified URL.

  2. pipe |: Redirects the output of one command as input to another.

    • Explanation: The pipe (|) operator is used to redirect the output of one command as input to another command.

    • Pipe parameter sends the output of the first command to the second command

    • Pipe command it will receive the information from the stdout not from stdin

    • Example:

        ls | grep "pattern"
      

      This command will list files in the current directory and then filter the output to display only the lines containing the specified pattern.

  3. awk -F "" '{print $2}': Processes text files.

    • Explanation: awk is a programming language used for pattern scanning and processing. -F "" specifies an empty field separator, and {print $2} prints the second field.

    • Example:

        awk -F "," '{print $2}' file.txt
      

      This command will process the text file "file.txt" using comma as a field separator and print the second field of each line.

  4. find / -name filename: Searches for files and directories.

    • Explanation: find is a command-line utility for searching files and directories in a directory hierarchy. / specifies the starting directory, and -name filename specifies the search criteria.

    • Example:

        find / -name "file.txt"
      

      This command will search for the file named "file.txt" starting from the root directory /.

  5. shebang: Specifies the interpreter for a script.

    • Explanation: The shebang (#!) line at the beginning of a script specifies the interpreter to be used to execute the script.

    • Example:

        #!/bin/bash
      

      This shebang line specifies that the script should be executed using the Bash shell.

  6. set -x: Enables tracing of commands and their arguments.

    • Explanation: set -x enables debugging mode, where each executed command and its arguments are printed to the terminal.

    • set -x : It will print all the commands on the script in a debug mode

    • Example:

        set -x
      

      This command enables tracing of commands and their arguments.

  7. set -e: Exits immediately if a command exits with a non-zero status.

    • Explanation: set -e causes the script to exit immediately if any command returns a non-zero exit status.

    • set -e : It will exit the script when there is an error in the script

    • Example:

        set -e
      

      This command enables immediate exit on error.

  8. set -o: Displays or sets shell options.

    • Explanation: set -o displays or sets various shell options.

    • We need to use set -o because set -e won’t give any error if the last command in the line is executed successfully. It will fail only if the last command on the line fails

    • Example:

        set -o
      

      This command sets the nounset option, causing an error to occur if