Safer rm with trash-cli and cron
rm is efficient, but it is also permanent. A typo or an incorrect glob can remove important files without a recovery path, because command-line deletion normally bypasses the trash directory used by graphical file managers.
Alternatively, a safer command-line workflow can keep the familiar rm interface while routing deletions through the trash first.
One implementation uses the following architecture:
- Frontend:
rmtrash, a drop-inrmreplacement. - Backend:
trash-cli, which manages the actual trash operations. - Cleanup: a cron job that permanently removes old trash entries.
§1. Frontend: rmtrash
rmtrash is a wrapper around rm. It accepts common GNU rm flags such as -f, -i, and -rf, but sends files to the trash instead of permanently deleting them.
Place the script in a directory on PATH:
1 | cp rmtrash ~/bin/rmtrash |
Configure rm as a shell alias for rmtrash:
1 | alias rm=~/bin/rmtrash |
After reloading the shell, rm foo.txt sends foo.txt to the trash instead of deleting it permanently.
§2. Backend: trash-cli
rmtrash depends on trash-cli for the actual trash operations. Install it with pipx:
1 | pipx install trash-cli |
This exposes commands such as trash-put, trash-list, trash-restore, trash-empty, and trash-rm in ~/.local/bin/.
The trash itself follows the FreeDesktop.org layout under ~/.local/share/Trash/:
1 | ls ~/.local/share/Trash/ |
files/- the actual deleted files and directoriesinfo/-.trashinfometadata files containing the original path and deletion timestamp
For example:
1 | ls ~/.local/share/Trash/info/ |
Useful commands:
1 | trash-list # see what is in the trash |
§3. Cleanup: cron
The trash still needs periodic cleanup. A daily cron job handles this:
1 | @daily ~/.local/bin/trash-empty 30 |
This permanently deletes trash entries older than 30 days. The result is a safer command-line rm workflow with a bounded trash directory.