Skip to content

Basic Shell Scripting

Category: Linux Command Basics
Type: Linux Commands
Generated on: 2025-07-10 03:08:09
For: System Administration, Development & Technical Interviews


Basic Shell Scripting Cheat Sheet (Linux Commands)

Section titled “Basic Shell Scripting Cheat Sheet (Linux Commands)”

This cheat sheet covers essential Linux commands for shell scripting, useful for both system administrators and developers.

1. Command Overview

  • Purpose: Provides a quick reference for frequently used Linux commands, covering their purpose, syntax, examples, and troubleshooting tips.
  • Use Cases: System administration tasks, automating repetitive tasks, scripting deployments, monitoring system health, and more.

2. Basic Syntax

Most commands follow this general syntax:

Terminal window
command [options] [arguments]
  • command: The name of the command to execute (e.g., ls, mkdir, rm).
  • options: Modify the behavior of the command (e.g., -l, -r, -v). Options usually start with a hyphen (-).
  • arguments: Data or targets for the command to operate on (e.g., filenames, directories).

3. Practical Examples

  • ls (List Directory Contents)

    • Example: ls -l /home/user/documents (Lists files in /home/user/documents with detailed information).
  • mkdir (Make Directory)

    • Example: mkdir my_new_directory (Creates a directory named my_new_directory in the current working directory).
  • rm (Remove Files or Directories)

    • Example: rm myfile.txt (Deletes the file myfile.txt). WARNING: This is permanent!
    • Example: rm -r my_directory (Recursively deletes the directory my_directory and all its contents). EXTREMELY DANGEROUS - DOUBLE CHECK BEFORE RUNNING
  • cp (Copy Files or Directories)

    • Example: cp myfile.txt /tmp/ (Copies myfile.txt to the /tmp/ directory).
    • Example: cp -r my_directory /backup/ (Recursively copies the directory my_directory to the /backup/ directory).
  • mv (Move or Rename Files or Directories)

    • Example: mv myfile.txt newfile.txt (Renames myfile.txt to newfile.txt).
    • Example: mv myfile.txt /opt/ (Moves myfile.txt to the /opt/ directory).
  • cat (Concatenate and Display Files)

    • Example: cat myfile.txt (Displays the contents of myfile.txt on the terminal).
    • Example: cat file1.txt file2.txt > combined.txt (Concatenates file1.txt and file2.txt and saves the output to combined.txt).
  • echo (Display a Line of Text)

    • Example: echo "Hello, world!" (Prints “Hello, world!” to the terminal).
    • Example: echo $USER (Prints the current username).
  • grep (Search for Patterns in Files)

    • Example: grep "error" logfile.txt (Searches for the word “error” in logfile.txt).
    • Example: grep -i "error" logfile.txt (Case-insensitive search).
  • find (Find Files and Directories)

    • Example: find . -name "*.txt" (Finds all files with the .txt extension in the current directory and its subdirectories).
    • Example: find / -type d -name "config" (Finds all directories named “config” starting from the root directory).
  • chmod (Change File Permissions)

    • Example: chmod 755 myscript.sh (Makes myscript.sh executable by the owner and readable/executable by others). (755 is a common permission set for scripts).
  • chown (Change File Ownership)

    • Example: chown user:group myfile.txt (Changes the owner of myfile.txt to user and the group to group).
  • sudo (Execute a Command as Superuser)

    • Example: sudo apt update (Updates the package list as root). Use with caution!
  • ps (Process Status)

    • Example: ps aux (Lists all running processes with detailed information).
  • kill (Terminate a Process)

    • Example: kill 1234 (Sends a SIGTERM signal to process with PID 1234).
    • Example: kill -9 1234 (Sends a SIGKILL signal - use only as a last resort, as it doesn’t allow the process to clean up).
  • df (Disk Free Space)

    • Example: df -h (Displays disk space usage in a human-readable format).
  • du (Disk Usage)

    • Example: du -sh /var/log (Displays the total size of the /var/log directory in a human-readable format).
  • top (Display Top CPU Processes)

    • Example: top (Interactive display of resource usage).
  • head (Display the Beginning of a File)

    • Example: head -n 10 myfile.txt (Displays the first 10 lines of myfile.txt).
  • tail (Display the End of a File)

    • Example: tail -f logfile.txt (Displays the last lines of logfile.txt and follows the file, showing new lines as they are added - useful for monitoring logs in real-time).
  • wc (Word Count)

    • Example: wc -l myfile.txt (Counts the number of lines in myfile.txt).
  • wget (Download Files from the Web)

    • Example: wget https://example.com/myfile.txt (Downloads myfile.txt from the specified URL).
  • curl (Transfer data with URLs)

    • Example: curl https://example.com (Fetches the content of the website).
    • Example: curl -X POST -d "name=John&age=30" https://example.com/api/users (Performs a POST request to the API endpoint with the provided data).
  • tar (Tape Archive - Archiving and Compression)

    • Example: tar -czvf myarchive.tar.gz my_directory (Creates a compressed archive of my_directory).
    • Example: tar -xzvf myarchive.tar.gz (Extracts the contents of myarchive.tar.gz).
  • gzip (Compress Files)

    • Example: gzip myfile.txt (Compresses myfile.txt into myfile.txt.gz).
    • Example: gzip -d myfile.txt.gz (Decompresses myfile.txt.gz).
  • gunzip (Decompress Files)

    • Example: gunzip myfile.txt.gz (Decompresses myfile.txt.gz into myfile.txt).
  • zip (Compress files into a zip archive)

    • Example: zip myarchive.zip myfile.txt myotherfile.txt (Creates a zip archive named myarchive.zip containing the specified files).
  • unzip (Extract files from a zip archive)

    • Example: unzip myarchive.zip (Extracts all files from myarchive.zip).
  • ssh (Secure Shell - Remote Login)

    • Example: ssh user@remote_host (Connects to remote_host as user).
    • Example: ssh -i /path/to/private_key user@remote_host (Connects using a specific private key).
  • scp (Secure Copy - Remote File Transfer)

    • Example: scp myfile.txt user@remote_host:/tmp/ (Copies myfile.txt to the /tmp/ directory on remote_host).
    • Example: scp user@remote_host:/tmp/myfile.txt . (Copies myfile.txt from the /tmp/ directory on remote_host to the current directory).

4. Common Options

CommandOptionDescription
ls-lLong listing format (detailed information).
ls-aShow all files, including hidden files (starting with .).
ls-hHuman-readable file sizes (e.g., 1K, 234M, 2G).
ls-tSort by modification time (newest first).
rm-rRecursive (remove directories and their contents). EXTREMELY DANGEROUS!
rm-fForce (ignore nonexistent files, never prompt). EXTREMELY DANGEROUS!
cp-rRecursive (copy directories and their contents).
cp-vVerbose (show files being copied).
mv-iInteractive (prompt before overwriting existing files).
grep-iCase-insensitive search.
grep-vInvert match (show lines that do not match the pattern).
grep-rRecursive search (search in all files under each directory, recursively).
grep-nShow line numbers.
find-nameFind files by name.
find-typeFind files by type (e.g., f for file, d for directory).
find-sizeFind files by size (e.g., +10M for files larger than 10MB, -10k for files smaller than 10KB).
df-hHuman-readable output.
du-hHuman-readable output.
du-sSummarize disk usage (show only the total).
tail-fFollow the file (display new lines as they are added).
head-nSpecify the number of lines to display.
tail-nSpecify the number of lines to display.
wc-lCount lines.
wc-wCount words.
wc-cCount bytes.
tar-cCreate archive.
tar-xExtract archive.
tar-vVerbose (list files being processed).
tar-zCompress archive with gzip.
tar-fSpecify archive filename.
gzip-dDecompress.
ssh-iSpecify private key file.
ssh-pSpecify port number.

5. Advanced Usage

  • Piping (|): Connect the output of one command to the input of another.

    Terminal window
    cat logfile.txt | grep "error" | wc -l

    (Counts the number of lines in logfile.txt that contain the word “error”).

  • Redirection (>, >>, <): Redirect input and output.

    Terminal window
    ls -l > filelist.txt # Redirect standard output to a file (overwrites).
    ls -l >> filelist.txt # Redirect standard output to a file (appends).
    cat < input.txt # Redirect standard input from a file.
  • Command Substitution ($(), `): Execute a command and use its output as part of another command.

    Terminal window
    echo "Today is $(date)"
    USER_HOME=`echo ~` # Be careful using backticks, prefer $()
  • Loops (for, while): Automate repetitive tasks.

    Terminal window
    # For loop
    for i in *.txt; do
    echo "Processing file: $i"
    # Do something with the file here
    done
    # While loop
    while read line; do
    echo "Line: $line"
    done < input.txt
  • Conditional Statements (if, then, else): Execute different commands based on conditions.

    Terminal window
    if [ -f myfile.txt ]; then
    echo "File exists."
    else
    echo "File does not exist."
    fi
  • Variables: Store and reuse values.

    Terminal window
    MY_VARIABLE="Hello"
    echo $MY_VARIABLE
  • Functions: Define reusable blocks of code.

    Terminal window
    my_function() {
    echo "This is a function."
    }
    my_function # Call the function
  • Combining find and xargs: Process files found by find in batches.

    Terminal window
    find . -name "*.log" -print0 | xargs -0 grep "error"

    (Finds all .log files and searches for “error” in each of them. -print0 and xargs -0 handle filenames with spaces correctly.)

  • Using sed (Stream EDitor): Powerful text manipulation.

    Terminal window
    sed 's/old_text/new_text/g' myfile.txt # Replace all occurrences of "old_text" with "new_text".
    sed '/pattern/d' myfile.txt # Delete lines containing "pattern".
  • Using awk (Pattern Scanning and Processing Language): Advanced text processing.

    Terminal window
    awk '{print $1}' myfile.txt # Print the first column of each line.
    awk -F',' '{print $2}' data.csv # Print the second column of a CSV file.

6. Tips & Tricks

  • Tab Completion: Use the Tab key to autocomplete commands, filenames, and directory names.

  • History: Use the Up and Down arrow keys to navigate through your command history. history command to view history. !n to execute the nth command from history.

  • Aliases: Create shortcuts for frequently used commands. Add to ~/.bashrc.

    Terminal window
    alias la='ls -la'
  • Wildcards:

    • *: Matches zero or more characters. ls *.txt
    • ?: Matches exactly one character. ls file?.txt
    • []: Matches a range of characters. ls file[1-5].txt
  • man pages: Use the man command to access the manual page for any command (e.g., man ls).

  • apropos: Search for commands by keyword (e.g., apropos disk).

  • Ctrl+C: Interrupt the currently running command.

  • Ctrl+Z: Suspend the currently running command. Use fg to bring it back to the foreground, or bg to run it in the background.

  • !!: Execute the last command again.

  • $_: Refers to the last argument of the previous command. mkdir mydir; cd $_

  • set -x: Debug a shell script by printing each command before it is executed.

  • set -e: Exit immediately if a command exits with a non-zero status.

7. Troubleshooting

  • “Command not found”: The command is not installed or is not in your PATH. Check the spelling and ensure the command is installed. Use which commandname to find the location of the executable.
  • “Permission denied”: You do not have the necessary permissions to execute the command or access the file. Use chmod to change file permissions or sudo to run the command as superuser (with caution!).
  • “No such file or directory”: The file or directory does not exist or the path is incorrect. Double-check the spelling and path.
  • Script doesn’t execute: Ensure the script has execute permissions (chmod +x script.sh) and the shebang is correct (#!/bin/bash at the top of the script).
  • Unexpected output: Carefully review the command syntax, options, and arguments. Use echo statements to debug your script.
  • Infinite loop: Interrupt the script with Ctrl+C. Review the loop condition to ensure it will eventually terminate.

8. Related Commands

  • Package Management: apt, yum, dnf, pacman (depending on your distribution).
  • System Information: uname, hostname, uptime.
  • Networking: ifconfig, ip, ping, netstat, ss, traceroute, dig, nslookup.
  • Process Management: top, htop, nice, renice.
  • Text Editors: vi, vim, nano.
  • Version Control: git.
  • Job Control: nohup, screen, tmux.
  • systemctl: Manage systemd services.
  • journalctl: View systemd logs.

This cheat sheet provides a solid foundation for working with the Linux command line and writing shell scripts. Remember to consult the man pages for detailed information on each command. Practice is key to mastering these tools!