Cheatsheets
Bash / shell
Shell commands & operators for files, search, pipes, and processes.
Visualize
Bash — a pipeline (stdout → stdin)
Each command's stdout becomes the next command's stdin — watch the stream narrow to a single count.
ps auxwaiting on stdin…grep nodewaiting on stdin…wc -lwaiting on stdin…Press play — each | wires one command’s stdout into the next’s stdin.
42 entries
Navigation & files8
ls -lahList all files, long format, human sizes
cd -Switch to the previous directory
cp -r src dstCopy a directory recursively
mv a bMove or rename
rm -rf dirRemove a directory and contents (careful!)
mkdir -p a/b/cCreate nested directories
ln -s target linkCreate a symbolic link
du -sh * | sort -hSize of each item, sorted
Search & filter8
grep -rn "text" .Recursive search with line numbers
grep -i / -v / -ECase-insensitive / invert / regex
find . -name '*.ts'Find files by name
find . -type f -mtime -1Files modified in the last day
sort | uniq -c | sort -rnCount and rank unique lines
awk '{print $1}'Print the first column
sed -i 's/old/new/g' fileIn-place replace
cut -d, -f2Take the 2nd CSV field
Pipes & redirects7
cmd > file / >> fileRedirect stdout (overwrite / append)
cmd 2>&1Redirect stderr to stdout
cmd1 | cmd2Pipe stdout into the next command
cmd1 && cmd2 / ||Run next on success / on failure
find . -name '*.log' | xargs rmBuild commands from input
cmd | tee fileWrite to a file and stdout
diff <(cmd1) <(cmd2)Compare two command outputs
Variables & scripting9
#!/usr/bin/env bash
set -euo pipefailSafe script header (exit on error)
"$var" vs $varAlways double-quote variable expansions
[[ -f "$file" ]] && [[ "$a" == "$b" ]]Use [[ ]] for tests in bash — smarter than [ ]
${VAR:-default}Use a default if VAR is unset
name="world"; echo "hi $name"Assign and expand a variable
$(command)Capture command output (command substitution)
for f in *.txt; do
echo "$f"
doneLoop over files
if [[ -f "$f" ]]; then
echo exists
fiTest and branch
myfunc() {
local msg="$1"
echo "$msg"
}Define a function with local variables
Robust scripts2
trap 'cleanup' EXIT INT TERMRun cleanup code on exit or signal
exit $? / if cmd; thenExit codes: 0 = success, non-zero = failure
Permissions & processes8
chmod +x fileMake a file executable
chmod 644 fileSet rw-r--r--
chown user:group fileChange owner and group
ps aux | grep nodeFind running processes
kill -9 <pid>Force-kill a process
lsof -i :3000Find what's using a port
nohup cmd &Run immune to hangups, in background
df -h / du -shDisk free / directory size