As everyone knows, these are two deadly unix commands that both tell a machine to commit suicide. But what is the difference between the two? The first one deletes the root directory, while the second one deletes everything in it. Both are equally bad, but will the first one delete the filesystem because it deletes the root directory itself? What is the difference?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The same difference as with rm -r dir and rm -r dir/*.
The second only removes whatever the glob matches, usually every file whose name doesn’t start with a dot, but can be configured in bash and likely others. It also fails if you have lots of files in the directory since the command line can only fit so much. Not that you’d usually have either in the root directory, but still.
The first one will recurse into dir, removing all contents, and then the directory itself. But as said, you can’t remove the root directory anyway. On Linux, the error you get is Device or resource busy, which is exactly what you get trying to remove any directory holding a mounted filesystem. (It doesn’t even bother checking if the directory is empty before dropping that.)
For the same reason, you can’t usually get the root directory empty either, you’ll have stuff like /proc and /sys (on Linux) mounted, and you can’t remove the mount points without unmounting them.
And well, strictly speaking, removing all files doesn’t kill the system… It just makes the usual paradigm of launching external programs to do stuff a bit hard to use. But running programs that don’t need any files on the file system any longer wouldn’t be affected. You might be able to try it with something like the busybox shell, that has integrated rm and ls. (Booting up the next time might be hard though, if your boot files were on a mounted filesystem.)
Method 2
--no-preserve-root bypasses the opposite --preserve-root directive which could be set as an alias or even a default option in rm depending on the system. This option is merely a super newbie protection which won’t protect the system much against someone who would run such a command in the first place.
If there is no --preserver-root directive (bypassed or default), rm will attempt to delete everything on the system.
Note that it will always fail to do so as there are many files which it will not be able to delete because they are open. Also note that / is a kernel construct and could not be deleted in any case, even if it were possible to delete all its mounted content.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0