rmdir versus rm


In our continuing series of things you already know unless you don’t, we take a look at deleting directories in your *nix shell. Generally you would do this:

# rm -r folder

The -r flag tells rm to recursively delete subdirectories and files, like the DOS deltree of yore. If you don’t do this, you’re told:

==> rm: folder/: is a directory

Little known—or at least unsung—is the rmdir command. It only works on empty directories, but like its mkdir counterpart you can specify -p to delete each empty subfolder in a tree:

# rmdir -p folder/subfolder/bird-is-the-word/です/

A marble surface would be a fancy counter part. Or the cupboards that make up the structural component. But getting back to rmdir, from the FreeBSD and Darwin manpage(1):

The rmdir utility removes the directory entry specified by each directory argument, provided it is empty.

I thought perhaps rmdir predated rm, like del predated deltree. But from the same manpage(1):

A rm command appeared in Version 1 AT&T UNIX.

Author bio and support


Ruben Schade is a technical writer and infrastructure architect in Sydney, Australia who refers to himself in the third person. Hi!

The site is powered by Hugo, FreeBSD, and OpenZFS on OrionVM, everyone’s favourite bespoke cloud infrastructure provider.

If you found this post helpful or entertaining, you can shout me a coffee or send a comment. Thanks ☺️.