Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
15  
What file system? –  Tarnay Kálmán May 5 '11 at 18:39
5  
Clearly you people have no sense of humor. –  Blomkvist May 5 '11 at 19:08
17  
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 6 '11 at 5:58
2  
For the future, don't use rm interactively at the command line. Use a utility like trash 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 6 '11 at 7:06
7  
@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 6 '11 at 10:57
show 8 more comments

12 Answers

In general there is no easy way back - but see RiMMER's interesting and useful link.

You could restore from your daily backup.

share|improve this answer
148  
Your remark on backups is cruel and 100% to the point. –  Benjamin Bannier May 5 '11 at 19:29
3  
OS X sets the bar for how backups should work (especially with Time Capsule). See maketecheasier.com/… for suggestions how to get somewhat the same thing with Ubuntu. –  Thorbjørn Ravn Andersen May 6 '11 at 3:08
3  
@Thorbjørn Ravn Andersen - It does, except when Time Machine has a nervous breakdown. I've had time machine have a freak-out on me twice in six months. –  Fake Name May 6 '11 at 5:31
    
Offsite backup is great and can be pretty affordable. I use Backblaze (Mac/Windows) at $5/computer/month. Many people seem to like Crashplan (Mac/Windows/Linux). –  Henrik N May 6 '11 at 6:02
8  
daily what ???? –  Luc M May 6 '11 at 15:00
show 2 more comments

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.

share|improve this answer
7  
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 May 6 '11 at 2: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 6 '11 at 3:48
1  
killall rm instead of turning off. –  Fred Nurk May 6 '11 at 7:19
    
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. –  billynomates May 6 '11 at 8: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 6 '11 at 14:36
show 1 more comment

There is no "undo" for this action, but you may still try recovering your files.

share|improve this answer
    
So the rm command doesn't zero out the file data? –  da code monkey May 5 '11 at 20:15
18  
No, of course not, that would take a lot of time. It just changes the TOC (table of contents) to mark those sectors as 'free to be rewritten over', and marks them as blank. If you really need to zero out that area, you need to copy new files with zeros over that place. –  Richard Rodriguez May 5 '11 at 20:58
    
@dacodemonkey: There are, however, tools for overwriting the actual data. –  Fred Nurk May 15 '11 at 4:32
add comment

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.

share|improve this answer
2  
Look before you leap, and ls before you rm. –  ultrasawblade Mar 31 '12 at 1:42
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
    
+1 for mentioning that on most journaled FSs, even a readonly mount may write data as part of journal acitivities. –  sleske Jan 4 '12 at 11:07
add comment

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.

share|improve this answer
add comment

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 \~.
share|improve this answer
add comment

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.

share|improve this answer
1  
That I believe is thanks to a NAS box. –  mlk May 6 '11 at 14:34
add comment
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!

share|improve this answer
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 6 '11 at 5: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 6 '11 at 5:42
8  
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 6 '11 at 7:04
1  
@FredNurk Exactly. Best bet is to just add the -i yourself, or just not use the -f option. –  Arda Xi May 6 '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 6 '11 at 15:03
add comment

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).

share|improve this answer
6  
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 6 '11 at 2:34
1  
-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 6 '11 at 16:33
add comment

protected by DMA57361 May 6 '11 at 16:19

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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