171

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.

Dawoodjee
  • 681
Rasmus
  • 8,655

10 Answers10

151

As of 2017, you can use gio, which supports several operations, including trashing files.

gio trash filename
gio trash *.ext
gio trash filename1 filename2

From the manual:

Usage:
  gio trash [OPTION…] [LOCATION…]

Move/Restore files or directories to the trash.

Options: -f, --force Ignore nonexistent files, never prompt --empty Empty the trash --list List files in the trash with their original locations --restore Restore a file from trash to its original location (possibly recreating the directory)

Note: for --restore switch, if the original location of the trashed file already exists, it will not be overwritten unless --force is set.

You used to be able to use gvfs-trash command from the package gvfs-bin. This is still installed by default in Ubuntu, but has been deprecated.

Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407
85

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

user55822
  • 3,245
56

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.

Eugene
  • 684
18

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

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.

Sntn
  • 115
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.

4

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
Frank N
  • 1,430
1

The best solution is to use gio trash as gvfs-trash is deprecated.

  1. Open the terminal and type the below command:

    gedit ~/.bashrc
    
  2. This command will open your .bashrc file, which is the configuration file for your terminal. If you are using any other terminal open their rc file (e.g. zsh terminal will be like .zshrc).

  3. Paste the below command at the end of your opened file:

    alias rm='gio trash'
    
  4. Now just save and exit the file.

  5. Restart your terminal and Boom, you have successfully changed rm from permanently deleting a file to deleting the file to trash!!

1

Gnome vs KDE

gio trash uses gvfs drivers which are specific to Gnome.

KDE uses KIO drivers instead. On KDE kioclient move 'url' trash:/ commands is available (ktrash6 and ktrash5 are internal helpers and not the actual commands).

command driver DE
gio trash 'url' gvfs Gnome
kioclient move 'url' trash:/ kio KDE

Locations

  • ~/.local/share/Trash which is $XDG_DATA_HOME/Trash Freedesktop standardized value if it something removed from your home directory
  • /<mountpoint>/.Trash-<uid> e.g. /tmp/.Trash-1000 if it's a mounted partition (more rarely /<mountpoint>/.Trash/<uid> when .Trash as the sticky bit set)

Common utilities

Send2Trash

Send2Trash python package provides a binary send2trash that works on all systems.

  • OS X: native FSMoveObjectToTrashSync Cocoa calls or can use pyobjc with NSFileManager
  • Windows: native IFileOperation call if on Vista or newer and pywin32 is installed or falls back to SHFileOperation calls
  • Other platforms (Linux, BSD, etc.): PyGObject and GIO if available else fallbacks to the trash specifications from freedesktop.org.

trash-cli

trash-cli python package provides binaries: trash, trash-empty, trash-list, trash-put, trash-restore, trash-rm. It should respect Freedesktop spec and be compatible with KDE, GNOME, and XFCE.

noraj
  • 831