Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I use BSD and Linux every day, I have never had a circumstance which I must use rmdir(1) rather than rm(1). What's the purpose of rmdir's existence?

Thank you!

share|improve this question
1  
OT, but, FWIW, this shows interesting (and different) usage: cyberciti.biz/faq/linux-unix-remove-directory –  pst May 29 '12 at 3:01
add comment

migrated from stackoverflow.com May 30 '12 at 0:28

This question came from our site for professional and enthusiast programmers.

3 Answers

up vote 12 down vote accepted

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 were available online until recently at http://cm.bell-labs.com/7thEdMan; they were not accessible there when I tried tonight, though. Google thinks they should be at http://plan9.bell-labs.com/7thEdMan; they are not accessible there, either, tonight. There does some to be at least one alternative source online still (http://wolfram.schneider.org/bsd/7thEdManVol2/) for the articles in Volume 2, but not 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 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.

share|improve this answer
    
aha! this all makes sense, great! –  Howard Guo May 29 '12 at 3:05
2  
Nice. I just checked a copy of 3BSD I have here and there's no documentation for an rmdir syscall there either, and rmdir(1) is still implemented using unlink. –  Andy Ross May 29 '12 at 3:10
2  
One major downside of the mknod + link and unlink system was that creating a directory was not an atomic operation, so you could end up with a partially complete directory. There were lots of programs devised to check file systems for the inconsistencies that arose; fsck(1) is the one that survived. –  Jonathan Leffler May 29 '12 at 3:22
    
@Jonathan, bringing back memories of Xenix on that one. Yuck! Yes, really bad things could happen. –  Fiasco Labs May 30 '12 at 0:40
add comment

"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.

share|improve this answer
3  
A happy side-effect is that you're slightly less likely to accidentally delete a directory when you intend to remove only a file. –  Adam Liss May 29 '12 at 3:01
    
Thank you. I noticed that "rm -r" and "rmdir" have the same amount of key strokes. Does rmdir exist purely for historical reason (.. being compatible with decades old Unix programs)? –  Howard Guo May 29 '12 at 3:03
2  
Actually, back in the early days of Unix, neither rmdir(2) nor mkdir(2) existed as a system call; user root could use the mknod(2) call to create a directory node and the link(2) call to create the . and .. entries in the directory; and root could use the unlink(2) call to remove the directory entries. –  Jonathan Leffler May 29 '12 at 3:05
add comment

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).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.