All cheatsheets

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 aux
waiting on stdin…
grep node
waiting on stdin…
wc -l
waiting on stdin…

Press play — each | wires one command’s stdout into the next’s stdin.

42 entries

Navigation & files8

ls -lah

List all files, long format, human sizes

cd -

Switch to the previous directory

cp -r src dst

Copy a directory recursively

mv a b

Move or rename

rm -rf dir

Remove a directory and contents (careful!)

mkdir -p a/b/c

Create nested directories

ln -s target link

Create a symbolic link

du -sh * | sort -h

Size of each item, sorted

Search & filter8

grep -rn "text" .

Recursive search with line numbers

grep -i / -v / -E

Case-insensitive / invert / regex

find . -name '*.ts'

Find files by name

find . -type f -mtime -1

Files modified in the last day

sort | uniq -c | sort -rn

Count and rank unique lines

awk '{print $1}'

Print the first column

sed -i 's/old/new/g' file

In-place replace

cut -d, -f2

Take the 2nd CSV field

Pipes & redirects7

cmd > file / >> file

Redirect stdout (overwrite / append)

cmd 2>&1

Redirect stderr to stdout

cmd1 | cmd2

Pipe stdout into the next command

cmd1 && cmd2 / ||

Run next on success / on failure

find . -name '*.log' | xargs rm

Build commands from input

cmd | tee file

Write to a file and stdout

diff <(cmd1) <(cmd2)

Compare two command outputs

Variables & scripting9

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

Safe script header (exit on error)

"$var" vs $var

Always 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" done

Loop over files

if [[ -f "$f" ]]; then echo exists fi

Test and branch

myfunc() { local msg="$1" echo "$msg" }

Define a function with local variables

Robust scripts2

trap 'cleanup' EXIT INT TERM

Run cleanup code on exit or signal

exit $? / if cmd; then

Exit codes: 0 = success, non-zero = failure

Permissions & processes8

chmod +x file

Make a file executable

chmod 644 file

Set rw-r--r--

chown user:group file

Change owner and group

ps aux | grep node

Find running processes

kill -9 <pid>

Force-kill a process

lsof -i :3000

Find what's using a port

nohup cmd &

Run immune to hangups, in background

df -h / du -sh

Disk free / directory size