User-proofing
This article may be too technical for most readers to understand. Please help improve this article to make it understandable to non-experts, without removing the technical details. The talk page may contain suggestions. |
Systems administrators, designers, and even users often attempt to defend themselves against accidentally deleting files by creating an alias or function along the lines of:
alias rm="rm -i" rm { /bin/rm -i "$@" ; }This results in rm
asking the user to confirm on a file-by-file basis whether it should be deleted, by pressing the Y or N key. Unfortunately, this tends to train users to be careless about the wildcards they hand into their rm commands, as well as encouraging a tendency to alternately pound y and the return key to affirm removes - until just past the one file they needed to keep. Users have even been seen going as far as "yes | rm files
".
A compromise that allows users to confirm just once, encourages proper wildcarding, and makes verification of the list easier can be achieved with something like:
if ; then rm { ls -FCsd "$@" echo 'remove? ' | tr -d '\012' ; read if ; then /bin/rm -rf "$@" else echo '(cancelled)' fi } fiIt's important to note that this function should not be made into a shell script, which would run a risk of it being found ahead of the system rm in the search path, nor should it be allowed in non-interactive shells where it could break batch jobs. Enclosing the definition in the if ; then .... ; fi
construct protects against the latter.
There exist third-party wrappers that prevent accidental deletion of important files, like "safe-rm".
Read more about this topic: Rm (Unix)