All cheatsheets

Cheatsheets

Bash / shell

Shell commands & operators for files, search, pipes, and processes.

37 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 & scripting6

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

Safe script header (exit on error)

name="world"; echo "hi $name"

Assign and expand a variable

${VAR:-default}

Use a default if VAR is unset

$(command)

Capture command output

for f in *.txt; do echo "$f" done

Loop over files

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

Test and branch

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