Skip to content

04. Essential Commands

A DevOps engineer spends the majority of their time navigating the filesystem, viewing logs, and processing text. Mastering these essential commands is the key to working efficiently in the terminal.

  • pwd (Print Working Directory): Shows exactly where you currently are in the filesystem.
  • cd (Change Directory): Moves you to a different folder.
    • cd ~ (Go to home directory)
    • cd - (Go back to the previous directory)
  • ls (List): Lists directory contents.
    • ls -la (List all files, including hidden ones, in a long-format list)
  • tree: Displays directories as a tree (may require installation via apt install tree).
  • touch: Creates an empty file or updates the timestamp of an existing file.
    • touch config.yaml
  • mkdir (Make Directory): Creates a new folder.
    • mkdir -p /app/data/logs (The -p flag creates parent directories if they don’t exist).
  • rm (Remove): Deletes files or directories. Warning: There is no recycle bin in Linux!
    • rm file.txt (Delete a file)
    • rm -rf /folder (Recursively and forcefully delete a folder and all its contents)
  • cp (Copy): Copies files or directories.
    • cp source.txt dest.txt
    • cp -r /source_dir /dest_dir (Recursive copy for folders)
  • mv (Move): Moves or renames files and directories.
    • mv old_name.txt new_name.txt (Rename)
    • mv file.txt /var/www/ (Move)

When troubleshooting, you often need to read configuration files and logs without opening a full text editor.

  • cat (Concatenate): Prints the entire contents of a file to the terminal. Best for short files.
  • less & more: Pagers that let you scroll through large files one screen at a time. (less is modern and allows backward scrolling).
    • Press q to quit, / to search forward.
  • head: Prints the first 10 lines of a file.
    • head -n 20 file.txt (Prints first 20 lines)
  • tail: Prints the last 10 lines of a file.
    • tail -f /var/log/syslog (The -f flag “follows” the file, printing new lines in real-time as they are written. Essential for watching logs!)
  • find: The most powerful tool for searching the filesystem based on various criteria.
    • find /var/log -name "*.log" (Find all files ending in .log in /var/log)
    • find / -type f -size +1G (Find all files larger than 1GB)
  • locate: A faster alternative to find that searches a pre-built database (requires running updatedb).
  • which: Shows the absolute path of the executable that would run if you typed a command.
    • which docker (Outputs /usr/bin/docker)
  • whereis: Similar to which, but also locates the source and manual pages for a command.

Linux treats everything as text. These tools allow you to slice, filter, and modify text streams.

  • grep: Searches text for matching patterns.
    • grep "ERROR" /var/log/syslog
    • grep -i "error" file.txt (Case-insensitive)
  • sort: Sorts lines of text alphabetically or numerically.
  • uniq: Removes duplicate consecutive lines. Often paired with sort.
  • wc (Word Count): Counts lines, words, and characters.
    • wc -l file.txt (Counts the number of lines)
  • cut: Extracts specific columns or fields from a file.
    • cut -d':' -f1 /etc/passwd (Extracts the first column, delimited by a colon)
  • awk & sed: Advanced text processing languages.
    • awk '{print $2}' file.txt (Prints the second column of every line)
    • sed 's/old/new/g' file.txt (Finds “old” and replaces it with “new”)
  • tr (Translate): Translates or deletes characters.
    • tr 'a-z' 'A-Z' (Converts input to uppercase).

The true power of Linux comes from chaining small, single-purpose tools together using Pipes and Redirection.

By default, commands output to the terminal (Standard Output / stdout).

  • > (Overwrite): Redirects output to a file, overwriting the file if it exists.
    • echo "Hello" > file.txt
  • >> (Append): Redirects output to a file, appending to the end.
    • echo "World" >> file.txt
  • < (Input): Passes the contents of a file as input to a command.
  • 2> (Standard Error): Redirects errors (stderr) to a file instead of the screen.
    • find / -name "secret" 2> errors.log
  • 2>&1: Redirects stderr to the same place as stdout. (Combines regular output and errors).

Takes the output of the command on the left, and uses it as the input for the command on the right.

Terminal window
# List all running processes, but filter only the ones containing "nginx"
ps aux | grep nginx
# Read a massive log file, find errors, and count how many there are
cat /var/log/syslog | grep "ERROR" | wc -l

You can execute multiple commands in a single line based on logical conditions.

  • ; (Sequential): Runs the second command regardless of whether the first succeeded.
    • mkdir new_folder ; cd new_folder
  • && (Logical AND): Runs the second command ONLY if the first command succeeds (exit code 0).
    • apt update && apt upgrade -y
  • || (Logical OR): Runs the second command ONLY if the first command fails.
    • cat config.txt || echo "File not found!"