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.
You can use
gvfs-trash
command from the packagegvfs-bin
which is installed by default in Ubuntu.Move file to trash:
See the content of the trash:
Empty the trash:
Install trash-cli –
sudo 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
As of 2017,
gvfs-trash
seems to be deprecated.You should use
gio
, specificallyis the recommended way.
Updating
@Radu Rădeanu
answer. Since Ubuntu is telling me to usegio
instead...So, to trash
some_file
(or folder) useTo go dumpster diving use
To empty trash
I like the simpler ways better. I made a folder
.Tr
in my home directory by typing:and instead of using
rm
to delete files, I move those files to the~/.Tr
directory by typing: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.
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.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).
In KDE 4.14.8 I used the following command to move files to trash (as if it were removed in Dolphin):
Appendix: I found about the command with
EDIT : For KDE 5.18.5, the command is
kioclient5
, the syntax is identical.The best solution is to use
gio trash
asgvfs-trash
is deprecated.Open the terminal and type the below command:
This command will open your
.bashrc
file, which is the configuration file for your terminal. If you are using any other terminal open theirrc
file (e.g.zsh
terminal will be like.zshrc
).Paste the below command at the end of your opened file:
Now just save and exit the file.
Restart your terminal and Boom, you have successfully changed
rm
from permanently deleting a file to deleting the file to trash!!