5

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.

Volker Siegel
  • 13,065
  • 5
  • 49
  • 65
αғsнιη
  • 35,660
  • 1
    Also see this recent question+answer: http://askubuntu.com/questions/213533/command-to-move-a-file-to-trash-via-terminal – v2r Sep 14 '14 at 14:45
  • 1
    I missed out on that one! Thanks for mentioning it! – v2r Sep 14 '14 at 15:11
  • 4
    If you will use mv to move a file to trash, you will never enjoy the option to restore that file from the trash! From here my downvote (sorry). – Radu Rădeanu Sep 17 '14 at 06:00
  • 2
    Ok, then what is your reason to put things in the trash? Just to have things there? Perhaps you do not understand the point of the trash. A novice user can take your idea as a good advice and this is not desirable. – Radu Rădeanu Sep 17 '14 at 06:30
  • 1
    @KasiyA It's still not right. How you will reverse after you used mv command twice for two different files from different directories, but with the same name? Not to say that you have to remember all the time which was the previous file location. – Radu Rădeanu Sep 17 '14 at 07:11
  • 1
    @KasiyA You have steeldriver's answer. See man gvfs-trash for more info. – Radu Rădeanu Sep 17 '14 at 07:17
  • 4
    @KasiyA Another reason for downvote: your terminology is not correct: You can delete something from ..., but not to .... When you delete something from one location and then you see that thing at another location implies an action which is called move (not delete). – Radu Rădeanu Sep 17 '14 at 07:30
  • 1
    @KasiyA Looking for what?!?... You are looking in the wrong direction. I told you: the answer is there and has explained why mv is bad in this case. – Radu Rădeanu Sep 17 '14 at 07:42
  • 1
    @KasiyA The only reason: mv doesn't has this property: to remember from where a file was moved (only you or history can do this). – Radu Rădeanu Sep 17 '14 at 07:53
  • 1
    @KasiyA I revised/improved my answer – Pandya Sep 17 '14 at 14:31

2 Answers2

13

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.

steeldriver
  • 136,215
  • 21
  • 243
  • 336
11

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:

  1. trash-put somefiles
  2. 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.

Pandya
  • 35,771
  • 44
  • 128
  • 188