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".)
# 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
# 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.)
if [[ -d ~/.local/share/Trash ]] ; then
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."
echo "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
# 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"
echo "moved to trash: $file_abspath"