rmdir versus rm

Software

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

Me!

Ruben Schade is a technical writer and IaaS engineer in Sydney, Australia who refers to himself in the third person in bios. Wait, not BIOS… my brain should be EFI by now.

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

You can leave me a comment by contacting me, and I might publish your thoughts. Please read the FAQs first though.

If you found this post helpful or entertaining, you can shout me a coffee or buy some silly merch. Thanks!