What is the reason for rmdir(1) and rm(1) to co-exist?
The main reason is probably historical. Back in the old, old days, there were no rmdir(2)
and mkdir(2)
system calls (we're discussing 7th Edition UNIX™ here), and rmdir(1)
was (of necessity) a SUID root program that used the unlink(2)
system call to remove directories.
The 7th Edition UNIX manuals are available online at http://cm.bell-labs.com/7thEdMan (last checked 2017-04-23); They are also available at http://plan9.bell-labs.com/7thEdMan (last checked 2017-04-23). There also seems to be at least one alternative source online — http://wolfram.schneider.org/bsd/7thEdManVol2/ — for the articles in Volume 2, with a link to the FreeBSD site for the commands and system calls in Volume 1.
The rm
command (a regular non-SUID program) invoked the rmdir(1)
command to remove empty directories. It could not do so itself; that required root privileges. So, the rmdir(1)
command (see here for its source code in Unix V7) existed to remove empty directories, and the rm
command did not remove empty directories itself.
To use rm
to remove directories, you have to give the -r
option.
There's also a symmetry argument. You need a command mkdir(1)
to create directories; it seems reasonable to have a command rmdir(1)
to undo what mkdir(1)
did. Plus they are (these days) simple exercisors of the the rmdir(2)
and mkdir(2)
system calls — yes, back in 7th Edition UNIX, mkdir(1)
was also a SUID root program, using the mknod(2)
call to create a directory node and the link(2)
call to create the .
and ..
entries in the directory.
"rm" does not work on directories. You have to either use rmdir or specify the -r switch for a recursive deletion. The reason is historical: unlink
and rmdir
are separate system calls and have been from the early days of Unix.
Also rmdir only removes empty directories. If you want to make sure you don't delete any additional files in a directory, rmdir
is more safe than rm -r
(except if you aliased rm such that you always need to confirm what you delete, i.e. alias rm='rm -i'
in ~/.bashrc or whatever you are using).