35

Possible Duplicate:
UNDO LINUX Trash Command

Hi,

Is there any simple way to undo an rm command?

The question is purely theoretical; I have NEVER deleted the log of a benchmark queue who took a whole lunchtime to run.

2
40

On ubuntu or similar:

$ sudo apt-get install trash-cli
$ alias rm=trash

Then put that alias in .bashrc or the appropriate login script for your shell of choice.

The trash-cli package is a command-line interface to the same trash can that GNOME and KDE and other use. So anything you delete via the trash command can be restored by GNOME/KDE and vice-versa.

The other commands in the trash-cli package are trash-list, trash-empty, and restore-trash.

9
  • 13
    +1 for trash-cli, -1 for aliasing rm.
    – Roger Pate
    Feb 15 '10 at 14:48
  • 1
    What's wrong with aliasing rm? I like having an undo button for my accidental deletions, even on the command line. Also, trash-cli explicitly facilitates this by accepting (and ignoring) many GNU rm options. Besides, the OP asked for a way to undo rm. This solution doesn't answer the question correctly without the alias. Feb 19 '10 at 23:21
  • 20
    @Ryan: Because you get used to rm to meaning something different than what the rm command really does, and then you make a mistake when it isn't aliased. Get used to using the trash command instead and that can't happen.
    – Roger Pate
    Mar 21 '10 at 6:02
  • 6
    @Roger: Or...you can alias rm and then hypnotize yourself into forgetting that you did. That way you can still keep using rm as though it's final.... but if you slip up and go "oh noes!!" you're not completely screwed. (i.e. don't rely on it's "recoverable" functionality)
    – mpen
    Jun 23 '10 at 18:36
  • 2
    Just for completeness, it is possible to recover, if you've just deleted the file and it wasn't already overwritten in disk. I've done it before a while back using low level tool(s), but honestly I forgot. There's probably an answer with the details somewhere in stack exchange. But for the future, using trash is the way. I use an alias reminder instead, e.g.: alias rm='echo "tip: use trash next time"; rm', that way I retain functionality and the choice.
    – Nagev
    Feb 15 at 23:02
16

The traditional answer is:

You recover the file from the latest backup. You do have a recent backup, don't you?

because on many unix filesystems this simple isn't possible, or is very difficult.

As others have noted this is not the end-all and be-all of the issue any more, but not making mistakes of this kind is still the preferred approach.

5
  • 5
    I really intend to put all my scripts on a hourly-committed bzr repository.
    – Adam Matan
    Aug 30 '09 at 14:49
  • 3
    The modern answer is: Recover the file from a recent filesystem snapshot. :-)
    – knweiss
    Aug 30 '09 at 14:57
  • Thats a cool question... I usually don't make backups of ~/Downloads... and wiped out my content by passing rm -fr *(1)* ... every file wen away. So having backups is not always the solution. Trash-cli sounds cools.
    – m3nda
    Jan 11 '16 at 21:03
  • No, having backups is ALWAYS the solution. Just because you chose not to backup something, doesn't make backing up not the solution, it makes you silly, for not backing up something. Jun 6 '17 at 14:57
  • "You do have a recent backup, don't you?" "[...], but not making mistakes of this kind is still the preferred approach." - Wow, great level of arrogance which is absolutely no help to the question at hand.
    – Markus
    Apr 6 '20 at 0:22
4

To prevent hypopthetical future mistakes, you might want to alias rm to rm -i...

3
  • 7
    I generally think that this would never prevent any errors (the 'y' is typed quite automatically), but consume a lot of time, and encourage the use of He-Whose-Name-Must-Not-Be-Mentioned rm -rf.
    – Adam Matan
    Aug 30 '09 at 14:48
  • It does give you one extra go though before messing things up. I generally just ls with the same parameters before so I can see what's going to go. Aug 30 '09 at 15:40
  • 1
    I usually delete files on purpose, I just realize I needed those files later. So interactive wouldn't help Dec 13 '15 at 22:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.