norboten · cheat sheet

Deleting files is the one thing a script must get exactly right — cheat sheet

bash, boot-systemd
set -euo pipefail
cd -- "$dir"                              # stops the script if it fails (with -e)
cd "$dir" || exit 1                       # the same, without -e
: "${DIR:?DIR must be set}"               # abort if unset or empty
for f in ./*; do [ -d "$f" ] && [ ! -L "$f" ] && …; done   # directories, not links
shopt -s nullglob                         # an unmatched glob expands to nothing
arr=(); arr+=("$x"); echo "${#arr[@]}"    # array append, length
"${arr[@]:0:n}"                           # slice: the first n elements
find . -mindepth 1 -maxdepth 1 -type d -print0 | sort -z | xargs -0 …   # NUL-safe pipeline
cmd | xargs printf '[%s]\n'               # see the arguments xargs would pass
ls -l --time-style=+%F_%T                 # full modification times
cp -a src dst                             # copy keeping modes, links and mtimes
systemctl status NAME.timer               # enabled? active? next trigger?
systemctl list-timers --all               # every timer, with next and last run
systemctl cat NAME.service                # the unit plus its drop-ins
systemd-analyze calendar '*-*-* 03:30:00' # when a calendar expression fires
sudo systemctl edit NAME.service          # create a drop-in (clears nothing by itself)
sudo systemctl daemon-reload              # after editing unit files by hand
sudo systemctl enable --now NAME.timer    # boot link + start now
sudo systemctl start NAME.service         # run the job once, exactly as the timer would
journalctl -u NAME.service -b             # what the last runs printed