How to delete to trash via terminal (with mv command)? [duplicate]
I would like to know how can I delete a file and/or move it to trash with mv
command?
I know where the trash located is, then I tried with this commands to move it to trash:
mv somefile /home/username/.local/share/Trash
this command work fine, but when I open trash in launcher it's still show empty. I can undo this deleted file in original location with:
mv /home/username/.local/share/Trash/somefile ~/
but if this location /home/username/.local/share/Trash
isn't for Trash then where is moved my file?
I know rm
will permanently delete file and not send it to a trash folder and there is no flag in man rm
to delete file to trash.
You can use gvfs-trash
instead of mv
gvfs-trash somefile
The reason you were unable to see your file after moving it to ~/.local/share/Trash is that there is an additional directory structure below that i.e.
-
~/.local/share/Trash/files
to contain the actual trashed file; and -
~/.local/share/Trash/info
containing metadata such as the original location
Although you could have used
mv somefile ~/.local/share/Trash/files
in which case somefile
would be visible in the trash can, it would not be possible to use the nautilus/gvfs Restore
function to undelete the file, due to the lack of info
metadata - you would need to know and manually mv
the file to its original location. In that sense, only gvfs-trash
is the exact command-line equivalent to trashing a file via nautilus.
Information about Trash directory:
~/.local/share/Trash
contains two directories named info
and files
.
-
~/.local/share/Trash/files
contains original files. -
~/.local/share/Trash/info
contains files (extension :.trashinfo
)which have information about path and deletion date of files.
Therefore, if you want to use mv
command then follow command:
mv somefile ~/.local/share/Trash/files
But it is not recommended to use above method to move files to trash.(as commented here)
You can install trash command line i.e. trash-cli
by following command:
sudo apt-get install trash-cli
And here are available commands for trash:
$ apropos trash
gvfs-trash (1) - Move files or directories to the trash
restore-trash (1) - Restore for Command line trash utility.
trash (1) - Command line trash utility.
trash-empty (1) - Empty for Command line trash utility.
trash-list (1) - List trashed files.
trash-put (1) - Command line trash utility.
You can move/put files to trash by following command:
trash-put somefiles
- or
gvfs-trash somefiles
List trash by command: trash-list
Restore files by command: restore-trash
Empty trash by command: trash-empty
Also visit this Question.