Hacking, Code & Open Source Reads
// tip of the week series →

Bash !$ for Safer File Deletion

Christian Lehnert2026-05-14~1 min read

Bash !$ for Safer File Deletion

Bash expands !$ to the last argument of the previous command. Used
in the right order, this turns rm into a two-step ritual where the
first step is always a preview.

1$ ls /var/log/myapp/*.log
2/var/log/myapp/access.log
3/var/log/myapp/error.log
4/var/log/myapp/debug.log
5 
6$ rm !$
7rm /var/log/myapp/*.log

The ls shows exactly which files the glob matches. The rm !$
reuses the same argument without retyping. Three files are deleted,
and three files are exactly what you just verified. There is no
chance that your finger slipped on the wildcard between the preview
and the delete.

The pattern works with any destructive operation. mv !$, chmod !$,
git rm !$. The habit is to preview first with ls, head, find,
or anything else read-only, then act with !$ on the same argument.

Human error concentrates at the keyboard. Reduce the typing on the
destructive command to two characters, and the error budget
collapses.

Tagged:
#tip-of-the-week #linux #bash #shell
// series
This is part of the tip of the week series — short, focused notes on Linux, BSD, and the shells, tools, and habits that hold them together. Shipped weekly.
← Back to posts