3

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 2

7

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.

0
3

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.

You must log in to answer this question.

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