The Linux command line is an intricate web of powerful tools, and mastering even a fraction of its commands can dramatically boost your productivity.

Let’s dive into some essential commands, not just what they do, but how they fit together and why they’re so indispensable.

  • pwd: Print Working Directory. This tells you exactly where you are in the file system hierarchy.
    • Example:
      $ pwd
      /home/user/documents
      
  • ls: List Directory Contents. Shows you what files and directories are in your current location.
    • Example:
      $ ls -lha
      total 20K
      drwxr-xr-x 2 user user 4.0K Jan 20 10:00 .
      drwxr-xr-x 3 user user 4.0K Jan 20 09:55 ..
      -rw-r--r-- 1 user user  512 Jan 20 10:00 my_document.txt
      -rw-r--r-- 1 user user 1.2K Jan 19 15:30 script.sh
      
      • -l: Use a long listing format.
      • -h: Print sizes in human-readable format (e.g., 1K, 2M).
      • -a: Do not ignore entries starting with . (hidden files).
  • cd: Change Directory. This is how you move around.
    • Example:
      $ cd documents  # Move into the 'documents' directory
      $ cd ..         # Move up one directory
      $ cd ~          # Move to your home directory
      $ cd /          # Move to the root directory
      
  • mkdir: Make Directory. Creates a new directory.
    • Example:
      $ mkdir new_project
      $ mkdir -p parent/child/grandchild # Creates parent, child, and grandchild directories if they don't exist
      
  • rmdir: Remove Directory. Deletes an empty directory.
    • Example:
      $ rmdir old_stuff
      
    • Note: For non-empty directories, use rm -r.

Working with Files

  • touch: Create Empty Files or Update Timestamps. If the file doesn’t exist, it’s created. If it does, its access and modification times are updated.
    • Example:
      $ touch new_file.txt
      $ touch existing_file.log # Updates timestamp
      
  • cp: Copy Files and Directories.
    • Example:
      $ cp source.txt destination.txt     # Copy file
      $ cp -r source_dir destination_dir # Copy directory recursively
      
  • mv: Move or Rename Files and Directories.
    • Example:
      $ mv old_name.txt new_name.txt      # Rename file
      $ mv my_file.txt /path/to/new/location/ # Move file
      
  • rm: Remove Files or Directories. Be careful with this one!
    • Example:
      $ rm unwanted_file.txt
      $ rm -r old_project # Remove directory and its contents recursively
      $ rm -rf / # DANGER: Do not run this command! It will delete everything.
      
      • -r: Recursive, for directories.
      • -f: Force, ignore nonexistent files and never prompt.
  • cat: Concatenate and Display Files. Primarily used to display file content.
    • Example:
      $ cat my_document.txt
      $ cat file1.txt file2.txt > combined.txt # Concatenate two files into a new one
      
  • less: View File Content Page by Page. More advanced than cat for large files.
    • Example:
      $ less /var/log/syslog
      
      • Navigate with arrow keys, Page Up/Page Down. Press q to quit.
  • head: Display the Beginning of a File.
    • Example:
      $ head -n 10 my_log.txt # Show the first 10 lines
      
  • tail: Display the End of a File.
    • Example:
      $ tail -n 50 error.log # Show the last 50 lines
      $ tail -f access.log   # Follow the file as it grows (real-time log monitoring)
      

Searching and Filtering

  • grep: Search for Patterns in Text. Incredibly powerful for finding specific lines in files or command output.
    • Example:
      $ grep "error" /var/log/messages
      $ ps aux | grep "nginx" # Find processes related to nginx
      
      • ps aux: Lists all running processes.
      • |: The pipe symbol, which sends the output of the command on its left as input to the command on its right.
  • find: Search for Files in a Directory Hierarchy.
    • Example:
      $ find . -name "*.log" # Find all files ending with .log in the current directory and subdirectories
      $ find /home/user -type f -mtime -7 # Find files modified in the last 7 days in /home/user
      
      • -type f: Only find files.
      • -mtime -7: Modified less than 7 days ago.

System Information and Monitoring

  • ps: Report a Snapshot of the Current Processes.
    • Example:
      $ ps aux # Shows all processes for all users in a user-friendly format
      $ ps -ef # Shows all processes in a standard format
      
  • top: Display Linux Processes. An interactive, real-time view of running processes, CPU usage, memory, etc.
    • Example:
      $ top
      
      • Press k to kill a process, enter PID, then signal (e.g., 9 for SIGKILL). Press q to quit.
  • htop: An interactive process viewer. A more user-friendly and feature-rich alternative to top.
    • Example:
      $ htop
      
      • Install it if you don’t have it (sudo apt install htop or sudo yum install htop).
  • df: Report File System Disk Space Usage.
    • Example:
      $ df -h # Show disk space in human-readable format
      
  • du: Estimate File Space Usage.
    • Example:
      $ du -sh /var/log # Show total size of /var/log in human-readable format
      $ du -h --max-depth=1 /home/user # Show sizes of top-level directories in /home/user
      
  • free: Display Amount of Free and Used Memory in the System.
    • Example:
      $ free -h # Show memory in human-readable format
      

Permissions and Ownership

  • chmod: Change File Mode Bits (Permissions).
    • Example:
      $ chmod 755 my_script.sh # Owner: rwx, Group: r-x, Others: r-x
      $ chmod +x my_script.sh  # Add execute permission for everyone
      $ chmod u+w file.txt     # Add write permission for the owner
      
      • Numeric 755 means: owner (4+2+1=7), group (4+0+1=5), others (4+0+1=5).
      • u (user/owner), g (group), o (others), a (all).
      • + (add), - (remove), = (set exactly).
  • chown: Change File Owner and Group.
    • Example:
      $ chown user:group my_file.txt # Change owner to 'user' and group to 'group'
      $ chown user my_file.txt      # Change owner to 'user', group remains unchanged
      $ chown -R user:group my_directory # Recursively change owner and group
      

Networking

  • ping: Send ICMP ECHO_REQUEST to Network Hosts. Checks network connectivity.
    • Example:
      $ ping google.com
      
      • Press Ctrl+C to stop.
  • ssh: OpenSSH SSH Client (Remote Login Program).
    • Example:
      $ ssh user@remote_host_ip
      
  • scp: Secure Copy. Copies files between hosts over SSH.
    • Example:
      $ scp local_file.txt user@remote_host:/path/on/remote/
      $ scp user@remote_host:/path/on/remote/remote_file.txt . # Copy to current directory
      

Archiving and Compression

  • tar: Tape Archiver. Used to create and extract compressed archives (often .tar.gz or .tar.bz2).
    • Example (Create a compressed archive):
      $ tar -czvf archive.tar.gz /path/to/directory
      
      • -c: Create an archive.
      • -z: Compress with gzip.
      • -v: Verbose (show files being processed).
      • -f: Use archive file.
    • Example (Extract an archive):
      $ tar -xzvf archive.tar.gz # Extract a .tar.gz file
      $ tar -xjvf archive.tar.bz2 # Extract a .tar.bz2 file
      
      • -x: Extract files from an archive.
      • -j: Compress with bzip2.

Text Manipulation

  • sed: Stream Editor. Powerful for performing text transformations on an input stream (a file or pipeline).
    • Example (Replace all occurrences of 'old' with 'new'):
      $ sed 's/old/new/g' my_file.txt
      $ sed -i 's/old/new/g' my_file.txt # Edit file in-place
      
      • s: substitute command.
      • g: global, replace all occurrences on a line.
  • awk: Pattern Scanning and Processing Language. Excellent for structured text data.
    • Example (Print the first and third fields of each line):
      $ awk '{print $1, $3}' my_data.csv
      
      • $1 refers to the first field, $2 to the second, etc. Fields are space-delimited by default.

Other Useful Commands

  • man: Manual Pages. Get help on any command.
    • Example:
      $ man ls
      
      • Press q to quit.
  • echo: Display a Line of Text. Often used in scripts or to display variables.
    • Example:
      $ echo "Hello, World!"
      $ echo $PATH # Display the value of the PATH environment variable
      
  • alias: Create Command Aliases. Shorthands for longer commands.
    • Example:
      $ alias ll='ls -lha'
      $ ll # Now runs 'ls -lha'
      
  • history: Display Command History.
    • Example:
      $ history 10 # Show the last 10 commands
      $ !500       # Re-run command number 500 from history
      

This list is just the beginning, but these commands form the bedrock of effective command-line usage in Linux. The true power comes from combining them using pipes (|), redirection (>, >>, <), and understanding their options.

Want structured learning?

Take the full Linux & Systems Programming course →