6

How to restore a file from the trash using command line?

Obviously you can use restore-trash command from the trash-cli package, but this wouldn't list all files inside the trash.

Moreover what about files moved to trash with root permission?

Those are files in trash: enter image description here

Now using restore-trash command: enter image description here

only 4 files are shown.

Maythux
  • 82,867
  • 54
  • 239
  • 271
  • 1
    Have you tried running `restore-trash` using `sudo` or checking out `sudo trash-list` are you looking for something else? – Ravexina Aug 12 '17 at 13:14
  • @Maythux so what was the problem? cause I can list all files (even deleted by root) without `sudo`, and remove them using `sudo`... – Ravexina Aug 12 '17 at 13:55
  • Could you specify how did you delete one of the files which is not in the `sudo restore-trash` output? I guess it's related to programs behaving differently and not following the standards ... – Ravexina Aug 12 '17 at 14:07
  • @Ravexina command line `rm` – Maythux Aug 12 '17 at 14:10
  • Shouldn't `rm` delete the file permanently ? use `trash` or `trash-put` or `gvfs-trash` instead of `rm`. – Ravexina Aug 12 '17 at 14:15
  • [This trash-cli](https://github.com/andreafrancia/trash-cli) gave me `restore-trash`: `restore-trash: command not found`. As [the highest answer](https://askubuntu.com/a/1166008/830570) points out, this is because is called `trash-restore` since 2019. – Cadoiz May 16 '23 at 07:02

3 Answers3

5

If you run trash-restore from within a directory, It will only show the trashed files and directories that were trashed from the current directory or its sub-directories


As it appears, you are running trash-restore from your home directory. So, it is only showing files and directories that were trashed from your home directory or its sub-directories.


To restore files trashed from another directory, for example /var, you would need to cd to the /var directory first and then run trash-restore from there.


Alternatively, you can specify the system root directory / after trash-restore like so trash-restore / to make it show all trashed files and directories originating from anywhere in the system.


Notice:

The tool is called trash-restore since 2019 (instead of restore-trash previously) under the latest trash-cli package.

Cadoiz
  • 268
  • 1
  • 9
Raffa
  • 24,905
  • 3
  • 35
  • 79
3

For listing all files in the trash,

ls -l .local/share/Trash/files/

sudo ls -l /root/.local//share/Trash/files/

should do under Ubuntu (in my case: MATE 16.04). I found those paths here. It also has further explanations of what is in the other folders next to files/. In case you want to code your own restore routine, that should help...

Not sure what you are up to (writing tests to verify delete-to-trash?, ...) but looking at the specific source of the trash npm module (js) might give further insight.


UPDATE: actually, finding the right location of a trashed file can get a whole lot more complicated, e.g. when it's not a delete from /home/someuser/... but other places on same partition (say /usr/share/...) or elsewhere (mounted drives, incl. removables...)

This should be the spec to read: https://specifications.freedesktop.org/trash-spec/trashspec-1.0.html

The trash package mentioned above relies on xdg-trashdir to find the right path.

Frank N
  • 1,280
  • 2
  • 21
  • 43
  • Why is it `..../local//share/...` (with double `//`), not `..../local/share/...` (with single `/`)? Or is that just a typo? – Cadoiz May 16 '23 at 07:04
2

Each volume has it's own trash. While nautilus or nemo shows the trash combined for all mounted volumes, restore-trash shows only the files from the volume you're currently in, unlike trash-list, which shows the trash for all mounted volumes.

pLumo
  • 26,204
  • 2
  • 57
  • 87