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.

Adam Matan
  • 8,090
  • 17
  • 59
  • 84
  • 1
    dupe: http://superuser.com/questions/31171/undo-in-command-line-operation –  Aug 30 '09 at 12:50
  • 1
    ...or at least very closely related, even if the question itself is formulated to be a little more general – Jonik Aug 30 '09 at 12:52

3 Answers3

41

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.

MarAja
  • 321
  • 1
  • 5
  • 15
Ryan C. Thompson
  • 11,563
  • 9
  • 47
  • 69
  • 13
    +1 for trash-cli, -1 for aliasing rm. –  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 C. Thompson Feb 19 '10 at 23:21
  • 22
    @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. –  Mar 21 '10 at 06:02
  • 7
    @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
  • Hopefully you'll have an answer to that soon enough: http://superuser.com/questions/192725/how-can-i-alias-a-command-for-sudo – Ryan C. Thompson Sep 25 '10 at 18:06
  • @Roger you could consider set up an alias to enable undo for 3rd party scripts. – Lucas Jan 31 '14 at 22:28
  • In support of not aliasing `rm`: https://apple.stackexchange.com/a/17637/37190 – 0 _ Jun 15 '17 at 07:03
  • 3
    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 '21 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
    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. – djsmiley2kStaysInside Jun 06 '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 06 '20 at 00:22
4

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

Cadoiz
  • 519
  • 3
  • 10
Rich Bradshaw
  • 6,416
  • 3
  • 31
  • 43
  • 8
    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
  • 1
    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