Linux tricks, one-liners & shell magic

#!/bin/vpp.dk

Community Linux tricks, one-liners & shell scripts — searchable & formatted.

+ Add a New Trick

A universal extract function you can add to ~/.bashrc — handles tar.gz, zip, bz2, 7z and more.
bash
$ extract() { case "$1" in *.tar.bz2) tar xjf "$1" ;; *.tar.gz) tar xzf "$1" ;; *.tar.xz) tar xJf "$1" ;; *.zip) unzip "$1" ;; *.7z) 7z x "$1" ;; *.gz) gunzip "$1" ;; *) echo "Unknown format: $1" ;; esac } # Usage: extract archive.tar.gz
Delete this trick? This cannot be undone.
The watch command re-runs a command at a set interval and displays the output — great for monitoring.
bash
$ # Refresh every 2 seconds (default): watch df -h # Every 0.5 seconds, highlight differences: watch -n 0.5 -d 'free -m'
Delete this trick? This cannot be undone.
Find which directories are eating your disk space, sorted from largest to smallest.
bash
$ du -sh /* 2>/dev/null | sort -rh | head -20 # Show only current directory depth 1: du -sh ./*/ | sort -rh
Delete this trick? This cannot be undone.
Securely transfer files between machines using scp. Works in both directions — local to remote and remote to local.
bash
$ # Local -> Remote scp localfile.txt user@host:/remote/path/ # Remote -> Local scp user@host:/remote/file.txt ./ # Recursive directory copy: scp -r ./mydir user@host:/opt/
Delete this trick? This cannot be undone.
Use grep to search through all files in a directory tree for a pattern, showing filenames and line numbers.
bash
$ grep -rn 'search_term' /path/to/dir/ # Case-insensitive, show 2 lines of context: grep -rin -C2 'error' /var/log/
Delete this trick? This cannot be undone.
Stream new lines appended to a log file, useful for monitoring deployments or service output.
bash
$ tail -f /var/log/syslog # Follow multiple files at once: tail -f /var/log/nginx/access.log /var/log/nginx/error.log
Delete this trick? This cannot be undone.
Quickly find the PID of a running process and kill it — no need to first run ps and note the ID manually.
bash
$ kill $(pgrep -f process_name) # Or with a signal: kill -9 $(pgrep -f process_name)
Delete this trick? This cannot be undone.
12