Sudo rm -rf –no-preserve-root / vs sudo rm -rf /*

commandfilesystemsrmroot-filesystem

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?

Best Answer

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.)

Related Question