How to delete to trash from the shell in Ubuntu

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:

  1. First, install trash-cli. I prefer installing it from the Ubuntu repositories using the package name trash-cli

  2. 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

  3. 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"
  1. 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 and Ubuntu 12.04 LTS

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s