2

So I wanted to clean a directory and subdirectories from non .doc|.docx files and I ran the following command:

rm -rf /home/user/dir/dir1/dir11/ !(*.doc|*.docx)

and I just deleted my home directory (hundreds of thousands of files...). My last save is not so recent. It's not the end of the world but it would help a lot if I could recover it. So my questions are:

1) why did it go wrong?

2) can I recover the deleted directories and files? Obviously they're not in the Trash. Would testdisk or photorec help?

Thanks for the help!

jejuba
  • 157

2 Answers2

1
  1. The problem in your command is the !(*.doc|.docx) construct. The * expands to every file and directory.
  2. You have to cease any further writes to the file system because when files are removed and unlinked (no remaining hard links to them), the file system free the blocks previously allocated for the deleted file, these blocks are allocated to new files and their contents overwritten. So to recover your files, you can use the photorec command (sudo apt-get install testdisk for debian). It will open a text based window so follow its instructions. The files will be recovered with different names generated by system.
Dababi
  • 3,355
  • 24
  • 23
  • 1
    rm -rf /home/user/dir/dir/dir11/ does not remove the whole home directory, only dir11 and what's beneath it. The culprit here is the !(*.doc|.docx) construct which does not do what the asker intended it to do, but instead the * expands to every file and directory. – Johan Myréen Dec 28 '16 at 07:51
  • You are right, I updated my answer – Dababi Dec 28 '16 at 08:14
0

1) why did it go wrong?

See @Johan Myréens comment and @Dababi's answer.

2) can I recover the deleted directories and files? Obviously they're not in the Trash. Would testdisk or photorec help?

Maybe. In my humble (and hard earned) experience, the only foolproof way to recover files are via backups. Yes, there are tools claiming that they can recover deleted files. I have had mixed (to say the least) experiences with those. More often than not, the files that are recovered are also corrupted and thus useless.

Although this is not an answer you may like, my answer is still: make regular backups of important files. Because sooner or later, "something" will happen, and when it does you can simply restore your files from whatever media your backup is stored (physical drive, server, "cloud" etcetera).

  • Thank you for the help. Now to make things clear(er), what would be the syntax to erase all .doc and .docx files from a directory and all its subdirectories. I'm sure, but not entirely sure, I've used the * and rm command without the upsetting consequences I encountered today. I thought the syntax would go deeper not work backwards to my home directory. To recover the files: you're right, there is nothing better than a backup. I have one, it's just no so recent. It's not recent because I never managed to set up a script to update my encrypted home with a backup on an external drive. – jejuba Dec 28 '16 at 08:39