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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityIn 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.
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)
Dec 13, 2015 at 4:20
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:
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.
rm
remove an empty directory (except in recursive mode)?
Dec 13, 2015 at 4:19
-r
flag to enable the directory special-file feature.
Dec 13, 2015 at 4:23
rmdir
removes directories,rm
removes. Why would it be intuitive forrmdir
to remove non-directories?