Rm option to fail on nonexistent files

rmsafety

The man page of rm in GNU coreutils 8.12.197-032bb describes the -f or --force option as "ignore nonexistent files, never prompt". Without this option, it will remove any existing files, never prompt, and return a non-zero exit code if any of the specified files did not exist. I'd like to preserve the files if any of the specified files do not exist. What is the easiest way to do this?

The use case is safety: If I'm trying to remove a file which doesn't exist, it could be because there's an invalid expectation (or plain bug) in the command. For example the famous rm -rf /usr /lib/nvidia-current/xorg/xorg could have been averted in many ways, one of them being such an option (obviously unless the user by some incredible coincidence had a /lib/nvidia-current/xorg/xorg directory), and another being to Use More Quotes™. However, quotes aren't always enough. For example, consider ssh host '/bin/rm some paths; /bin/bash foo.sh' – If I had forgotten the semicolon or inserted pretty much any other symbol like colon or comma, it would have tried to remove /bin/bash and ~/foo.sh.

Best Answer

I use this sort of thing:

mkdir DELETE && mv "some" "paths" DELETE && rm -rf DELETE

For a single path:

mv /some/path DELETE && rm -rf DELETE

Even better, type the rm command on a separate command line: mv /some/path DELETE Enter rm -rf DELETE Enter. That way, the only rm command that makes it into your shell history is on a file called DELETE, so if you removed an old version of a file, you don't risk removing the new version by accidentally pressing Up the wrong number of times then Enter.

If you want to automate a bit:

mv_to_DELETE () {
  mkdir DELETE &&
  mv -- "$@" DELETE/
}
mv_to_DELETE "some" "paths"
rm -rf DELETE
Related Question