In the next article we are going to take a look at trash-cli. This is a small pack of free software commands that will provide us with a trash can for the Command Line Interpreter. Nowadays, whatever system you use, be it Gnu / Linux, Windows or Mac OS from the graphical environment, when you delete a file, it usually ends up in the Trash. This gives us the option to repent and restore the file or delete it permanently. On the other hand, if you delete a file from the command line, the option to recover those files, in principle we will not have it.
As I was saying, trash-cli is a client to manage the recycle bin from the command line. If you create an alias so that when you use rm, you are actually using trash-cli, you will have a good way to prevent data loss. With this you can avoid deleting files that you really did not want to delete, either by carelessness or by mistake. This is so because when you delete something with trash-cli, it will stay in the trash.
Later if you want to recover the files, it can be done easily. What’s more, trash-cli will remember the date a certain file was deleted, its permissions and the path where it was located before it was deleted. So we can recover them as they were.
Install trash-cli on Ubuntu
Installing trash-cli is very easy in Ubuntu using apt package manager. All you need to do is open a terminal (Ctrl + Alt + T) and run the command:
sudo apt-get install trash-cli
The trash-cli tool is based on Python, so We can also install it from the sources. The procedure is common for all distros. You just need to open a terminal (Ctrl + Alt + T) and run the commands:
git clone https://github.com/andreafrancia/trash-cli.git cd trash-cli sudo python3 setup.py install sudo python3 setup.py install --user
Example of use
Once installed, users we can now use the commands:
- trash-put: to delete files and directories.
- trash-empty: empty the bin.
- trash-list: lists the files and folders that are in the trash.
- trash-restore– Restore files and folders that are in the trash.
- trash-rm– Individually delete a specific file that is in the trash.
Send a file to the trash
Now we can use these commands available from the terminal in a very simple way. For example, if we want send something to trash, instead of using rm (that would be in principle unrecoverable), you can use this other command:
trash-put ejemplo-trash-cli.txt
Trash-cli doesn’t actually delete the files or directories, it just moves them to a hidden directory. NOTE, each file or directory will save the path it was originally on. That means that if you later restore it, it will be to the directory from which we removed it.
The hidden directory to which the files and directories are removed we can see it with the command:
ls -la $HOME/.local/share/Trash
In this hidden directory you will find another two directories:
- files: This is where the trash-put command will move the deleted file or directory.
- info: The command group handles a .trashinfo file for each deleted file / directory.
List deleted files or directories
For list the files or directories found in the trash, from the command line we execute:
trash-list
Free the trash
As everyone knows, files in the trash take up disk space. For this reason it is a good habit from time to time, free up trash space. And we can do this with the command:
trash-empty
Running it like this, without parameters it will delete EVERYTHING we have in the trash.
In case of wanting delete files or directories of x days stored, we will have to pass the number of days as a parameter. For example, if we want to delete what was stored in the last 5 days we would write:
trash-empty 5
Restore files or directories
This is the command for which you may be interested in installing trash-cli, and it is that it will allow us to restore what was deleted. Trash-restore restores files or directories to their original place, remember that trash-put stores this info.
trash-restore
Deleting files from the trash
With the trash-rm command we can permanently delete files or directories from the trash. We have several ways to accomplish this task. With the first we will delete a file by name:
trash-rm ejemplo-trash-cli.txt
We can also choose remove all files with a specific extension:
trash-rm '*.txt'
If we wanted remove a folder from the trash, the command to use would be:
trash-rm carpeta-ejemplo
Help
The most complete help can be found on the man page. To consult it we will only need to execute the command:
man trash
The trash-cli command in Gnu / Linux allows us to delete files and directories. It will also allow us to recover them by saving them with their absolute path, permissions, date. It can consult more about this project from page on GitHub of the same.