0

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?

2
  • It will remove all files on your external hard drive if mounted and for what your user has the rights to delete. Instead of using rm -rf ./* you might consider using rm -rf * for deleting the content of current folder (incl. subfolders). So you can not accidentally end with /* pattern.
    – Marvin
    Nov 19, 2020 at 22:25
  • 1
    If the backup is done as a different user, the normal user may not have permissions to delete files from the external disk. The permissions depend on the file system. (For example FAT32 does not provide Unix style permissions.)
    – Bodo
    Nov 19, 2020 at 22:40

1 Answer 1

2

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.

2
  • Note that they're not running the command as root. So, they could also mount the backup drive somewhere non-root users can't read.
    – mattdm
    Nov 19, 2020 at 22:52
  • Can you give me an example of where I could mount it to achieve this.
    – cyrusbehr
    Nov 20, 2020 at 22:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .