Trash Cli: Command line recycle bin tool on Linux

  • 2020-11-26 19:06:53
  • OfStack

I'm sure everyone is familiar with The Recycle bin (trashcan) because it is common to Linux users, Windows users, or Mac users. When you delete a file or directory, the file or directory is moved to the recycle bin.

Note that after moving files to the recycle bin, file system space is not freed unless the recycle bin is emptied.

If you don't want to delete files permanently (emptying the recycle bin), you can use the recycle bin to temporarily store deleted files, which will help you recover deleted files if necessary.

However, if you delete using the rm command at the command line, you will not be able to find any deleted files or directories in the recycle bin. Therefore, always think three times before executing the rm command. If you make an error (executing the rm command), the file is permanently deleted and cannot be recovered because the metadata stored on disk is no longer there.

According to the specification, garbage (trash) is a feature provided by desktop managers such as GNOME, KDE, and XFCE. When the file manager to delete a file or directory, the file or directory will be rubbish (trash), is then moved to the recycle bin, the recycle bin corresponding directory is $HOME /. local/share/Trash

The recycle bin directory contains two subdirectories: files and info. The files directory stores the actual deleted files and directories, and the info directory contains information about the deleted files and directories, such as the file path, the date and time of deletion, and each file is stored separately.

You might ask, why do you need a command-line tool when you already have a graphical user interface (GUI) recycle bin? Because for most of the guys who use *NIX systems (including me), even if they use a GUI based system, they prefer to use the command line over the GUI. So, if someone is looking for a command-line recycle bin tool, here is a good option.

What is Trash - Cli

trash-cli is a command-line recycle bin tool and complies with the ES46en.ES47en garbage (trash) specification. It can store the name, original path, delete date, and permissions of every garbage file.

How do I install Trash-ES53en on Linux

Most of the official Linux distribution repositories offer the es57EN-ES58en installation package, so you can run the following commands to install it.
For Debian/Ubuntu users, install ES65en-ES66en using the apt-ES63en or apt commands:
$ sudo apt install trash-cli

For RHEL/CentOS users, use the yum command to install ES74en-ES75en:
$ sudo yum install trash-cli

For Fedora users, use the dnf command to install ES82en-ES83en:
$ sudo dnf install trash-cli

For Arch Linux user, use the pacman command to install Trash-ES93en:
$ sudo pacman -S trash-cli

For openSUSE users, use the zypper command to install Trash-ES102en:
$ sudo zypper in trash-cli

If the Trash-ES109en installation package is not available in your distribution, you can also install it using pip. To be able to install the python package, you should have the pip package manager on your system.

$ sudo pip install trash-cli
Collecting trash-cli
Downloading trash-cli-
Installing collected packages: trash-cli
Running bdist_wheel for trash-cli ... done
Successfully installed trash-cli-

How to use Trash-ES118en

Trash-Cli is not difficult to use because it provides a very simple syntax. Trash-Cli provides the following commands:

trash-put: Delete files and directories (put them in the recycle bin only) trash-list: Lists files and directories that have been deleted trash-restore: Recovers the file or directory trash from the recycle bin. trash-rm: Deletes files in the recycle bin trash-empty: Empty the recycle bin

Now, let's try out 1 with some examples

1) Delete files and directories: In this example, we move the file 2g.txt and the folder magi to the recycle bin by running the following command.
$ trash-put 2g.txt magi
Just like what you see in the file manager.

2) List the deleted files and directories: To view the deleted files and directories, you need to run this command. You can then see the details of the deleted files and directories in the output, such as name, date and time of deletion, and file path.

$ trash-list
2017-10-01 01:40:50 /home/magi/magi/2g.txt
2017-10-01 01:40:50 /home/magi/magi/magi

3) Restore files or directories from the recycle Bin: You can restore files and directories at any time by running this command. It will ask you to choose which files or directories you want to recover. In this case, I'm going to restore the 2ES151en.txt file, so My choice is 0.

$ trash-restore
0 2017-10-01 01:40:50 /home/magi/magi/2g.txt
1 2017-10-01 01:40:50 /home/magi/magi/magi
What file to restore [0..1]: 0

4) Delete files from the recycle Bin: If you want to delete certain files in the recycle bin, run this command. In this example, I will delete the magi directory.
$ trash-rm magi

5) Empty the recycle Bin: If you want to delete all files and directories in the recycle bin, you can run this command.
$ trash-empty

6) Delete junk files that exceed X days: Alternatively, you can delete files in the recycle bin that exceed X days by running this command. In this example, I will delete items in the recycle bin that are longer than 10 days.
$ trash-empty 10
Trash-Cli works well, but if you want to try some of its alternatives, you can also try gvfs-ES174en and autotrash.


Related articles: