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?
|
In general there is no easy way back - but see RiMMER's interesting and useful link. You could restore from your daily backup. |
|||||||||||||||||||
|
|
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. |
|||||||||||||||
|
|
There is no "undo" for this action, but you may still try recovering your files. |
|||||||||
|
|
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
before trying to delete the folder, you would have seen that it would instead change directory to your home folder, not the 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. |
|||||
|
|
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. |
|||
|
|
|
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. |
|||
|
|
|
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. |
|||
|
|
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. |
|||
|
|
|
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
|
|||
|
|
|
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. |
|||||
|
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.
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! |
|||||||||||||||||
|
|
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). |
|||||||||
|
This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.
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 likelsorcdfirst 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