Bash & The Terminal

Pipes, process control, the text-tool trinity and the shell one-liners that replace small programs.

Pipes & redirection

cmd1 | cmd2
stdout → stdin; the whole philosophy in one character
> >> 2>&1
overwrite · append · send stderr where stdout goes
cmd >/dev/null 2>&1
silence everything
tee
write to file AND pass through: cmd | tee log.txt
$(cmd)
command substitution — use the output inline
xargs
stdin → arguments: ls *.log | xargs rm

The text trinity

grep -rn "x" src/
recursive search with line numbers; -v invert, -E regex, -c count
sed "s/old/new/g"
stream replace; -i edits in place; -n "5,10p" prints a range
awk "{print $2}"
column extraction; -F, sets delimiter; $NF = last field
sort | uniq -c | sort -rn
the frequency-count pipeline — memorize it whole
cut -d: -f1
lighter column cutter for delimited data
head / tail -f
first lines / follow a growing log live

Processes & jobs

ctrl-z · bg · fg
suspend · resume in background · bring back
ps aux | grep x · kill -9
find and kill; try plain kill (TERM) before -9
nohup cmd &
survive terminal close; check jobs
lsof -i :3000
what is holding that port
time cmd
wall/user/sys — first stop for "why slow"

Script survival

set -euo pipefail
die on error, unset vars, and pipe failures — top of every script
if [[ -f file ]]
file exists; -d dir · -z empty string
for f in *.txt; do … done
the loop you write weekly
"${var:-default}"
default values; ALWAYS quote "$var"
ctrl-r
reverse history search — the highest-ROI keybinding in Unix