Linux tricks, one-liners & shell magic

#!/bin/vpp.dk

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

+ Add a New Trick

Serve the current directory over HTTP for quick file sharing on the LAN — requires only Python 3.
bash
$ python3 -m http.server 8080 # On a specific interface: python3 -m http.server 8080 --bind 0.0.0.0 # Then access from another machine: curl http://<your-ip>:8080/
Delete this trick? This cannot be undone.
Watch live bandwidth usage broken down by network interface using nload or ifstat.
bash
$ # Using nload (apt install nload): nload eth0 # Without extra tools — poll /proc/net/dev: watch -n 1 'cat /proc/net/dev | column -t'
Delete this trick? This cannot be undone.
Before editing a config, make a dated backup with a one-liner. The $(date) sub-shell appends the current timestamp.
bash
$ cp nginx.conf nginx.conf.$(date +%Y%m%d_%H%M%S).bak # Backup and keep last 5 only: cp important.conf important.conf.$(date +%Y%m%d).bak ls -t important.conf.*.bak | tail -n +6 | xargs rm -f
Delete this trick? This cannot be undone.
wc is the Swiss Army knife for file statistics. Combine flags or chain with other commands.
bash
$ wc -l file.txt # lines wc -w file.txt # words wc -c file.txt # bytes # Count non-empty lines: grep -c . file.txt # Total lines across multiple files: wc -l *.log | tail -1
Delete this trick? This cannot be undone.
List all systemd services and their current status on modern Linux systems.
bash
$ systemctl list-units --type=service --state=running # Full list including inactive: systemctl list-units --type=service
Delete this trick? This cannot be undone.
Forgot to sudo? Run the previous command with root privileges without re-typing it.
bash
$ sudo !!
Delete this trick? This cannot be undone.
Print every environment variable in alphabetical order — handy when debugging missing config or checking what a script will see.
bash
$ printenv | sort # Or just search for a specific one: printenv | grep -i path
Delete this trick? This cannot be undone.
List all listening TCP/UDP ports and the process that owns them.
bash
$ # All listening sockets + process names: ss -tulnp # Or with netstat (older systems): netstat -tulnp # Check if a specific port is open: ss -tulnp | grep ':443'
Delete this trick? This cannot be undone.
Use sed with find to do a batch search-and-replace across many files at once.
bash
$ # Replace 'foo' with 'bar' in all .conf files: find /etc -name '*.conf' -exec sed -i 's/foo/bar/g' {} + # Preview changes first (dry-run): grep -rl 'foo' /etc/*.conf
Delete this trick? This cannot be undone.
Access a service running on a remote server as if it were local. Useful for databases, dev servers, etc.
bash
$ # Forward remote port 5432 to local port 5432: ssh -L 5432:localhost:5432 user@remote-host # Persistent background tunnel (no shell): ssh -fNL 8080:localhost:80 user@remote-host
Delete this trick? This cannot be undone.
12