7

I have mistakenly deleted a file via while connected ssh. How I can recover that file or, if ubuntu has something like trash bin or so ?

2
  • I edited the question to give more focus to trash aspect, to make clear it's not a full duplicate of the linked. (Otherwise it would be closed as duplicate.) Oct 29, 2014 at 13:41
  • See also: askubuntu.com/questions/6698/…
    – Seth
    Nov 6, 2014 at 4:24

2 Answers 2

13

rm versus deleting from GUI

If you used the command rm, you did indeed delete the file. The data is no longer reachable from the file system - except there was another hard link to the file (unusual), or you deleted a symbolic link.

The actual data may be still on the disk, on disk blocks the filesystem assumes to be unused. It you want to recover it, you should not write to the disk, because it could reuse the disk space holding you data. You could try regular data recovery on the file with the tools suitable for the file system you used.

For the default ext4 filesystem, see: Is there any recovery software available for ext4? and most answers on the question linked below.

Trash for the CLI

It is possible to use the trash bin from the command line, but that's not set up by default. The package trash-cli contains commandline tools to use the trash bin of your desktop environment.

There are commands like trash-put, trash-list etc;
The names are pretty long - you could define shell aliases like

alias tp='trash-put'

or

alias del='trash-put'

and then use del instead of rm to delete files or directories in a more safe way.

As it is proposed in the answer linked below, it's possible to use the alias name rm, replacing the actual rm command with trash-put, but there are multiple reasons not to do that; some of them are:

  • The implementation of trash-put is much less well-tested that rm from 'GNU coreutils' in Ubuntu, for example.
  • The options of the default rm are not all supported by trash-put
  • Someone else will use the shell with the alias sooner or later, and will see a very broken rm command, technically.
  • A special syntax is needed when the real rm is required
  • Generally, it's not a good idea to mess with a very dangerous tool, at least risking confusion.

Having said that, see the answer of @enzotib on Can files/directories deleted with rm be restored? on how to configure the alias.

3
  • 2
    Probably not a good idea to alias basic commands like rm (it seems like it could encourage bad habits) -- better to use something like alias del='trash-put', I think.
    – evilsoup
    Oct 29, 2014 at 17:05
  • 1
    @evilsoup I agree... except that this does not solve the problem of making rm more save, really. The alias alias del='trash-put' would be nothing else than a shorter name for a long command name, right? Could be tp just the same, not related to make rm more save. - But the important part in what you say is: Don't show bad patterns in examples; very true, and reason enough to link to the near-duplicates, but discourage the alias, proposed there too. Oct 29, 2014 at 18:33
  • @evilsoup As you see, I agree ;) I found more arguments against it than I expected. Thank you for making me think about it. Oct 29, 2014 at 19:04
3

There's a special case where you can get a deleted file back - when it's still open in some program. You just have to figure out what program is using it.

I saved this answer by Patrick from Unix and Linux Stack Exchange, but not a link to the original.

Recovering a deleted file if the file is still open in a program.

If the file is still open, you can get it back by going to /proc/PID/fd where PID is the pid of a program that has the file still open. This directory contains all the open file descriptors of the program, and you can access them just like normal files, so you could copy the file off /proc. – Patrick Dec 18 at 4:44 and – camh Dec 19 at 7:12 - Unix and Linux stackexchange

1
  • Very good point! Even if it does not apply often - it would be really good to not miss it when it does. Oct 30, 2014 at 2:06

Not the answer you're looking for? Browse other questions tagged .