Tags: cli, terminal, bash, zsh, shell, navigation, process-management, aliases Last updated: 2026-06-26

Command Line Cheatsheet

Quick Reference

CommandDescription
ls -laList all files with details
cd <dir>Change directory
pwdPrint working directory
cp -r src dstCopy file or directory
mv src dstMove or rename
rm -rf <path>Remove file or directory
grep -r "text" .Search recursively for text
ps auxList all running processes
kill <pid>Terminate a process
aliasList all aliases

Navigation

Directory Traversal

$ pwd                       # Show current directory
$ cd /path/to/dir           # Go to absolute path
$ cd ..                     # Go up one level
$ cd -                      # Go back to previous directory
$ cd ~                      # Go to home directory
$ cd ~/projects             # Go to ~/projects

Listing Files

$ ls                        # List files (bare)
$ ls -l                     # Long format (permissions, size, date)
$ ls -la                    # Long + hidden files (inc . and ..)
$ ls -lh                    # Long + human-readable sizes
$ ls -lt                    # Long + sort by modification time
$ ls -ltr                   # Long + reverse time (newest last)
$ ls -d */                  # List only directories
$ ls *.md                   # List matching a glob

Tree View

$ tree                      # Directory tree (install: apt/brew tree)
$ tree -L 2                 # Limit depth to 2 levels
$ tree -d                   # Directories only
$ tree -a                   # Include hidden files

File Manipulation

Create & Delete

$ touch file.txt            # Create empty file or update timestamp
$ mkdir my-folder           # Create directory
$ mkdir -p a/b/c            # Create nested directories
$ rm file.txt               # Delete file
$ rm -r folder/             # Delete directory recursively
$ rm -rf folder/            # Force-delete (no prompts)
$ rmdir empty-folder        # Delete empty directory only

Copy & Move

$ cp src.txt dst.txt        # Copy file
$ cp -r src/ dst/           # Copy directory recursively
$ cp -u src.txt dst.txt     # Copy only if source is newer
$ cp -p src.txt dst.txt     # Preserve timestamps and permissions
$ mv old.txt new.txt        # Rename file
$ mv file.txt ~/somewhere/  # Move file to another directory

Viewing Files

$ cat file.txt              # Print entire file
$ less file.txt             # Scrollable view (q to quit)
$ head -n 20 file.txt       # First 20 lines
$ tail -n 20 file.txt       # Last 20 lines
$ tail -f log.txt           # Follow (stream new lines live)
$ wc -l file.txt            # Count lines
$ wc -w file.txt            # Count words

File Info

$ file mystery.bin          # Detect file type
$ stat file.txt             # Detailed file metadata
$ du -sh folder/            # Disk usage of folder (summary, human)
$ du -sh *                  # Size of each item in current dir
$ df -h                     # Disk free space (all mounts)

Searching & Finding

grep

$ grep "text" file.txt      # Find lines matching text
$ grep -i "text" *.txt      # Case-insensitive, across multiple files
$ grep -r "text" .          # Recursive search in current dir
$ grep -rl "text" .         # List filenames only
$ grep -v "text" file.txt   # Invert match (lines NOT containing)
$ grep -c "text" file.txt   # Count matching lines
$ grep -n "text" file.txt   # Show line numbers

find

$ find . -name "*.md"       # Find by name pattern
$ find . -type d            # Find directories only
$ find . -type f            # Find files only
$ find . -mtime -1         # Modified in last 24 hours
$ find . -size +10M         # Files larger than 10 MB
$ find . -name "*.log" -delete  # Find and delete matching files

Other Search Tools

$ which python              # Show path to executable
$ whereis python            # Locate binary, source, and man pages
$ locate filename           # Fast indexed search (requires mlocate)

Process Management

Listing Processes

$ ps aux                    # All processes, all users, detailed
$ ps aux | grep nginx       # Filter by name
$ top                       # Interactive process viewer
$ htop                      # Colourful interactive viewer
$ pgrep -l nginx            # List PIDs matching name
$ jobs                      # List background jobs in current shell

Killing & Signals

$ kill 1234                 # Graceful terminate (SIGTERM)
$ kill -9 1234              # Force kill (SIGKILL)
$ kill -15 1234             # Same as plain kill (SIGTERM)
$ pkill nginx               # Kill by name
$ killall nginx             # Kill all processes by name
$ kill -l                   # List all signal names

Background & Foreground

$ sleep 30 &                # Run in background
$ Ctrl+Z                    # Suspend current foreground job
$ bg                        # Resume suspended job in background
$ fg                        # Bring background job to foreground
$ fg %1                     # Bring job #1 to foreground
$ disown                    # Detach job from shell (survive shell exit)
$ nohup long-script.sh &    # Run immune to hangups

I/O Redirection & Pipes

$ cmd > file.txt            # Redirect stdout to file (overwrite)
$ cmd >> file.txt           # Redirect stdout to file (append)
$ cmd 2> errors.txt         # Redirect stderr to file
$ cmd &> all.txt            # Redirect both stdout and stderr
$ cmd1 | cmd2               # Pipe stdout of cmd1 into cmd2
$ cmd1 | tee file.txt       # Pipe to file AND stdout
$ cmd < input.txt           # Read stdin from file
$ cmd <<< "string"          # Here-string — pass string as stdin

Permissions

Viewing & Understanding

$ ls -l                     # Shows rwxrwxrwx (user, group, others)
# r=read (4), w=write (2), x=execute (1)
# chmod 755 = rwxr-xr-x
# chmod 644 = rw-r--r--

Changing Permissions

$ chmod 755 script.sh       # rwxr-xr-x (numeric)
$ chmod u+x script.sh       # Add execute for user (symbolic)
$ chmod g-w file.txt        # Remove write for group
$ chmod -R 755 folder/      # Recursive
$ chown user:group file.txt # Change owner and group
$ chown -R user:group dir/  # Recursive

Aliases & Shell Customisation

Creating Aliases

# Add these to ~/.bashrc or ~/.zshrc
alias ll="ls -la"
alias gs="git status"
alias gp="git pull"
alias ..="cd .."
alias ...="cd ../.."
alias rmrf="rm -rf"
alias serve="python -m http.server 8080"
alias ports="netstat -tulanp"
alias reload="source ~/.zshrc"

Applying Changes

$ source ~/.bashrc          # Reload bash config
$ source ~/.zshrc           # Reload zsh config
$ exec bash                 # Replace shell with fresh bash
$ exec zsh                  # Replace shell with fresh zsh

Useful Built-in Aliases

$ alias                     # List all defined aliases
$ unalias ll                # Remove an alias
$ type ll                   # Show what ll resolves to

History & Productivity

$ history                   # Show command history
$ history | grep git        # Search history
$ !!                        # Re-run last command
$ !git                      # Re-run last command starting with "git"
$ !$                        # Last argument of previous command
$ !*                        # All arguments of previous command
$ Ctrl+R                    # Reverse-search history interactively
$ Ctrl+G                    # Cancel reverse search

History Settings (~/.bashrc / ~/.zshrc)

HISTSIZE=10000              # Lines kept in memory
HISTFILESIZE=20000          # Lines kept on disk
HISTCONTROL=ignoredups      # Don't store duplicate adjacent commands
shopt -s histappend         # Append, don't overwrite history file

Scripting Basics

Variables

$ NAME="value"              # Assign (no spaces around =)
$ echo "$NAME"              # Use variable (quote in strings)
$ export VAR="val"          # Export to child processes
$ echo $?                   # Exit code of last command (0 = success)

Conditionals

if [ -f file.txt ]; then
    echo "exists"
fi

if [ -d folder/ ]; then
    echo "directory exists"
fi

# Common tests:
# -f  file exists
# -d  directory exists
# -z  string is empty
# -n  string is non-empty

Loops

for f in *.txt; do
    echo "Processing $f"
done

while true; do
    echo "tick"
    sleep 1
done

Tips