I see a lot of resources on rm -r
but almost nothing on rm -d
even though the d
flag is apparently all about deleting directories.
What is the difference between the two exactly? Is rm -d
the same as rmdir
? Is it "safer" than rm -r
?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityI tried both commands and here are my conclusions:
rm -d
works just like rmdir
(unless there's something going on with links or something like that).rm -r
deletes the target directory and recursively deletes everything inside that directory (including non-empty directories and files of any type)So the difference is pretty clear. There's no doubt that rm -r
is a really dangerous command that should be used very carefuly (Example: rm -rf /
can potentialy delete your system; or rm -rf *
could delete your current project), while rm -d
will only let you delete empty directories
Remember that rm -rd
is a valid command and it will work like rm -r
, so be careful.
rm -rf /
being dangerous, however there are protections that help to prevent you from deleting your file system, and you would have to be actively malicious to provide the proper arguments necessary to erase the root system directory.
--no-preserve-root
option to do that kind of thing, but the point is pretty clear.
Sep 28, 2017 at 22:46
rm -d
doesn't remove an empty directory on my system with coreutils 6.9 (not Ubuntu), while rmdir
does. Not sure why.
rm -rf /
shouldn’t work because it would remove the directory you are currently in. Sun Microsystems took this approach in Solaris, so Solaris derivatives implement this afaik. Then again, Linux isn’t Unix and cares less and less about POSIX I feel.
rm -rf *
is dangerous, better example? I'm going to edit it.
Sep 29, 2017 at 15:07
A difference between rm -rd
and rm -rf
is that the former will prompt you before deleting a write-protected file or directory, while the latter will go ahead and delete write-protected files without prompting if it can do so. While rm -rd
and rm -rf
are both recursive and potentially dangerous if directed to the wrong directory, the former provides a little more protection against doing something you really didn't want to do.
-d
will only remove empty directories.-r
doesn't care.-r
only doesn't care if also-f
is set-r
is recursive, it repeatedly deleted the contents.-f
just means don't prompt the user