In this short tutorial, we’re going to learn how to soft-delete files in Linux systems.
When we soft delete a file we just move it to the trash instead of deleting it permanently.
2. Trash Directory
The trash directory in Linux systems is located in a local file system. For a standard user, the path is:
And for a root user, it’s:
It’s safe to delete the trash directory. It’ll be recreated if needed.
3. How Linux Trash Works
In the trash directory, we can find two other directories. The first is the files directory which contains the actual trashed files. And the second is the info directory which contains a text file with information about the trashed file.
For example, if we trash /home/baeldung/article.txt file we’ll have:
The files/article.txt is the original file. And the info/article.txt.trashinfo contains metadata:
[Trash Info] Path=/home/baeldung/article.txt DeletionDate=2020-02-14T12:09:26
The above information is used for example during restoring trashed files.
4. Trashing Files Using gio
The most recommended way to trash a file is to use the gio library. It provides an interface to a virtual file system and gives easy access to both local and remote files with a single consistent API.
gio is a part of GLib project and can be found by default in most Linux distributions. If not we can install it manually:
$ sudo apt-get install libglib2.0-0
To trash a file using gio, we use its trash command followed by the name of a file:
$ gio trash article.txt
After that, we can list files in our trash directory:
$ gio list trash://
list is similar to ls command, so we need to provide a directory that we want to list. In our case the local file system trash is defined as trash://.
As a result, we’ll get the list of all the files in the trashcan. In our case, it’s just one file:
To empty the trash directory we should use the trash command with an –empty parameter:
$ gio trash --empty
There’s no output if emptying the trash succeeds.
5. Trashing Files Using trash-cli
One of the alternatives to gio is trash-cli.
It’s an open-source project providing a command-line interface into the KDE, GNOME, and XFCE GUI trashcans. By using trash-cli, we can delete and restore items as the way we do in the graphical Trashcan.
First, we should install the trash-cli:
$ sudo apt-get install trash-cli
Now, to trash a file we should use the trash-put command:
$ trash-put article.txt
We can also list the content of the trashcan:
The output will contain all trashed files with trash date and full path:
2020-02-24 17:19:07 /home/baeldung/article.txt
In addition to gio, the trash-cli can restore already trashed file:
$ trash-restore article.txt
And finally, to empty the trash directory we should use the trash-empty command:
Similarly to gio, there will be no output if emptying the trash succeeds.
Note that neither trash-cli nor gio will automatically empty out the trash. But we can do that with autotrash, a separate utility.
In this short tutorial we’ve learned how to soft-delete (trash) files in Linux systems.