139

I would like to know if there is a command I can issue in a terminal so I do not classically remove (rm) the file, but instead move it to trash (i.e. Nautilus Move to Trash behavior).

In case there is such a command, I would also be interested in knowing what it is.

2
121

You can use gvfs-trash command from the package gvfs-bin which is installed by default in Ubuntu.

Move file to trash:

gvfs-trash filename

See the content of the trash:

gvfs-ls trash://

Empty the trash:

gvfs-trash --empty
3
  • Also visit my gvfs-question. – Pandya Sep 17 '14 at 14:47
  • This is the simplest answer for me that works. Thank you. – Teody C. Seguin Aug 2 '17 at 10:55
  • 27
    According to man gvfs-trash it is deprecated in favour of gio trash, see man gio. – pbhj Sep 1 '18 at 21:14
78

Install trash-cli Install trash-clisudo apt-get install trash-cli

Put files in the trash with: trash file1 file2

List files in trash: trash-list

Empty trash with: trash-empty

2
  • 1
    That (Ubuntu-related) tool points forward to a trash spec. Pretty interesting, not sure how widely adopted, though... – Frank Nocke Aug 15 '17 at 17:59
  • After installation, I run the command and get the error: File "/usr/bin/trash-list", line 4, in <module> ImportError: No module named 'trashcli' – Daniel Aug 1 '18 at 8:48
39

As of 2017, gvfs-trash seems to be deprecated.

$ touch test
$ gvfs-trash test
This tool has been deprecated, use 'gio trash' instead.
See 'gio help trash' for more info.

You should use gio, specifically

gio trash

is the recommended way.

5
  • 2
    Could you link a source for gvfs-trash being deprecated and what gio is? – Melebius Dec 11 '17 at 9:04
  • 1
    I can't provide a link unfortunately, but this is what I get trying to use gvfs-trash on Kubuntu 17.10: pastebin.com/HA4a1pbs – Eugen Tverdokhleb Jan 10 '18 at 19:56
  • 1
    You could paste the example here in your answer, it would be sufficient for me together with system version number. I am using 16.04 LTS and gvfs-trash is the only option here. – Melebius Jan 10 '18 at 20:50
  • This tool has a bunch of other nice features. I like the info command; it seems useful. – Raffi Khatchadourian Aug 2 '18 at 18:35
  • @Melebius On Bionic Beaver (18.04), gvfs-trash(1) says that it has been deprecated. On Focal Fossa (20.04) even the package description says that it's deprecated. – Franklin Yu Feb 25 '20 at 2:12
13

Updating @Radu Rădeanu answer. Since Ubuntu is telling me to use gio instead...

So, to trash some_file (or folder) use

gio trash some_file

To go dumpster diving use

gio list trash://

To empty trash

gio trash --empty
3
  • gio list trash:// gio: trash://: Operation not supported – Qinsi Sep 28 '20 at 6:08
  • It's missing one slash, it should be gio list trash:///. I tried to edit the answer but it doesn't allow me to make an edit of only one character. – user1754322 Dec 19 '20 at 10:08
  • either double or triple slash works for me on 18.04, tip, you can always add ```bash or other formatting rules if you only want to make minor character edits – CervEd Apr 5 at 9:33
5

I like the simpler ways better. I made a folder .Tr in my home directory by typing:

mkdir ~/.Tr

and instead of using rm to delete files, I move those files to the ~/.Tr directory by typing:

mv fileName ~/.Tr

This is an effective and simple way of keeping access to files you think you don't want. This has the added benefit (in my case) of not messing with the system's folders, as my Ubuntu knowledge levels are fairly low and I worry about what I might be screwing up when I mess with system stuff. If you are also low level please note that the "." in the directory name makes it a hidden directory.

1
  • The downside of using this approach is that moving files with the same name will overwrite them. When you use the gnome's trash that doesn't happen, it keeps all of them – user1754322 Dec 19 '20 at 10:13
3

A previous answer mentions the command gio trash, which is fine as far as it goes. However, on server machines, there is no equivalent of a trash directory. I've written a Bash script that does the job; on (Ubuntu) desktop machines, it uses gio trash. (I've added alias tt='move-to-trash' to my alias definitions file; tt is a mnemonic for "to trash".) The script is tested to work; I use it all the time myself. Script updated on 2020-08-10.

#!/bin/bash
#
# move-to-trash
#
# Teemu Leisti 2020-08-10
#
# This script moves the files given as arguments to the trash directory, if they
# are not already there. It works both on (Gnome) desktop and server hosts. (The
# gio command only exists for Gnome.)
#
# The script is intended as a command-line equivalent of deleting a file from a
# graphical file manager, which, in the usual case, moves the deleted file(s) to
# a built-in trash directory. On server hosts, the analogy is not perfect, as
# the script does not offer the functionality of restoring a trashed file to its
# original location, nor that of emptying the trash directory; rather, it offers
# an alternative to the 'rm' command, giving the user the peace of mind that
# they can still undo an unintended deletion before emptying the trash
# directory.
#
# To determine whether it's running on a desktop host, the script tests for the
# existence of the gio command and of directory ~/.local/share/Trash. In case
# both exist, the script relies on the 'gio trash' command. Otherwise, it treats
# the host as a server.
#
# There is no built-in trash directory on server hosts, so the script creates
# directory ~/.Trash/, unless it already exists.
#
# The script appends a millisecond-resolution time stamp to all the files it
# moves to the trash directory, both to inform the user of the time of the
# deletion, and to avoid overwrites when moving a file to trash.
#
# The script will not choke on a nonexistent file. It outputs the final
# disposition of each argument: does not exist, was already in trash, or was
# moved to trash.


gio_command_exists=0
command -v gio > /dev/null 2>&1
if (( $? == 0 )) ; then
    gio_command_exists=1
fi

# Exit on using an uninitialized variable, and on a command returning an error.
# (The latter setting necessitates appending " || true" to those arithmetic
# calculations and other commands that can return 0, lest the shell interpret
# the result as signalling an error.)
set -eu

is_desktop=0

if [[ -d ~/.local/share/Trash ]] && (( gio_command_exists == 1 )) ; then
    is_desktop=1
    trash_dir_abspath=$(realpath ~/.local/share/Trash)
else
    trash_dir_abspath=$(realpath ~/.Trash)
    if [[ -e $trash_dir_abspath ]] ; then
        if [[ ! -d $trash_dir_abspath ]] ; then
            echo "The file $trash_dir_abspath exists, but is not a directory. Exiting."
            exit 1
        fi
    else
        mkdir $trash_dir_abspath
        echo "Created directory $trash_dir_abspath"
    fi
fi

for file in "$@" ; do
    file_abspath=$(realpath -- "$file")
    file_basename=$(basename -- "$file_abspath")
    if [[ ! -e $file_abspath ]] ; then
        echo "does not exist:   $file_abspath"
    elif [[ "$file_abspath" == "$trash_dir_abspath"* ]] ; then
        echo "already in trash: $file_abspath"
    else
        if (( is_desktop == 1 )) ; then
            gio trash "$file_abspath" || true
        else
            # The name of the moved file shall be the original name plus a
            # millisecond-resolution timestamp.
            beginning="$trash_dir_abspath/$file_basename"_DELETED_ON_
            move_to_abspath="$beginning$(date '+%Y-%m-%d_AT_%H-%M-%S.%3N')"
            while [[ -e "$move_to_abspath" ]] ; do
                # Generate a new name with a new timestamp, as the previously
                # generated one denoted an existing file.
                move_to_abspath="$beginning$(date '+%Y-%m-%d_AT_%H-%M-%S.%3N')"
            done
            # We're now almost certain that the file denoted by name
            # $move_to_abspath does not exist. For that to be the case, an
            # extremely unlikely race condition would have had to take place:
            # some other process would have had to create a file with the name
            # $move_to_abspath after the execution of the existence test above.
            # However, to make absolute sure that moving the file to the trash
            # directory will always be successful, we shall give the '-f'
            # (force) flag to the 'mv' command.
            /bin/mv -f "$file_abspath" "$move_to_abspath"
        fi
        echo "moved to trash:   $file_abspath"
    fi
done
2

Here is an open-source nodejs-based version (if you want to know what happens under the hood, or need this in a project) that also has command line support (if you are happy if it just works).

> trash pictures/beach.jpg
1

In KDE 4.14.8 I used the following command to move files to trash (as if it were removed in Dolphin):

kioclient move path_to_file_or_directory_to_be_removed trash:/

Appendix: I found about the command with

    ktrash --help
...
    Note: to move files to the trash, do not use ktrash, but "kioclient move 'url' trash:/"

EDIT : For KDE 5.18.5, the command is kioclient5, the syntax is identical.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.