How can I make a safer Bash alternative to rm?

0
0
Asked By MellowPine42 On

I wrote a Bash function called del that moves files and directories into $HOME/temp/trash instead of deleting them immediately. It also has -show and -clear options, with -clear permanently removing the contents of the trash directory. The goal is to reduce accidental deletions, but I'm unsure whether the implementation is safe and reliable. What bugs or design issues should I fix, and is there a better-established approach for sending files to the trash?

4 Answers

Answered By SageOrbit90 On

The basic idea is reasonable, but remember that this function only affects interactive calls to `del`; scripts that invoke `rm` will continue deleting normally. If a script intentionally needs the system command, it can call `command rm`, but aliases and shell functions are not a general safety mechanism. For everyday use, delegating to the desktop trash implementation is usually more portable and reliable than building a replacement around `mv`.

Answered By CopperLynx56 On

The cleanup operation is the riskiest part. `rm -fr "$trash"/*` does not include hidden entries, has no confirmation, and permanently deletes everything in the directory. It also assumes the trash path is exactly what you expect. Consider requiring an explicit confirmation, refusing suspicious paths, and handling dotfiles deliberately. More importantly, moving something into the trash is only safer if the final purge is carefully protected.

Answered By BlueRook_31 On

A trash directory needs more than just the basename of each file. Two different paths can contain files with the same name, so moving both into one directory creates collisions and loses the original locations. Broken symlinks also fail the `-e` test. If you want a proper recoverable trash system, store the original path and use unique names or metadata. The freedesktop Trash specification already defines how to handle duplicate names, restoration, mount points, and trash metadata; a tool such as `gio trash` is safer than maintaining all of this yourself.

Answered By QuartzMango7 On

There are several important Bash bugs here. `for arg in $@` must be replaced with `for arg in "$@"`, otherwise spaces and glob characters in filenames are broken. The loop also checks `$1` instead of `$arg`, so every iteration examines only the first argument. Option handling returns from the entire function immediately, and mixed commands such as `del file -show` won’t behave as expected. Use a real option parser or clearly separate options from operands, and support `--` so filenames beginning with a dash can be handled safely.

NimbleCedar18 -

Also use `printf '%sn' "object $object not found"` rather than allowing user-controlled filenames to become part of the format string. Check whether `mkdir` and `mv` actually succeed before continuing.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.