Cheat sheets /

Terminal cheat sheet

The bash and zsh one-liners you search for every few weeks: what’s eating the disk, what’s on port 3000, find that file, count those lines.

Download all 37 for Snippet Keeper Get Snippet Keeper

In Snippet Keeper, click Import at the bottom of the sidebar and choose the file. They land in your terminal app, and ones you already have are skipped. The highlighted parts become fill-in fields: type a value (or pick one), and the finished command is on your clipboard.

Files

Find files by name

find dir -name "pattern"

Find big files

find dir -type f -size +size

Folder sizes, biggest last

du -sh dir/* | sort -h

Free disk space

df -h

Search for text in files

grep -rn "text" dir

Replace text in files

Works the same on Mac and Linux, unlike sed -i.

perl -pi -e 's/find/replace/g' files

Follow a log file

tail -f file

Compress a folder

tar -czf name.tar.gz dir

Extract a .tar.gz

tar -xzf file

Make a file executable

chmod +x file

Create a symlink

ln -s target link

Processes and ports

What’s using a port

lsof -i :port

Kill whatever is on a port

kill $(lsof -t -i :port)

Find a process

ps aux | grep -i name

Kill processes by name

pkill -f name

Keep running after you log out

nohup command > log 2>&1 &

Time a command

time command

Rerun a command every few seconds

On a Mac: brew install watch.

watch -n seconds command

Network

Look up a domain’s IP

dig +short domain

Check whether a port is open

nc -zv host port

Your public IP address

curl -s https://ifconfig.me

Text

Most common lines

sort file | uniq -c | sort -rn | head -count

Print one column

awk '{print $column}' file

Pretty-print JSON

On a Mac: brew install jq.

jq . file

Base64-encode text

printf '%s' 'text' | base64

Base64-decode text

printf '%s' 'text' | base64 --decode

Generate a random password

openssl rand -base64 bytes

SHA-256 checksum of a file

shasum -a 256 file

Shell

Search your command history

Type part of an old command; press Ctrl-R again for older matches.

Ctrl-R

Rerun the last command with sudo

sudo !!

Go back to the previous folder

cd -

Add a folder to PATH for this session

export PATH="dir:$PATH"

Reload your shell config

source ~/.file

Mac only

Copy a command’s output to the clipboard

command | pbcopy

Open a folder in Finder

open dir

Flush the DNS cache

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

Keep the Mac awake

Until you press Ctrl-C.

caffeinate -d

Keep these one keystroke away.

Snippet Keeper is a Mac app for the commands you keep looking up. Press ⌥⇧Space in any app, type a few letters, fill in the blanks, and it’s on your clipboard.

Snippet Keeper asking for the values in a kubectl command before copying it