The rm command is probably the most dangerous command to use from the shell in Linux. Files need to be deleted and directories need to be cleaned up all the time. However, one small mistake with rm can lead to disaster. The question that begs then is, why not safely delete to the trash can, just like we do on the desktop?
Thankfully, that is possible thanks to trash-cli. It has commands to move files and directories to the trash can on your Ubuntu desktop.
Here is how I have set up my system to delete files to trash from the shell:
- First, install trash-cli. I prefer installing it from the Ubuntu repositories using the package name trash-cli
trash-cli has many commands: to trash, to empty the trash can, to list contents of the trash can and to restore. The only command I really need is trash-put, which moves files and directories to the trash can. Since its name is pretty long, I have added an alias to my ~/.bash_aliases file that maps this command to a shorter name: can
Next, you need to overcome your habit of using rm for deleting and use can instead. To do this, create an alias in ~/.bash_aliases named rm that reminds you whenever you use rm:
alias rm="echo Use trash-put or /bin/rm"
- For the rare cases where you actually need rm, it is still around. You can use it by providing its full path: /bin/rm
Note: Do not make the mistake of making an alias named rm that maps to trash-put! That can be dangerous when you switch to using another computer which does not have trash-cli and you use rm thinking it does.
Tried with: trash-cli 0.12.10.3 and Ubuntu 12.04 LTS