How to use Trash-CLI with Ranger

When a file is deleted using Ranger, that deletion is permanent. Instead, if you would like to delete files to Trash, then you can do that using the Trash-CLI set of tools.

Installing them is easy:

$ sudo apt install trash-cli

The tool we are interested in is trash-put which deletes a file to the Trash. The Trash directory is usually ~/.local/share/Trash. Any key can be mapped in Ranger to this command for deletion.

For example, to map the Del key to delete to Trash, add this to your ~/.config/ranger/rc.conf:

map <DELETE> shell -s trash-put %s

Tried with: Ranger 1.6.1, Trash-CLI 0.12.7-1 and Ubuntu 14.04

Advertisements

How to find size of Trash directory from commandline

The trash-cli set of tools are a great way to view, add and manage files in the Trash directory. One of the few features missing is a way to view the size of the Trash directory.

This can be done easily using the du command on the Trash directory:

$ du -hs ~/.local/share/Trash

If you use this frequently, you may want to add an alias to your shell:

alias trash-size="du --human-readable --summarize ~/.local/share/Trash"

Tried with: Ubuntu 12.04 LTS

How to empty trash from shell in Ubuntu

The tools in trash-cli can be used to manage the trash can in Ubuntu. It can be installed easily by using the package name trash-cli.

To empty the trash can from the shell, just call trash-empty.

I find that sometimes the Empty Trash command in the GUI does not empty! At such times, this shell command has always worked.

Tried with: trash-cli 0.12.10.3 and Ubuntu 12.04 LTS

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 0.12.10.3 and Ubuntu 12.04 LTS