122

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.

107

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
  • 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
  • 11
    According to man gvfs-trash it is deprecated in favour of gio trash, see man gio. – pbhj Sep 1 '18 at 21:14
67

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

  • 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
27

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.

  • 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
5

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
4

I like the low tech ways the best. 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 with 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.

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".)

#!/bin/bash
# move-to-trash

# Teemu Leisti 2018-07-08

# This script moves the files given as arguments to the trash directory, if they
# are not already there. It works both on (Ubuntu) desktop and server hosts.
#
# 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 functionalities of restoring a trashed file to
# its original location nor of emptying the trash directory; rather, it is an
# alternative to the 'rm' command that offers the user the peace of mind that
# they can still undo an unintended deletion before they empty the trash
# directory.
#
# To determine whether it's running on a desktop host, the script tests for the
# existence of directory ~/.local/share/Trash. In case it is, the script relies
# on the 'gio trash' command.
#
# When not running on a desktop host, there is no built-in trash directory, so
# the first invocation of the script creates one: ~/.Trash/. It will not
# overwrite an existing file in that directory; instead, in case a file given as
# an argument already exists in the custom trash directory, the script first
# appends a timestamp to the filename, with millisecond resolution, such that no
# existing file will be overwritten.
#
# 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 the trash.


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

is_desktop=0

if [[ -d ~/.local/share/Trash ]] ; 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
            move_to_abspath="$trash_dir_abspath/$file_basename"
            while [[ -e "$move_to_abspath" ]] ; do
                move_to_abspath="$trash_dir_abspath/$file_basename-"$(date '+%Y-%m-%d-at-%H:%M:%S.%3N')
            done
            # While we're reasonably sure that the file at $move_to_abspath does not exist, we shall
            # use the '-f' (force) flag in the 'mv' command anyway, to be sure that moving the file
            # to the trash directory is successful even in the extremely unlikely case that due to a
            # run condition, some other thread has created the file $move_to_abspath after the
            # execution of the while test above.
            /bin/mv -f "$file_abspath" "$move_to_abspath"
        fi
        echo "moved to trash:   $file_abspath"
    fi
done
2

Here is a 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
0

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:/"

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.