The previous answers mention commands
rmtrash. Neither of those are found by default on Ubuntu 18.04, but the command
gio is. Commanding
gio help trash outputs:
gio trash [OPTION…] [LOCATION...]
Move files or directories to the trash.
-f, --force Ignore nonexistent files, never prompt
--empty Empty the trash
I tested using
gio trash FILENAME on the command line, and it works just like I'd selected the file in the file browser and clicked the DEL button: the file is moved to the desktop's Trash folder. (The command doesn't prompt for confirmation even though I did not use the
Deleting files this way is reversible, while being more convenient than redefining
rm to be
rm -i for safety and having to confirm each deletion, which still leaves you out of luck if you accidentally confirm a deletion you shouldn't have.
alias tt='gio trash' to my alias definitions file;
tt is a mnemonic for To Trash.
Added on edit on 2018-06-27 and updated on 2019-05-17: On server machines, there is no equivalent of a trash directory. I've written a Bash script that does the job; on desktop machines, it uses
# Teemu Leisti 2019-05-17
# 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 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 is an
# alternative to the 'rm' command, offering the user the peace of mind that they
# can still undo an unintended deletion before the trash directory is emptied.
# To determine whether it's running on a desktop host, the script tests for the
# existence of the gio utility 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 first invocation
# of the script creates directory ~/.Trash/ (unless a previous invocation has
# already created it).
# The script will append a millisecond-resolution time stamp to all files it
# moved 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.
command -v gio > /dev/null 2>&1
if (( $? == 0 )) ; then
# 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.)
if [[ -d ~/.local/share/Trash ]] && (( gio_command_exists == 1 )) ; then
if [[ -e $trash_dir_abspath ]] ; then
if [[ ! -d $trash_dir_abspath ]] ; then
echo "ERROR: File $trash_dir_abspath exists, but is not a directory. Exiting."
echo "INFO: Created directory $trash_dir_abspath"
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"
if (( is_desktop == 1 )) ; then
gio trash "$file_abspath" || true
while [[ -e "$move_to_abspath" ]] ; do
# We're now almost certain that the file at $move_to_abspath does
# not exist, as for that to be the case, an extremely unlikely run
# condition would have had taken 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'
/bin/mv -f "$file_abspath" "$move_to_abspath"
echo "moved to trash: $file_abspath"