Linux Command Reference
The snippets I’d otherwise have to look up.
systemd
Enable + start (or disable + stop) in one
sudo systemctl enable --now service-name
sudo systemctl disable --now service-name Disable systemd-resolved
sudo systemctl disable systemd-resolved.service For when port 53 needs to belong to something else.
SSH
Install a public key without ssh-copy-id
cat ~/.ssh/id_ed25519.pub | ssh user@remote-host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys" Fallback for stripped-down systems and appliances.
Docker
Validate a compose file without running it
docker compose config --quiet && printf "OK\n" || printf "ERROR\n" Cheap pre-deploy sanity check.
Time and timezone
Sync hardware clock and set timezone
sudo hwclock --systohc
timedatectl set-timezone Australia/Melbourne Databases
Initialise the MariaDB data directory
mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql First-time setup on Arch.
Image optimisation
Hardest PNG compression
optipng -o7 -zc9 <image> -o7 is max optimisation, -zc9 is max zlib compression. Slow but worth it for static assets.
System maintenance
Disable the Ubuntu Pro nag
sudo mkdir /etc/apt/apt.conf.d/off
sudo mv /etc/apt/apt.conf.d/20apt-esm-hook.conf /etc/apt/apt.conf.d/off MPD
Queue the entire library
mpc ls | mpc add Update the music database
mpc update Obscure but wonderful
Commands I keep rediscovering. Most aren’t installed by default.
pv — pipe viewer
tar c bigdir | pv | gzip > out.tar.gz Live progress and throughput for any pipe.
progress — attach to already-running coreutils
progress -m Scans /proc for active cp/mv/dd/tar and shows progress.
sponge — write back to the same file safely
sort file | sponge file
jq '.' messy.json | sponge messy.json sort file > file truncates before reading. sponge buffers until stdin closes, then writes.
ts — timestamp lines from stdin
tail -f /var/log/syslog | ts '[%Y-%m-%d %H:%M:%.S]' For logs and outputs that don’t include their own timestamps.
vipe — open $EDITOR mid-pipe
cat data.csv | vipe | jq -R 'split(",")' Pauses the pipe in your editor. Whatever you save continues down the pipe.
namei -lx — walk every component of a path
namei -lx /var/www/html/secret.txt Debug “permission denied” by seeing exactly which segment in the chain breaks.
column -t — auto-align tabular output
mount | column -t
cat /etc/passwd | column -t -s: Use -s to set a separator other than whitespace.
hyperfine — proper command benchmarking
hyperfine --warmup 3 'fd pattern' 'find . -name "*pattern*"' Runs commands repeatedly with statistical analysis and side-by-side comparison.
watchexec — run a command on file changes
watchexec -e rs cargo test
watchexec -w src 'npm run build' The modern entr. Language-aware filters and sane defaults.
socat — Swiss army knife for sockets
# Forward port 8080 to an internal host
socat TCP-LISTEN:8080,fork TCP:internal-host:80
# Talk to the Docker socket interactively
socat - UNIX-CONNECT:/var/run/docker.sock Forward anything to anything: TCP, UDP, Unix sockets, serial, files. Same syntax.
Tips
sudo !!
Re-run the last command with sudo.