What is the point of UNIX's rmdir?

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.

AJP

Posted 2012-09-20T21:15:37.970

Reputation: 201

Answers

8

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.

Joni

Posted 2012-09-20T21:15:37.970

Reputation: 334

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.

Greg Hewgill

Posted 2012-09-20T21:15:37.970

Reputation: 5 099