13

I've always been curious why the command for deleting everything in a directory is rm -rf.

Why aren't there flags to do the same thing with rmdir?

Wouldn't it be more intuitive to use rmdir for directory operations?

  • 9
    rmdir removes directories, rm removes. Why would it be intuitive for rmdir to remove non-directories? – Stéphane Chazelas Dec 12 '15 at 21:39
  • 5
    Because it isn't. You're 40-something years late asking this question, and you're asking the wrong people. – user207421 Dec 13 '15 at 9:04
25

In the early Unix File System (at least back in the V7 days circa 1970) directories were implemented as special files and only root could use the mknod(2) system call that created them and only root could unlink(2) a directory special file.

These protections were in place in order to keep the filesystem structure consistent. For example, if a user was allowed to write to a directory special file, he could make its parent directory .. point to itself (specifically its own i-node). This would create a circular reference in the filesystem which would be a Bad Thing. Of course there are other inconsistencies one could make, this is just a clear example.

The consistency was maintained by user-space programs like mkdir(1) and rmdir(1) which were Set-UID root so that they could make the privileged system calls on behalf of an unprivileged user. When recursion was added to rm(1), the remove command would run as the current UID and then call out to rmdir(1) solely for removing empty directories. This is still a pretty standard method of elevation of permission: don't use more permissions than you need.

Sometime later mkdir(2) and rmdir(2) were added as their own system calls but the relation between rm(1) and rmdir(1) remains.

Personally, I find it a tad more satisfying to rmdir junk and know that the worst I did was remove an empty directory.

  • 1
    Re the last sentence: Presumably, if the recursive delete operation was in rmdir, it would still need a -r flag. (rmdir -r junk instead of rm -r junk, and rmdir junk still only works if the directory is empty) – user253751 Dec 13 '15 at 4:20
7

It's historical. rm was made to remove the references to files, rmdir was made to remove directories, parallel to mkdir. Many years ago, Unix rm could only remove directories by invoking rmdir. There also wasn't an rmdir(2) system call, rmdir was a program that called unlink(2).

References:

2

Purely a matter of opinion, but rm removes files while rmdir removes directories. A directory is a file, but a special type of file, so it makes sense for rm to remove them, but to treat them specially (i.e. to require an extra option to enable the capability.) On the other hand not all files are directories, and it makes no sense IMHO for rmdir to remove something that isn't a directory.

  • 1
    If that was the case, then why can't rm remove an empty directory (except in recursive mode)? – user253751 Dec 13 '15 at 4:19
  • Same reason it can't remove a full directory - it requires the -r flag to enable the directory special-file feature. – Darwin von Corax Dec 13 '15 at 4:23
  • 1
    Circular reasoning there... – user253751 Dec 13 '15 at 4:32
  • I'd say my reasoning has an eccentricity >> 1... – Darwin von Corax Dec 14 '15 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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