7

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
  • 84,289

3 Answers3

6

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
  • 278
Raffa
  • 32,237
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,340
  • 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,947