> Terminal cheatsheet

Learn essential commands for macOS & Linux

Navigation

CommandWhat it doesNotes
cd[dir]
Change working directory cd ~ goes home, cd - goes back to previous dir
ls[-la]
List directory contents macOS ls -G for colour; Linux ls --color
pwd
Print working directory path
pushd[dir]
Push directory onto stack and switch to it Pairs with popd to jump back
popd
Pop directory off stack and return to it dirs shows the current stack

File management

CommandWhat it doesNotes
cp[-r] src dst
Copy files or directories -r required for directories
mvsrc dst
Move or rename files Works across directories; also renames in-place
rm[-rf] file
Remove files or directories -r for dirs, -f to skip prompts — use carefully
mkdir[-p] dir
Create a directory -p creates parent dirs as needed
touchfile
Create an empty file or update its timestamp
ln[-s] src dst
Create a hard or symbolic link -s for symlink; omit for hard link

Viewing files

CommandWhat it doesNotes
catfile
Print file contents to stdout cat -n shows line numbers
lessfile
Page through a file interactively /term to search, q to quit
head[-n N] file
Show first N lines of a file Default is 10 lines
tail[-n N] file
Show last N lines of a file -f follows live output (e.g. logs)
wc[-lwc] file
Count lines, words, or bytes -l lines, -w words, -c bytes

Permissions

CommandWhat it doesNotes
chmodmode file
Change file permissions chmod 755 file or chmod +x file
chownuser[:group] file
Change file owner and/or group -R applies recursively to directories
sudocommand
Run a command as superuser sudo -i opens a root shell
su[user]
Switch to another user account su - loads the user's full environment

Processes

CommandWhat it doesNotes
ps[-aux]
List running processes ps aux shows all processes with CPU/mem
kill[-9] PID
Send a signal to a process by PID -9 (SIGKILL) forces immediate termination
killallname
Kill all processes matching a name killall -9 node force-kills all node processes
top
Interactive process monitor macOS quit with q; Linux k to kill, q to quit
htop
Enhanced interactive process viewer Install via Homebrew (macOS) or apt/dnf (Linux); mouse-friendly
jobs
List background jobs in current shell Shows job numbers used by bg / fg
bg%N
Resume a stopped job in the background Ctrl+Z to stop the current process first
fg%N
Bring a background job to the foreground Omit %N for the most recent job

Networking

CommandWhat it doesNotes
pinghost
Test reachability of a host macOS runs indefinitely by default; use -c N to limit
curl[-X POST] url
Transfer data to/from a URL -o file saves output, -I headers only, -s silent
wgeturl
Download a file from a URL Not built-in on macOS — install via Homebrew; -r recursive
sshuser@host
Open a secure shell connection -i key.pem to specify identity file, -p for custom port
scpsrc user@host:dst
Copy files securely over SSH -r for directories; same syntax reversed to pull files
netstat[-tulnp]
Show network connections and listening ports Linux prefer ss -tulnp on modern systems; netstat deprecated
ss[-tulnp]
Socket statistics — faster netstat replacement Linux only; not available on macOS

Search & text

CommandWhat it doesNotes
grep[-r] pattern file
Search for a pattern in files -i case-insensitive, -n line numbers, -r recursive
findpath -name "*.ext"
Find files matching criteria -type f files only, -mtime -1 modified in last day
sed's/old/new/g' file
Stream editor — transform text macOS sed -i '' for in-place; Linux sed -i (no quotes needed)
awk'{print $1}' file
Pattern-action text processing -F: sets field separator; $NF is the last field
sort[-n] file
Sort lines of text -n numeric sort, -r reverse, -u unique
uniq[-c] file
Filter or count adjacent duplicate lines Usually piped from sort; -c prefixes count

Pipes & redirection

OperatorWhat it doesNotes
|
Pipe stdout of one command to stdin of next ls -la | grep ".txt"
>
Redirect stdout to a file, overwriting it echo "hello" > file.txt
>>
Append stdout to a file echo "line" >> file.txt
<
Redirect file contents to stdin wc -l < file.txt
2>&1
Redirect stderr to stdout command > out.txt 2>&1 captures all output
teefile
Write stdin to file and also pass it through cmd | tee out.log — see it and save it; -a to append

Good to know

CommandWhat it doesNotes
history
Show command history !N re-runs command N; !! re-runs the last command
aliasname='cmd'
Create a shorthand for a command Add to ~/.bashrc or ~/.zshrc for persistence
env
List all environment variables env VAR=val cmd sets a var just for one command
exportVAR=value
Set an environment variable for child processes Without export, the variable stays in the current shell only
whichcommand
Show the full path of an executable Useful to confirm which version of a tool is being run
mancommand
Open the manual page for a command /term to search within man; q to quit
echotext
Print text to stdout -n suppresses trailing newline; -e enables escape sequences
xargscommand
Build and run commands from stdin find . -name "*.log" | xargs rm; -I{} for placeholders
No commands match ""
Copied