Skip to main content

Linux Terminal

Linux & Shell Cheatsheet

A quick, printable reference with essential POSIX/Bash terminal commands: navigation, file management, directories, I/O redirection, pipelines, and grep. Print to PDF with Ctrl/Cmd + P.

PDF / Print

Save or print this cheatsheet as PDF

Print-ready layout: open in one click and save as PDF from your browser, no account or email required.

0. Core Shell Invariants & Pitfalls to Avoid

Five terminal habits that prevent data loss and word-splitting bugs.

  1. Always double-quote file names and variables ("$file") so spaces and glob characters do not break arguments.
  2. rm and rm -rf bypass the trash completely: always inspect targets with ls or echo before wildcards like rm -rf *.
  3. Output redirection > truncates the destination file immediately before execution; use >> to append safely.
  4. Never pipe ls into rm: use shell globs or find -print0 | xargs -0 when handling arbitrary filenames.
  5. Follow least-privilege permissions with chmod and sudo — never treat chmod 777 as a fix.

2. Moving in the File System (cd)

  • Enter a folder

    cd documents
    cd /var/log

    Accepts paths relative to cwd or absolute paths starting at /.

    Pitfall: Wrap paths containing spaces in quotes: cd "my documents", never unquoted.

  • Go back (cd ..)

    cd ..

    Moves up one level into the parent directory.

    Pitfall: Inside automation scripts always guard cd dir || exit 1 before deleting or writing files.

  • Home folder (cd ~)

    cd ~

    Jumps directly to the active user home directory (plain cd does the same).

    Pitfall: Tilde ~ does not expand inside double quotes ("~/file" stays literal): use "$HOME/file" in scripts.

  • Previous folder (cd -)

    cd -

    Toggles back to the previous working directory ($OLDPWD).

    Pitfall: Only switches once you have already changed directory at least once in the session.

3. Resource Creation

  • Create an empty file (touch)

    touch app.log
    touch index.html

    Creates missing files or updates modification timestamps on existing ones.

    Pitfall: touch does not clear existing file contents; truncate safely with : > file.txt.

  • Create a directory (mkdir)

    mkdir projects

    Creates a single new directory when its parent already exists.

    Pitfall: Without -p, mkdir errors if the directory already exists or if parent folders are missing.

  • Create nested paths (mkdir -p)

    mkdir -p projects/frontend/src

    Recursively creates missing parent directories in one idempotent call.

    Pitfall: Succeeds silently if directories already exist — ideal for setup scripts.

4. Copying & Moving

  • Copy a file (cp)

    cp info.txt backup.txt

    Duplicates file contents to a new target path or filename.

    Pitfall: cp silently overwrites an existing target file unless you pass -i (interactive) or -n (no-clobber).

  • Copy a directory (cp -r)

    cp -r documents backup_documents

    The -r flag enables recursive directory traversal and copying.

    Pitfall: If backup_documents already exists, cp -r puts documents inside backup_documents rather than merging directly.

  • Move or rename (mv)

    mv info.txt docs/info.txt
    mv old.txt new.txt

    Moves into a directory target or renames when given a file path.

    Pitfall: Just like cp, mv silently replaces an existing target file unless guarded with -i or -n.

5. Deleting Resources

  • Delete files (rm)

    rm file.txt

    Unlinks the file immediately without sending it to a recoverable trash folder.

    Pitfall: To delete a filename starting with a hyphen, pass end-of-options: rm -- -weird.txt.

  • Delete directories (rm -r)

    rm -r projects_folder

    Recursively deletes a folder and everything inside it (use rmdir for empty folders).

    Pitfall: Double-check every relative/wildcard path before executing.

  • Force delete (rm -f)

    rm -f protected_file.txt
    rm -rf folder_to_delete

    Ignores missing targets and skips interactive confirmation prompts.

    Pitfall: An unquoted empty variable before / in rm -rf "$DIR"/ expands catastrophically to rm -rf / — validate variables first!

6. Contents & Redirects

  • Print messages (echo)

    echo "Welcome to the terminal"

    Writes arguments to standard output followed by a newline.

    Pitfall: Use printf "%s\n" "$var" instead of echo for arbitrary user strings starting with -n or backslashes.

  • Read file content (cat)

    cat notes.txt

    Concatenates and prints the full contents of files to standard output.

    Pitfall: Use less, head, or tail on huge files instead of dumping megabytes with cat.

  • Redirect and overwrite (>)

    echo "New text" > file.txt

    Redirects stdout into a file, truncating existing contents first.

    Pitfall: Never run sort file.txt > file.txt in place: the shell truncates file.txt BEFORE sort starts reading!

  • Redirect and append (>>)

    echo "Additional note" >> file.txt

    Appends stdout cleanly to the end of the file.

    Pitfall: Combine stdout and stderr into a log cleanly using cmd >> app.log 2>&1.

7. Pipelines & Filters

  • Chain commands (|)

    cat log.txt | grep "ERROR"

    Pipes stdout of the left command directly into stdin of the right command.

    Pitfall: Commands in a pipeline execute in subshells, so variable assignments inside a pipeline do not persist outside.

  • Search within a file (grep)

    grep "warning" sys.log

    Prints every line matching the given pattern.

    Pitfall: Pass -F (fixed strings) when searching literal text containing dots, brackets, or regex metacharacters.

  • Case-insensitive search (grep -i)

    grep -i "error" sys.log

    Matches Error, ERROR, and error regardless of capitalization.

    Pitfall: Add -n for line numbers or -c to count matching lines quickly.

  • Recursive folder search (grep -r)

    grep -r "TODO" src/

    Searches recursively through every file inside the target tree.

    Pitfall: Skip huge generated folders via --exclude-dir=.git --exclude-dir=node_modules.