Linux tricks, one-liners & shell magic

#!/bin/vpp.dk

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

Category: scripting — 5 trick(s)

+ Add a New Trick

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.
Forgot to sudo? Run the previous command with root privileges without re-typing it.
bash
$ sudo !!
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.
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.
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.