Best practices to alias the rm command and make it safer
Some time ago I erroneously deleted my home folder because I ran a rm -rf *
on the wrong terminal, whose working directory was the home folder!
I wish I had an alias for the rm
command, but it was not the case.
Now, I am planning to make a script as an alias for rm
.
Do you have any best practice to suggest?
Thanks.
Solution 1:
If you want a customized rm
, don't call it rm
but a name of yours, myrm
, delete
or whatever.
The rm='rm -i'
alias is an horror because after a while using it, you will expect rm
to prompt you by default before removing files. Of course, one day you'll run it with an account that hasn't that alias set and before you understand what's going on, it is too late.
In any case, a good way to be prepared for file loss or corruption is doing backups.
A fast alternative that will protect you against accidental file deletion or overwriting is using a file system that support unlimited snapshots like ZFS. If frequent snapshots are done automatically, you can recover the files at the state they were during the last snapshot before the incident.
Solution 2:
If you want save aliases, but don't want to risk getting used to the commands working differently on your system than on others, you can to disable rm
like this
alias rm='echo "rm is disabled, use remove or trash or /bin/rm instead."'
Then you can create your own safe alias, e.g.
alias remove='/bin/rm -irv'
or use trash
instead.