undo Linux's rm?

31

8

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

Posted 2009-08-30T12:09:51.560

Reputation: 5 930

Question was closed 2010-11-17T07:02:40.150

1...or at least very closely related, even if the question itself is formulated to be a little more general – Jonik – 2009-08-30T12:52:32.390

Answers

33

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.

Ryan C. Thompson

Posted 2009-08-30T12:09:51.560

Reputation: 10 085

11+1 for trash-cli, -1 for aliasing rm. – None – 2010-02-15T14:48:37.457

1What'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 – 2010-02-19T23:21:41.483

16@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. – None – 2010-03-21T06:02:30.773

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 – 2010-06-23T18:36:47.413

In support of not aliasing rm: https://apple.stackexchange.com/a/17637/37190

– Ioannis Filippidis – 2017-06-15T07:03:39.843

@RyanThompson: This is a nice tip, but how to configure this to work also with sudo rm? – takeshin – 2010-09-25T12:20:24.620

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 – 2010-09-25T18:06:40.190

@Roger you could consider set up an alias to enable undo for 3rd party scripts. – Lucas – 2014-01-31T22:28:00.027

15

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.

dmckee --- ex-moderator kitten

Posted 2009-08-30T12:09:51.560

Reputation: 7 311

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 – 2016-01-11T21:03:34.190

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 TMW – 2017-06-06T14:57:09.817

5I really intend to put all my scripts on a hourly-committed bzr repository. – Adam Matan – 2009-08-30T14:49:42.353

3The modern answer is: Recover the file from a recent filesystem snapshot. :-) – knweiss – 2009-08-30T14:57:24.007

4

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

Rich Bradshaw

Posted 2009-08-30T12:09:51.560

Reputation: 6 324

I usually delete files on purpose, I just realize I needed those files later. So interactive wouldn't help – andrewtweber – 2015-12-13T22:25:25.677

6I 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 – 2009-08-30T14:48:59.140

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 – 2009-08-30T15:40:46.027