DEV Community

vast cow
vast cow

Posted on

Introduction to `gio trash`: Send Files to the Trash Instead of “Deleting” Them in the Linux CLI

When deleting files from the Linux command line, many people instinctively use rm. However, rm generally performs a permanent deletion without using the Trash, making it unforgiving of mistakes.

In GNOME/GLib-based environments, you can use gio trash to move files and directories to the Trash, just like a GUI file manager does. gio trash is a subcommand of gio and also supports listing Trash contents, restoring items, and emptying the Trash. Ubuntu and Arch Linux man pages document options such as gio trash --list, --restore, and --empty. (Ubuntu Manpages)


What Is gio trash?

gio is a command-line utility provided by GLib/GIO. It can operate not only on local files but also on URIs and virtual file systems supported by GIO.

The trash subcommand is gio trash.

gio trash FILE...
Enter fullscreen mode Exit fullscreen mode

Instead of immediately deleting a file like rm FILE, it moves the target to the Trash.

This is similar to right-clicking a file in GNOME Files (formerly Nautilus) and selecting Move to Trash. GVfs is the user-space virtual filesystem implementation for GIO and provides backends for Trash, SFTP, SMB, WebDAV, and more. (wiki.gnome.org)


Basic Usage

Send a File to the Trash

gio trash memo.txt
Enter fullscreen mode Exit fullscreen mode

You can specify multiple files:

gio trash a.txt b.txt c.txt
Enter fullscreen mode Exit fullscreen mode

Directories are also supported:

gio trash old-project/
Enter fullscreen mode Exit fullscreen mode

When possible, metadata is stored so the item can later be restored to its original location.


View the Contents of the Trash

gio trash --list
Enter fullscreen mode Exit fullscreen mode

You can also list the trash:// URI directly:

gio list trash://
Enter fullscreen mode Exit fullscreen mode

The Arch Linux man page documents both gio trash --list and gio list trash:// as ways to inspect the Trash. (Arch Manual Pages)

Example:

trash:///memo.txt    /home/user/Documents/memo.txt
trash:///old-project /home/user/work/old-project
Enter fullscreen mode Exit fullscreen mode

The exact output format may vary by environment and version, but it generally shows the item's Trash URI and original location.


Restore Files from the Trash

Use --restore to restore an item:

gio trash --restore trash:///memo.txt
Enter fullscreen mode Exit fullscreen mode

The important detail is that restoration expects a trash:// URI, not a normal filesystem path.

The Ubuntu man page explains that --restore restores an item to its original location and expects a trash:// URI. If the original directory no longer exists, it will be recreated. (Ubuntu Manpages)

A typical workflow looks like this:

gio trash --list
gio trash --restore trash:///memo.txt
Enter fullscreen mode Exit fullscreen mode

Empty the Trash

To permanently empty the Trash:

gio trash --empty
Enter fullscreen mode Exit fullscreen mode

Use caution. While gio trash is useful for safer deletion, --empty is effectively the final deletion step.


Ignore Missing Files

Use -f or --force to ignore files that do not exist or cannot be trashed:

gio trash -f maybe-exists.txt
Enter fullscreen mode Exit fullscreen mode

The Arch Linux man page describes -f, --force as an option that ignores nonexistent files and files that cannot be moved to the Trash. (Arch Manual Pages)


Difference Between rm and gio trash

Command Behavior Recoverable
rm file Deletes the file Usually difficult
gio trash file Moves the file to the Trash Yes
gio trash --empty Empties the Trash Usually difficult

rm is powerful for scripts and temporary-file cleanup, but it can be risky for manual operations. In situations like the following, gio trash is often the safer choice:

gio trash ~/Downloads/*
gio trash old-report.pdf
gio trash tmp-output/
Enter fullscreen mode Exit fullscreen mode

That said, gio trash is only a Trash mechanism—it is not a substitute for backups.


Relationship to gvfs-trash

Older articles sometimes reference the gvfs-trash command:

gvfs-trash file.txt
Enter fullscreen mode Exit fullscreen mode

Today, gio trash is generally the preferred approach.

Ubuntu-related documentation and discussions often note that gvfs-trash is deprecated and recommend using gio trash instead. (Ask Ubuntu)

For new setups, use gio trash rather than gvfs-trash.


Difference Between gio trash and trash-cli

Another popular Trash utility for Linux is trash-cli:

trash-put file.txt
trash-list
trash-restore
trash-empty
Enter fullscreen mode Exit fullscreen mode

A practical comparison:

Use Case Recommended Tool
Use standard GNOME/GLib functionality gio trash
Want a desktop-environment-independent CLI tool trash-cli
Prefer interactive restoration workflows trash-cli
Want something lightweight that already exists on the system gio trash

On GNOME desktops or systems with GLib/GVfs installed, gio trash is often available without additional packages. On servers or minimal installations, it may not be installed.


Shell Alias Examples

To make it easier to use instead of rm, create aliases:

alias del='gio trash'
alias trash='gio trash'
Enter fullscreen mode Exit fullscreen mode

Add them to ~/.bashrc or ~/.zshrc:

echo "alias del='gio trash'" >> ~/.bashrc
source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Then use:

del old.txt
del old-directory/
Enter fullscreen mode Exit fullscreen mode

It is technically possible to replace rm itself:

alias rm='gio trash'
Enter fullscreen mode Exit fullscreen mode

However, this is generally not recommended. It can break assumptions in scripts, system administration tasks, and shared environments. A separate alias such as del or trash is usually the safer choice.


Caveats

1. Not All Filesystems Behave the Same Way

The Trash location can vary depending on where the file resides.

Red Hat documentation explains that files under a user's home directory typically use $XDG_DATA_HOME/Trash, but Trash locations may differ depending on the filesystem, and not all filesystems support the Trash concept. (Red Hat Documentation)

2. Avoid sudo gio trash

Running gio trash as root may cause files to be processed using the root user's environment and Trash location rather than your own.

For normal files, use:

gio trash file.txt
Enter fullscreen mode Exit fullscreen mode

If you truly need to remove root-owned files, administrators often prefer a clearly defined deletion policy and tools such as sudo rm rather than a Trash-based workflow.

3. Restoring Requires a trash:// URI

Commands like this may fail:

gio trash --restore /home/user/Documents/memo.txt
Enter fullscreen mode Exit fullscreen mode

Instead, identify the item with gio trash --list and restore it using its trash:// URI:

gio trash --restore trash:///memo.txt
Enter fullscreen mode Exit fullscreen mode

Practical Examples

Move Unwanted ZIP Files in Downloads to the Trash

gio trash ~/Downloads/*.zip
Enter fullscreen mode Exit fullscreen mode

Move All .log Files in the Current Directory to the Trash

gio trash ./*.log
Enter fullscreen mode Exit fullscreen mode

Review the Trash Before Emptying It

gio trash --list
gio trash --empty
Enter fullscreen mode Exit fullscreen mode

Configure Deletion Aliases

cat >> ~/.bashrc <<'EOF'
alias del='gio trash'
alias trash='gio trash'
EOF

source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Summary

gio trash is a practical command for safely sending files to the Trash from the Linux command line.

gio trash file.txt
gio trash --list
gio trash --restore trash:///file.txt
gio trash --empty
Enter fullscreen mode Exit fullscreen mode

rm performs immediate deletion, whereas gio trash moves items to the Trash. Simply understanding and using that distinction can significantly reduce the risk of accidental data loss during CLI work.

For day-to-day manual file management, gio trash is often the better choice. Use rm when you explicitly intend permanent deletion.

Top comments (0)