Sending files to trash from the Linux terminal

Radu Zaharia

--

Photo by Kinga Lopatin on Unsplash

It’s easy to remove files from the Linux terminal: just use the almighty rm. But how do file explorers work when they send deleted files to the trash instead of actually deleting them? Well it looks like there’s a complete virtual file system going on in your Linux system, working completely transparent and seamless in file explorers. But you can use it in the terminal too once you figure out the gio command. Let’s see how it looks.

The GTK virtual file system

Photo by Markus Spiske on Unsplash

GTK has so many high level libraries sitting on top of the Linux system calls that it’s very difficult to keep track of them all. GIO is one of them. GIO is a high level library that handles files: not just from the file system, but also abstractions like trash and virtual network mounts. And it also offers a neat command line tool called gio which you can use like this:

#gio list trash://

In the command above, you can see we use a virtual location called trash://. In reality, this location is in .local/share/Trash but it has a virtual mount called trash:// to make it easier to access. So basically if you would like to move a file to trash, instead of calling rm could you could simply call mv test trash://? No. Here is the error you would get returned:

cp: cannot create regular file 'trash://': Not a directory

This shows us that trash:// is not recognized as a normal file system location. It’s a virtual file system location handled by GTK and the only way to use it is with the giocommand. So then, how do we do it? Well the gio command has a move option so we could try gio move test trash:// but this would give us:

gio: file:///home/radu/test: Operation not supported

The gio command handles the trash:// location in a special way and it won’t simply allow you to copy files there. Instead we need to use another gio option: trash:

#gio trash test

This will move the test file into the trash:// virtual folder, which as we saw earlier is located in .local/share/Trash. You can also see your file now in the file explorer in the Trash location.

Virtual network mounts

Photo by JJ Ying on Unsplash

We mentioned virtual network mounts and we just jumped straight to the trash issue, but these are also aspects of the GTK virtual file system. When you discover network mounts in your file explorer (Gnome Files or whatever you use), you have to mount these locations in your file system. Well GTK is not going to do that because mount is an administrator command which needs administrative privileges. So it will mount it in the virtual file system instead which is handled entirely with the user’s permissions.

For example I clicked on my NFS network share called radu and Gnome mounted it in the virtual file system at this special location: nfs://home-pi.local/mnt/storage/radu. So now I can run:

#gio list nfs://home-pi.local/mnt/storage/radu

And I can see the files and folders at that network location:

notes
linux setup
photos
music
documents

Again, I cannot use this location with normal system Linux calls like cpor ls. These commands don’t know anything about a GTK managed virtual file system.

So, using the above network location I can copy, move, delete or send to trash files from my network share. A network share which is not mounted in the actual file system, but in the GTK virtual file system and which does not need sudo rights or any other rights to mount. Neat.

Mounting in the virtual file system

Photo by Alexander Schimmeck on Unsplash

Gnome Files mounts network shares automatically when you click on them but you can do it yourself of course using the gio command with the mount option like this:

#gio mount nfs://home-pi.local/mnt/storage/radu

Note the lack of sudo. You will have access to this mount by using gio as we so above in the gio list example. The mount types are handled by packages starting with gvfs like the following:

gvfs-afc
gvfs-fuse
gvfs-mtp
gvfs-afp
gvfs-goa
gvfs-nfs
gvfs-smb
gvfs-gphoto

That’s just a part of what types of mounts GIO can handle. Note that if you have NFS shares and you are missing the gvfs-nfs package, Gnome Files will complain about that when you try to click on a discovered NFS share. Same for the other types.

Ok, this was something new I learned and I really wanted to share because I found it cool. I never knew where the trash is in the file system and never knew about GVFS. I stumbled upon it when creating my NFS network shares and I was missing the gvfs-nfs package but I just installed it and shrugged it off. Only to dig a bit deeper later and find a whole set of virtual locations, trash included, and a whole new tool I knew nothing about: gio.

I hope this article was of use to you too. For any questions or things that I missed, don’t forget the comment section! See you next time!

--

--

No responses yet

Write a response