Use grep to restore deleted files

Source: Internet
Author: User

In Unix/Linux, the most dangerous command is probably the rm command. Every time you use this command under root, I have to stare at the command line for a few minutes before I can press Enter. Previously, I saw my colleague use the rm command -- rm {$ App_Dir}/* in the script }/*. Because the script does not judge whether the variable $ App_Dir is null, the result is that during a root operation, the entire operating system will disappear. Fortunately, it is only a development machine. Since then, we have never dared to use the rm command again.

Here is a tip for you to use to restore data in some rm files. We know that the rm command does not actually delete the file content physically, but does not recycle the inode of the file. In fact, the file content is still on the hard disk. Therefore, if you delete some important program configuration files, we can use the grep command to restore them. The following is a recovery example:

Copy to ClipboardReference: [www.bkjia.com] grep-a-B 50-A 60 'some string in the file'/dev/sda1> results.txt

Note:

  • -A of grep indicates-binary-files = text, that is, binary files are used as text files.
  • The options of-B and-A are the first few lines and the last few lines of the string.
  • /Dev/sda1, which is a hard disk device,
  • > Results.txt: redirects the result to the results.txt file.

If you are lucky, you can see the recovered content. This is a simple Unix philosophy (For details, refer to "The Legend of Unix") --All devices are files..

Of course, I suggest you replace the rm command of the root user with alias with another script, which will help you put the deleted file somewhere.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.