Possible Duplicate:
UNDO LINUX Trash Command


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.

marked as duplicate by BinaryMisfit Nov 17 '10 at 7:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


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.

  • 11
    +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. – Ryan Thompson Feb 19 '10 at 23:21
  • 15
    @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
  • 5
    @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
  • @RyanThompson: This is a nice tip, but how to configure this to work also with sudo rm? – takeshin Sep 25 '10 at 12:20

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
    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. – erm3nda 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. – djsmiley2k Jun 6 '17 at 14:57

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

  • 6
    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. – Rich Bradshaw Aug 30 '09 at 15:40
  • I usually delete files on purpose, I just realize I needed those files later. So interactive wouldn't help – andrewtweber Dec 13 '15 at 22:25

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