Can contents of an external hard drive be wiped by running `rm -rf /*` on Ubuntu

Can contents of an external hard drive be wiped by running `rm -rf /*` on Ubuntu

I know it is bad practice, but out of laziness I generally run rm -rf ./* when I want to remove the contents of my current directory (ex. want to clear out my build directory for a C++ project I am compiling). One time, I made a typo and instead ran rm -rf /* (...facepalm). I was fortunately able to stop this before it totally wiped everything from my computer, but it did do enough damage that it took me a day or two to get my system back into working condition, and I also did loose some valuable data.

This prompted me to get an external hard drive and to save a snapshot of my system to the hard drive every day. Now here is my questions, if I accidentally did run rm -rf /* (not as the sudo user), would this also delete the contents of my external hard drive (defeating the purpose of the daily snapshots)? Or would this only unmount my hard drive, preserving the data on it?

答案1

If you were to run rm -rf /* with the external hard drive mounted, it would in fact delete all files on the external hard drive that could be deleted by whatever user you are running as - it will not unmount the hard drive.

To minimize the risk of this, you could have your script mount the external hard drive before beginning the backup, and unmount it afterwards (though there is still some risk that if the unmount fails for whatever reason, the external hard drive would remain mounted after the backup is done, until the next time the script runs).

A better alternative would be to do your daily backup to some destination that is not on the machine you are backing up - or, at least, is not a mounted filesystem. That way, inadvertently deleting data via filesystem operations would not damage your backup.

相关内容