We all know that when we delete a file/folder, they will be moved to a temporary location called 'Trash' or 'Recycle bin'. We can delete the contents of Trash permanently or restore them if we need them later. This is how we use graphical Trashcans. However, If you're a heavy command-line user and looking for a command-line alternative way to use Trashcan from the Terminal, you need to check 'Trash-cli' utility.
In this brief tutorial, we will be discussing what is trash-cli, and how to install and use 'Trash-cli' utility in Linux.
Table of Contents
Trash-cli : A Commandline Trashcan (Recycle Bin) For Unix-like Systems
Trash-cli is a commandline trashcan for Linux and Unix-like systems. It uses the same trashcan used by GNOME, KDE, and XFCE, but we can use it right from the Terminal. Using trash-cli, we can delete, restore items as the way we do in the graphical Trashcan.
Install Trash-cli
Trash-cli is available in the default repositories of most Linux distributions.
To install it on Arch Linux and its derivatives:
$ sudo pacman -S trash-cli
On Debian, Ubuntu, Linux Mint:
$ sudo apt-get install trash-cli
On Fedora and other RPM based systems:
$ sudo dnf install trash-cli
Or,
$ sudo yum install trash-cli
If Trash-cli is not available on the repositories, you can compile and install it from sources.
To install it on system-wide, run the following commands one by one.
$ git clone https://github.com/andreafrancia/trash-cli.git
$ cd trash-cli
$ sudo python setup.py install
For user-level installation, run.
$ python setup.py install --user
How to Use Trash-cli
Trash-cli supports the following functions:
- trash-put - Delete files and folders,
- trash-empty - Empty the trashcan,
- trash-list - List deleted files and folders,
- trash-restore - Restore a trashed file or folder,
- trash-rm - Remove individual files from the trashcan.
Allow me to show you some examples, so you can understand how it works!
To send a file named 'file.txt' to trashcan, run:
$ trash-put file1
Similarly, we can trash a directory using command:
$ trash-put dir1
You can verify it whether the files or folders have been sent to the trashcan in your file manager.
As you see in the above output, file1 and dir1 have been sent to Trash folder.
To view the contents of your Trashcan from Terminal, run:
$ trash-list
Sample output would be:
2017-02-16 15:42:50 /home/sk/dir1 2017-02-16 15:41:47 /home/sk/file1
You can restore deleted items at any time using command:
$ trash-restore
It will ask you which file you'd like to restore. Just enter the number (0 or 1 in our case). The respective file will be restored. I wanted to restore dir1, so I entered 0(zero).
Sample output:
0 2017-02-16 15:42:50 /home/sk/dir1 1 2017-02-16 15:41:47 /home/sk/file1 What file to restore [0..1]: 0
now, the selected item will be restored.
To empty all items in the trashcan, you can use:
$ trash-empty
Also, you can remove a single folder/file from the Trashcan as shown below.
$ trash-rm file1
The above command will permanently delete the specified item i.e. file1.
Trash-cli also allows to remove contents older than x days. Say for example, to remove contents older than 7 days, run the following command:
$ trash-empty 7
Suggested Read:
You know now how to use trashcan from the commandline using trash-cli utility. Hope you find it useful.
Resource:
7 comments
neat, kind of want them to make it… rm (as alias or symlink) compatible, no more accidents, lol
“you can permanently _restore_ individual item as shown below.
trash-rm file1”
oops, this is gonna be a painful experience lol. please fix the text =)
Fixed it. Thank you.
This is so incredibly useful. I can’t believe I’m a Linux veteran and I had no idea this package even existed, until now. Thank you so much.
trash-restore: command not found
LOL
after googling it seems the command is restore-trash
Ha…I wonder if its ubuntu specific?
root@jpb-ubuntu16-64:~# rm txt
txt txt.txt
root@jpb-ubuntu16-64:~# rm txt.txt
rm is disabled. Please use trash , trash-list, restore-trash commands or /bin/rm instead. Thank you txt.txt
root@jpb-ubuntu16-64:~# trash-list
2018-11-15 15:48:05 /root/bsplayer-13_1_0-23201-amd64.deb
2018-11-15 15:54:29 /home/bsp/.config/Alex_BroadSign.conf
2018-11-15 15:54:32 /home/bsp/.config/BroadSign.conf
2018-11-15 09:35:55 /root/test.txt
2019-01-08 16:19:33 /opt/broadsign/suite/bsp/lib/i965_drv_video.so
root@jpb-ubuntu16-64:~# restore-trash
0 2018-11-15 15:48:05 /root/bsplayer-13_1_0-23201-amd64.deb
1 2018-11-15 09:35:55 /root/test.txt
What file to restore [0..1]:
Anybody know where the files are stored? Is this a script error?
In my case, as I have both trash-cli and gnome-shell I have this alias:
(( $+commands[trash-cli] && $+commands[gio] )) && alias rm=’gio trash’
The GIO is a kind of API for gnome shell
https://en.wikipedia.org/wiki/GIO_(software)