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:
For a single path:
Even better, type the
rm command
on a separate command line:mv /some/path DELETE
Enterrm -rf DELETE
Enter. That way, the onlyrm
command that makes it into your shell history is on a file calledDELETE
, 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: