By 苏剑林 | July 16, 2017
Warning
The following content contains many high-risk actions. Please do not imitate them casually. Minors should watch under the supervision of their parents~ (^_^)
Suicidal
Linux systems (the following also applies to Mac OS) are famous for being open-source and free. However, sometimes they are a bit too open, and I have been burned by these extremely open characteristics several times (mostly due to poor usage habits). I have summarized and shared these experiences for everyone's entertainment.
The most classic example is achieving "suicide" through the following command:
sudo rm / -rf
This destroys your Linux system. Obviously, in Windows, this would be equivalent to formatting the system drive while inside the operating system, which is strictly forbidden.
A similar command is mv. For example, the following command, while not quite as severe as rm, is enough to ruin your system:
sudo mv /* /root
There are many such high-risk commands, such as dd. In Linux or Mac OS, using dd to burn images is very common, but you must be certain of the target drive identifier. Otherwise, you might regret it deeply. For example:
sudo dd bs=4m if=xxx.img of=/dev/rdisk0
And then your system is gone—this is a personal experience of mine!! While burning an image for a Raspberry Pi, I accidentally wiped my MacBook's system and spent a long time reinstalling it over the network.
Accidental Killing
Of course, even though commands like sudo rm / -rf are dangerous, we aren't stupid, so we don't run them directly. Therefore, they are mostly for entertainment and don't lead to real danger. More serious consequences are often caused by accidents, and usually unexpected ones (especially for beginners).
For example, the following command is very common:
sudo mv a/* b
The intent is simple: move everything inside directory a in the current path to directory b in the current path. It looks safe and doesn't affect the system itself. However, what if your hand slips and you add an extra space?
sudo mv a /* b
Sorry, all you can do is laugh it off. You can only hope you reacted quickly enough to press Ctrl+C. Otherwise, you're in for a lot of trouble. This will also move all your system files into the b directory, causing almost all commands to stop working (including mv). This is also a pit I fell into not long ago, wasting a lot of time as a result. By the way, the rm command carries the same risk.
Recovery
Now that we've finished the sad stories, let's talk about something heartening~
If you accidentally delete system files, especially through a destructive deletion, I'm afraid I can't help much. However, if you only accidentally deleted a text file—say, a code script you just finished but forgot to back up (especially long code you spent ages writing, which makes you want to give up on life)—it is still possible to recover, and the recovery process isn't particularly complex.
First, let's create a file:
echo 'Physical cosmology is a branch of astrophysics that studies the large-scale structure of the universe and basic questions such as its formation and evolution. The object of study in cosmology is celestial motion and its first causes, which for a long period of human history was part of metaphysics. As a science, cosmology originated from the Copernican principle and Newtonian mechanics, which stated that celestial bodies and objects on Earth obey the same physical principles and explained the motion of celestial bodies. This branch is now known as celestial mechanics. It is generally believed that physical cosmology originated in the twentieth century with Einsteins general theory of relativity and astronomical observations of extremely distant celestial bodies.' > test.txt
Then delete it with rm test.txt. Next, execute:
sudo grep -a -B 5 -A 10 'The object of study in cosmology is celestial motion and its first causes' /dev/sda5 > results.txt
Parameter explanation:
1. grep is a text search based on regular expressions;
2. -a means --binary-files=text, treating binary files as text files;
3. The -B and -A options represent the number of lines before and after that string, respectively;
4. 'The object of study in cosmology is celestial motion and its first causes' is some content from the original text file;
5. /dev/sda5 is the hard disk device, which can be checked using the df command;
6. > results.txt outputs the results to the results.txt file.
This command is quite time-consuming and might even report out-of-memory errors. Regardless, after the command finishes, we can look for the content we need in results.txt. If you are lucky, the deleted content will be in results.txt. If you modified test.txt multiple times, the original text fragments might appear repeatedly in results.txt. To locate the text content, you can use programming tools to read results.txt and search (because if the text snippet you provided is too common, results.txt might be huge, and a standard text editor won't be able to handle it). For example, I use Python:
s = open('results.txt').read()
'The object of study in cosmology is celestial motion and its first causes' in s # Returns True, there is hope ^_^
idx = s.index('The object of study in cosmology is celestial motion and its first causes')
# Check if it is the content we just deleted
# If not, let s = s[idx+len('The object of study in cosmology is celestial motion and its first causes'):] and repeat the above
print s[idx-100:idx+100] # Gradually adjust the 100 parameter until the deleted content is found.
I didn't expect Linux to have this trick up its sleeve~ I only found it on Google after accidentally deleting a script a few days ago. Indeed, technical skills are accumulated through lessons of blood and tears.
Reference
http://coolshell.cn/articles/2822.html