All cheatsheets

Linux Shell Commands Cheat Sheet

Quick reference for everyday Linux / macOS shell commands: file operations, text processing, permissions, processes, networking, archive and storage — with common options and practical examples.

40 commands

Files & Navigation

pwd

Print the absolute path of the current working directory.

pwd
ls

List directory contents.

-l long format; -a include hidden; -h human sizes; -t sort by mtime; -r reverse

ls -lah /var/log
cd <dir>

Change the current working directory.

cd ~/-projects/app && pwd
tree <dir>

Recursively list a directory as a tree; great for overview (may need install).

-L <n> max depth; -a show hidden; -d dirs only; -I pattern

tree -L 2 -I 'node_modules|.git' .

File Operations

mkdir -p <dir>

Create a directory (and any missing parents) in one shot.

-p create parents; -v verbose; -m <mode> set permissions

mkdir -p src/components/ui
cp <src> <dest>

Copy files or directories.

-r recursive (for dirs); -i interactive; -v verbose; -p preserve mode/time

cp -rv src/ backup/
mv <src> <dest>

Move (rename) files or directories.

-i interactive; -v verbose; -n no-overwrite

mv README.md readme.md && mv ../tmp .
rm <path>

Remove files or directories. ⚠️ there is no trash — double-check paths.

-r recursive; -f force; -i interactive; -v verbose

rm -rf build/ node_modules/
ln -s <target> <link>

Create a symbolic link at <link> pointing to <target>.

-s symbolic (default: hard); -f overwrite existing; -n do not follow existing

ln -s /opt/app-v2 current
touch <file>

Create an empty file or update its mtime without changing contents.

touch .gitignore

Text & Viewing

cat <file>

Print the entire contents of a file to stdout.

-n number lines; -A show non-printing chars; -s squeeze blanks

cat -n package.json
less <file>

Open a file in a paginator (use /, n, N to search; q to quit).

-N line numbers; -S no wrap; +F auto-follow tail-like

less -NS /var/log/syslog
head -n 10 <file>

Output the first N lines (default 10) of a file.

head -n 30 /etc/passwd
tail -n 50 -f <file>

Show the last N lines and follow the file as it grows (for logs).

-f follow; -F follow & retry; -n lines (default 10); -c bytes; --pid=PID stop when PID dies

tail -f -n 50 /var/log/nginx/access.log

Search & Filter

grep <pattern> <files>

Search lines matching a pattern; supports extended and PCRE regex.

-i case-insensitive; -r recursive; -n line numbers; -v invert; -E extended regex

grep -rni "TODO" src/
find <path> -name <pattern>

Walk a directory tree and list files matching criteria.

-type f|d; -name glob; -iname case-insensitive; -size +1M; -mtime -7; -maxdepth

find . -type f -name "*.log" -mtime +7 -delete
awk '<prog>' <file>

Pattern-action text scanner — slicing columns, summing values, etc.

-F fs field separator; -v var=value; -f script file

awk -F'\t' 'NR>1 {sum+=$3} END {print sum}' data.tsv
sed 's/<a>/<b>/' <file>

Stream editor — substitution, deletion, insertion per-line.

-i edit in place; -E extended regex; -e combine exprs; s/from/to/[g] [p]

sed -i '' 's/version = "1\.."/version = "1.9"/' pyproject.toml
sort | uniq -c | sort -nr

Pipeline idiom: count occurrences and sort by frequency descending.

awk '{print $1}' access.log | sort | uniq -c | sort -nr | head

Permissions

chmod <mode> <file>

Change file permissions (e.g., 755, u+x, g-w, o=r).

-R recursive; --reference=<file> copy mode

chmod -R u=rwX,go=r ./docs
chown <user>[:<group>] <file>

Change file owner and group.

-R recursive; -h affect symlinks (with -R)

chown -R www-data:www-data /var/www/html
umask

Show/set the default permission mask for newly created files.

-S symbolic; -p umask preserved for portability

umask 022

Processes & System

ps aux

Show all processes with detailed info (BSD-style columns).

-e all; -f full; -L threads; --sort=-%mem

ps auxf | grep -v grep | grep nginx
top / htop

Live, interactive process viewer (htop has nicer UI; may need install).

P/CPU M/MEM sort; k kill; r renice; q quit

htop -u www-data
kill [-9|<sig>] <pid>

Send a signal to a process. SIGTERM (15) is polite; SIGKILL (9) is forceful.

-9 SIGKILL; -l list signals; -s specify signal by name

kill -TERM 12345 && sleep 2 && kill -9 12345
jobs / bg / fg / nohup

Manage job control — list, background, foreground, run immune to hangups.

nohup ./long-task.sh > out.log 2>&1 &
systemctl <verb> <unit>

Control systemd services and units (status, start, stop, enable, logs).

status / start / stop / restart / enable / disable; -u <user>

systemctl status nginx && systemctl reload nginx

Network

wget <url>

Non-interactive downloader; recursive / mirror friendly.

-c continue; -q quiet; --no-check-certificate; -r recursive

wget -c https://example.com/big-archive.tar.gz
ssh <user>@<host> [cmd]

Open an SSH session or run a one-off command on a remote host.

-p <port>; -i <key>; -L / -R / -D port forwardings; -N no remote cmd; -f background

ssh -i ~/.ssh/id_ed25519 user@host 'sudo systemctl restart nginx'
scp / rsync

Copy files to/from a remote host. rsync is faster for partial / repeated transfers.

scp -r / rsync -avz --progress src/ user@host:/dst/

rsync -avz --progress ./build/ deploy@server:/srv/app/
ping / traceroute / ss

Diagnose reachability, path and listening sockets.

ping -c N count; ss -tulnp listen info; ss -s summary

ss -tulnp | grep -E ':80|:443'

Archive & Compress

tar -czf out.tgz <paths>

Create or extract tar archives, optionally with gzip/bzip2/xz.

-c create; -x extract; -t list; -z gzip; -j bzip2; -J xz; -v verbose; -C <dir>

tar -czf - src/ | ssh server 'tar -xzf - -C /srv/app'
zip -r out.zip <paths>

Create a zip archive recursively.

-r recurse; -9 max compression; -e encrypt; -j junk paths

zip -r9 build-$(date +%Y%m%d).zip dist/
unzip [-d <dir>] file.zip

Extract a zip archive, optionally to a target directory.

-d target dir; -o overwrite; -l list; -p to stdout; -q quiet

unzip -o build.zip -d ./extracted

Disk & Storage

df -h

Show mounted filesystem disk space usage, human-readable.

-h human; -T print filesystem type; -i inodes

df -hT /
du -sh <path>

Estimate file space usage; -s summarizes a directory.

-s summarize; -h human; -a all; --max-depth=N; -x stay on one fs

du -sh node_modules/ build/ .

Shell Built-ins

history / !! / !$

Browse, replay and re-use previous commands.

history N; !<n> rerun line N; !$ = last arg of last command; !! rerun last

git add . && git commit -m "$(!!)"
alias / unalias

Define or list shell command shortcuts (often in ~/.bashrc / ~/.zshrc).

alias ll='ls -lah' && alias gs='git status'
env / export / unset

List, set and clear environment variables for child processes.

export DATABASE_URL=postgres://localhost/dev && env | grep DATABASE

Help

man <cmd> / <cmd> --help

Read the manual page (man) or built-in help for a command.

man -k keyword search; man -t <cmd> print to PDF; --help short usage

man -k 'tar archive'

Cheatsheet version 1.0.0

Covers bash 5+ / POSIX 2017