Bash / Shell Cheatsheet

Everyday bash for the command line. Works in zsh with minor differences.

1 credit

Navigation & files

7 items
Current dir
pwd
List (long, hidden, human)
ls -lah
Change dir / home
cd <path> / cd ~
Previous dir
cd -
Create / remove dir
mkdir -p a/b/c / rmdir <d>
Copy / move / remove
cp -r src dst / mv / rm -rf
Find files
find . -name "*.ts" -type f

Text processing

6 items
View / paginate
cat file / less file / head -n 20
Search lines
grep -rni "term" .
Replace in place
sed -i.bak 's/old/new/g' file
Column extract
awk '{print $1,$3}' file
Sort / unique / count
sort | uniq -c | sort -rn
Word/line count
wc -l file

Pipes & redirection

6 items
Pipe stdout
cmd1 | cmd2
Overwrite file
cmd > file
Append
cmd >> file
Redirect stderr
cmd 2> errors.log
Merge stderr + stdout
cmd > out.log 2>&1
Discard
cmd > /dev/null 2>&1

Processes & jobs

5 items
Running processes
ps aux / top / htop
Kill by PID / name
kill -9 <pid> / pkill <name>
Listening ports
lsof -iTCP -sTCP:LISTEN -n -P
Run in background
cmd &
Disown (survive exit)
nohup cmd & / disown

Scripting

bash
#!/usr/bin/env bash
set -euo pipefail

NAME="${1:-world}"
for i in {1..3}; do
  echo "$i hello, $NAME"
done

if [[ -f .env ]]; then
  source .env
fi

Always start scripts with `set -euo pipefail` — errors halt, undef vars error, pipe failures propagate.

Further reading