The previous answers mention commands trash-cli
and rmtrash
. Neither of those are found by default on Ubuntu 18.04, but the command gio
is. Commanding gio help trash
outputs:
Usage:
gio trash [OPTION…] [LOCATION...]
Move files or directories to the trash.
Options:
-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 -f
option.)
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.
I added alias tt='gio trash'
to my alias definitions file; tt
is a mnemonic for "to trash".
Added on edit on 2018-06-27: On server machines, there is no equivalent of a trash directory. I've written the following Bash script that does the job; on desktop machines, it uses gio trash
, and on other machines, moves the file(s) given as parameter(s) to a trash directory it creates. The script is tested to work; I use it all the time myself. Script updated on 2024-03-24.
#!/bin/bash
move_to_trash
Teemu Leisti 2024-03-24
USAGE:
move_to_trash moves the file(s) given as argument(s) to the trash directory,
if they are not already there. It works both on (Gnome) desktop and server
hosts.
RATIONALE:
move_to_trash is intended as a command-line equivalent of deleting a file or
directory from a graphical file manager, which, on Gnome desktop hosts,
moves the deleted file(s) to a built-in trash directory.
On a server host, the script uses a custom trash directory (~/.Trash/), and
the analogy of moving a file to trash 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, thereby giving the user the peace of mind
that they can still undo an unintended deletion before emptying the custom
trash directory.
On a server host, the user will have to perform an undo by commanding 'mv'
on the moved file, and to empty the custom trash directory by commanding:
rm -rf ~/.Trash/* ~/.Trash/.*
IMPLEMENTATION:
To determine whether it's running on a desktop host, move_to_trash tests for
the existence of the gio command and of directory ~/.local/share/Trash. (The
'gio' command only exists for Gnome.) In case both exist, the script relies
on calling 'gio trash'. Otherwise, it treats the host as a server.
On server hosts, there is no built-in trash directory, so move_to_trash
creates directory ~/.Trash/, unless it already exists. (The script errors
out if there is an existing file ~/.Trash.)
move_to_trash appends a millisecond-resolution timestamp to all the files it
moves to the trash directory, to inform the user of the time of the deletion
and to avoid overwrites.
move_to_trash will not choke on a nonexistent file. It outputs the final
disposition of each filename argument: does not exist, was already in trash,
or was moved to trash.
COPYRIGHT WAIVER:
The author dedicates this Bash script to the public domain by waiving all of
their rights to the work worldwide under copyright law, including all
related and neighboring rights, to the extent allowed by law. You can copy,
modify, distribute, and perform the script, even for commercial purposes,
all without asking for permission.
is_desktop=0
gnome_trash_directory=$(realpath ~/.local/share/Trash/)
gio_command_exists=0
if command -v gio > /dev/null 2>&1 ; then
gio_command_exists=1
fi
if [[ -d "$gnome_trash_directory" ]] && (( gio_command_exists == 1 )) ; then
# Executing on a Gnome desktop.
is_desktop=1
trash_dir_abspath="$gnome_trash_directory"
else
# Not executing on a Gnome desktop, so attempt to use a custom trash
# directory.
trash_dir_abspath=$(realpath ~/.Trash)
if [[ -e "$trash_dir_abspath" ]] ; then
if [[ ! -d "$trash_dir_abspath" ]] ; then
echo "Error: $trash_dir_abspath exists, but is not a directory."
exit 1
fi
else
mkdir "$trash_dir_abspath"
echo "Created directory $trash_dir_abspath"
fi
fi
Deal with all filenames given as arguments. (The concept "filename" covers
both file and directory names here.)
for file in "$@" ; do
file_current_abspath=$(realpath -- "$file")
file_basename=$(basename -- "$file_current_abspath")
if [[ ! -e $file_current_abspath ]] ; then
echo "does not exist: $file_current_abspath"
elif [[ "$file_current_abspath" == "$trash_dir_abspath"* ]] ; then
echo "already in trash: $file_current_abspath"
else
# $file_current_abspath exists and is not yet in the trash directory, so
# move it there.
if (( is_desktop == 1 )) ; then
# We're executing on a Gnome desktop, so just use gio.
gio trash "$file_current_abspath"
else
# We're not executing on a Gnome desktop, so move the file to the
# custom trash directory, with a new name that appends a
# millisecond-resolution timestamp to the original.
name_head="$trash_dir_abspath/$file_basename"DELETED_ON
file_new_abspath="$name_head$(date '+%Y-%m-%d_AT_%H-%M-%S.%3N')"
while [[ -e "$file_new_abspath" ]] ; do
# Generate a new name with a new timestamp, as the previously
# generated one denoted an existing file or directory. It's
# quite unlikely we'll have to execute this even once.
file_new_abspath="$name_head$(date '+%Y-%m-%d_AT_%H-%M-%S.%3N')"
done
# There is no file or directory named $file_new_abspath, so we can
# use it as the move target.
/bin/mv "$file_current_abspath" "$file_new_abspath"
fi
echo "moved to trash: $file_current_abspath"
fi
done
gvfs-trash
in the past, but never had a need to restore from the command-line until you sparked my curiosity. The answer to the linked question may be of help. – ephsmith Jul 10 '12 at 23:02mv test .local/share/Trash/files
. If you take this habit, you will avoid accidental removal of files. For example, this would have prevented me from removing accidentally a part of my home folder because I wanted to remove a folder named ~ created by mistake... I think this solution is safer than tweaking the rm command because one day, you may use the rm command on a system with the default rm command. The mv -i alias will alert before you overwrite existing files. – baptx May 01 '20 at 15:48rm
and, yes, I occasionally delete the wrong files by accident. Thanks to automatic backups this usually hasn’t been an issue (there are rare exceptions when working on scratch space, but by definition these files aren’t important … at least, ideally). – Konrad Rudolph Aug 19 '22 at 06:55