120

I had a folder I didn't want named '~', so from the command line I typed

rm -rf ~

and accidentally deleted my home folder (since ~ resolved to /home/username). Is there any way back or do I basically need to recreate the account from scratch?

LawrenceC
  • 73,030
  • 15
  • 129
  • 214
Chris
  • 1,819
  • 3
  • 14
  • 11
  • 18
    What file system? – Tarnay Kálmán May 05 '11 at 18:39
  • 1
    If you just want to restore your ability to login, login as root, `mkdir /home/{your username}`, and then `chown {your username}:{your group} /home/{your username}`. – LawrenceC May 05 '11 at 18:56
  • 7
    Clearly you people have no sense of humor. – Blomkvist May 05 '11 at 19:08
  • If you get in the habit of using `shred` instead of `rm`, you should end up ok in the end. – Thomas Eding May 06 '11 at 03:42
  • 22
    Though this doesn't directly answer your question, `rm -rf "~"` with quotes will look for that literal name instead of expanding it. But I would be scared to use that anyway – I would use something like `ls` or `cd` first to verify I get the right dir, or delete the folder from a GUI file manager. Perhaps first rename the dir (`mv "~" tilde`), verify and then remove. – Henrik N May 06 '11 at 05:58
  • 2
    For the future, don't use rm interactively at the command line. Use a utility like [trash](http://manpages.ubuntu.com/manpages/natty/man1/trash.1.html) and you 1) can't make the same mistake again and 2) can't get used to a poor shell alias of rm that will cause you trouble. – Fred Nurk May 06 '11 at 07:06
  • @Fred `rm -ir ~` will print `examine files in directory /Users/danielbeck`. It's safe enough for use like this. – Daniel Beck May 06 '11 at 07:18
  • 1
    @DanielBeck: If you have the discipline to always type the option, that works fine. In my experience, users are more likely to drop the option and go back to using rm directly. – Fred Nurk May 06 '11 at 07:22
  • 9
    @HenrikN: ./~ instead of "~" does the same, but also works with other common problematic filenames, like those that start with dashes and look like options. – Fred Nurk May 06 '11 at 10:57
  • @FredNurk Just not using `-f` is already an improvement. – Arda Xi May 06 '11 at 13:07
  • 1
    Blomkvist As I reported and commented on your answer, I assume your comment was directed at me. Just post your non-answers (i.e. comments) as actual comments next time and nobody will care. They'll probably even help you get the [Pundit](http://superuser.com/badges/40/pundit) badge. I liked the story and would have upvoted a comment. – Daniel Beck May 06 '11 at 17:15
  • 1
    Like @Henrik N's comment this is not a solution either. But I always use `gvfs-trash` (Ubuntu) even if I'm at the command line, instead of `rm`. It can delete directories or files, and unlike `rm` there is a recovery option. I made an `alias del=gvfs-trash` in my `.bash_aliases` and also `alias`ed `mv` to `mv -i` to avoid the other kind of irreparable error. – isomorphismes Sep 01 '12 at 02:33
  • Just be happy it wasn't `rm -rf /` or `rm -rf .`... ;) – sjas Feb 24 '13 at 17:48
  • I'm just curious: who created the folder named '~' which you found on your computer? – unforgettableidSupportsMonica Oct 07 '15 at 16:51
  • Where do I get my '"sudo rm -rf ~"-without-a-backup'-badge? I want to claim it right now. – Daniel F Jan 14 '16 at 15:30
  • Don't name folders "~"? ;-) – Craig Tullis Dec 11 '16 at 07:18

11 Answers11

153

In general there is no easy way back.

You could restore from your daily backup.

RedGrittyBrick
  • 81,981
  • 20
  • 135
  • 205
69

Others (such as @RiMMER, @Adam, and @James T) have mentioned that, for most filesystems, it's possible to recover most or all of your data (perhaps without filenames), because the data is not actually zeroed out, only removed from the file-table. This is not just true of Linux - the same is true of Windows and Mac.

However, no one has mentioned the most important thing - TURN OFF YOUR COMPUTER. NOW..

Many programs - including the internet browser you're using right now - will cache data to the hard-drive, and pretty much anything you do can cause your computer to resize/write to the swap file. Both of these things will write to free sectors on the hard drive, potentially overwriting your precious data.

Thus, turn off your computer, take out the hard-drive, and put it in another computer. Mount the hard-drive as read-only, and recover the files onto a separate hard-drive. When you've recovered everything you can of interest - and only then - write the files back to the original drive.

  • 12
    Even the process of shutting down can write to the hard-drive. I'm hesitant to recommend immediately unplugging your computer, because that can definitely make things **much** worse, but... well, depending on how important the data is, you might want to consider it. – BlueRaja - Danny Pflughoeft May 06 '11 at 02:24
  • 1
    Depending on the drive/OS/mount point it may be possible to immediately unmount and/or remount readonly live without turning the pc off, then shutting down etc. – Matthew Scharley May 06 '11 at 03:48
  • 1
    `killall rm` instead of turning off. – Fred Nurk May 06 '11 at 07:19
  • 1
    Depending on how big your home directory was and how big your hard drive is, the chances of over-writing the free sectors which contains your data is pretty slim. – MSpeed May 06 '11 at 08:27
  • You can recover text files using this method without a lot of difficulty (as long as you have a rough idea of whats in them), but binary files are so difficult as to be basically impossible. – Satanicpuppy May 06 '11 at 14:36
  • @Satanicpuppy: That depends on circumstances (particularly on the FS in use). I once accidentally deleted an 800MB video file, and was able to undelete it without any data loss - however that was on a separate partition without any regular write activity. So it really depends (and for the record, the partition used Ext3). – sleske Jan 04 '12 at 11:06
13

I believe that every linux user at one point in time must learn this painful lesson firsthand before actually learning the importance of backups, as well as paying attention to detail and being careful.

For instance, if you're deleting wierd looking files, you could first run commands on that wierd file that have no side effects and that don't make changes to your data. For instance, had you run

cd ~

before trying to delete the folder, you would have seen that it would instead change directory to your home folder, not the ~ folder. Once you go through enough troubleshooting steps to learn the true command you would need to correctly cd into the ~ folder, then you could press your up arrow key to pull the last command from history and then change cd to rm and then and only then try to delete the folder.

I suggest taking steps like this before performing any action that is permanent. It's not permanent or foolproof, but you have a much better chance of preventing those oops moments if you take this degree of care when performing actions that have side effects.

jmort253
  • 1,421
  • 12
  • 18
7

You can try using testdisk to undelete individual files. This apperently only works on ext2 and NTFS (but still worth a try).

If you have ext3 or ext4, you can recover known files using photorec, which will probably end up recovering more than you want it to. Photorec recovers files without the filename information so it can be tedious to go through the files.

James T
  • 9,941
  • 4
  • 28
  • 30
7

If you are using the ext3 filesystem, then ext3grep is your new best friend. A good tutorial (among many others) on using ext3grep can be found here.

Adam Prax
  • 1,080
  • 6
  • 9
4

If you have a large enough external drive you have another possibly safest option. You will need enough space to store the entire home partition including free space.

Boot from a CD and don't mount the home drive at all. Mount the external drive. dd the home drive partition into a file on the external drive. mount the file as a loopback device do your undeleting work on this image, safe in the knowledge that you can't do any damage to your original filesystem. Depending on the filesystem there may be many things to try, some of them potentially destructive.

This saved me once with a beta version of Reiser4 when I didn't know what it would do even if I mounted it read only. I read somewhere at the time that journal writes were still possible.

It's also a good technique for saving data from a dying drive.

barrymac
  • 765
  • 3
  • 9
  • 21
  • 1
    +1 for mentioning that on most journaled FSs, even a readonly mount may write data as part of journal acitivities. – sleske Jan 04 '12 at 11:07
2

There are techniques to recover files by inspecting the inodes that have not been recycled. The more data that you write to the file system after the deletion, the less likely you are to recovery your data. The best strategy to recovery from an accidental data deletion is to restore from backup.

Xenoactive
  • 1,040
  • 5
  • 7
2
alias rm="rm -i"

Assuming you are using the Bash shell (put this in your .bashrc file so it persists to new shells you open).

This should result in rm asking you to confirm what you want to delete, which is nice especially when you are doing -rf, but it also will expand '~' to the full path to your home directory in the prompt, which should give you a clue you are not deleting a file '~' but actually your homedir.

$ rm -rf ~
rm: examine files in directory /nas/users/u12345/unix (yes/no)? n

This is not technically an answer to your question, but might help you avoid such problem in future.

EDIT: Some people may discourage you from using this technique as it makes you less careful with the "rm" command. In my experience this is far from the truth. In reality, the effect of the alias is to discourage you from using "rm -rf" - as you know it will result in many prompts you don't want to answer - and in order to get a no-prompt "rm -rf" you need to deliberately refer to the rm command as /bin/rm (or whatever). This leads to much more conscious and deliberate use of the "rm" command in general - as you always need to precede usage of rm with a mental choice of "rm or /bin/rm" - on both your server and any other you happen to be logged in to. And a world where everyone thought before using rm would be a safer one indeed!

aaa90210
  • 161
  • 7
  • 2
    Depends on the system you're on. On RHEL5 and OS X 10.6 at least, `-f` overrides the `-i` option. – Daniel Beck May 06 '11 at 05:38
  • I did not know that. I append my advice with YMMV and "don't try this at home, or probably more importantly at work"! – aaa90210 May 06 '11 at 05:42
  • 9
    If you do this, you will one day not have this alias in place – maybe you're sitting at another computer or you have ssh'd into somewhere – and you will use rm being used to the safety net. You will do something you regret. – Fred Nurk May 06 '11 at 07:04
  • 1
    @FredNurk Exactly. Best bet is to just add the -i yourself, or just not use the -f option. – Arda Xi May 06 '11 at 13:06
  • When I have alot of files to delete, I begin with `rm -i my_list_of_files`. Then I use CTRL-C and recall my command to modify it to `rm -rvf my_list_of_files` – Luc M May 06 '11 at 15:03
2

I'm sorry about your data. Everybody else has chimed in with how to deal with your data problem, but here is some information on how you can deal with the next time you see a folder caled ~.

  1. Find whoever created it and make sure they never do it again.
  2. Don't use -rf or any mad powerful arguments to rm to do the delete. Instead take it nice and slow and make sure you can see what's happening. It's possible to enter the tilde without it expanding to your home directory by escaping it on in the shell. In theory you could rm -rI \~, however you can go one step further.
    1. First, cd \~ to get INTO that folder, maybe even use pwd to check that you got there. Ten remove it's contents while you're in there. Again be careful but clean out the folder with rf -rI * and if you need to get .* too.
    2. Now move back up with cd .. and remove the directory with rmdir \~.
Caleb
  • 5,770
  • 5
  • 27
  • 34
1

I just cd .snapshot and restore the files. But, I do this at work and I have no idea whare the .snapshot directory comes from. I though maybe it was a Linux feature.

meBigGuy
  • 11
  • 1
-3

rm is forever.

Although this doesn't help you now, you could in the future not use rm or to really force yourself, add a script found earlier in your path named rm that moves the files to a trash folder that you clean out some other time (allowing you to make mistakes and not be SOL).

Corey
  • 11
  • 7
    Only problem with this is you may become a little fast and loose with the rm command and forget that you don't have the handy undelete functionality on your company Linux server... oops... – jmort253 May 06 '11 at 02:34
  • 2
    -1 This has the same problem as over down-voted answers (so I'll steal someone else's comment): one day not have this script in place – maybe you're sitting at another computer or you have ssh'd into somewhere – and you will use rm being used to the safety net and you will do something you regret. – DMA57361 May 06 '11 at 16:33
  • @Corey Although I agree that this could cause a bad habbit, +1 because this answer has been too heavily downvoted – John Militer Feb 12 '17 at 21:56