I'm new to UNIX (MAC OS X bourne shell POSIX), I understand rmdir
will only remove an empty directory but under what circumstances would you actually want to use rmdir <directory name>
and not just rm -r <directory name>
? Is there an intelligent use case I'm failing to appreciate and that knowing about would make my programming that little bit better if I did? Thanks.
2 Answers
rmdir
is safer: it refuses to remove a directory if it contains any files. What if you are trying to remove the wrong directory, or the directory still contains (possibly hidden) files you need?
There are also historical reasons: in the beginning the rm
program didn't have a -r
switch, so rmdir
was needed to remove directories. Back then rmdir
was a non-trivial program, but today it is just a wrapper for the rmdir(const char*)
C function.
I use rmdir
when I'm removing a directory that I expect to be empty, so that it will fail if there are still files in it. Using rm -rf
might blow away files that I want to keep, if I mistype something.