Command Line

Let’s Learn To Remove A Directory

Today’s article is going to teach you how to remove a directory. This being Linux and rmdir, you can only remove empty directories. That’s okay, that’s still what we’re going to use. I’m pretty sure I’ve explained how to remove files before and, if not, that’s a pretty simple task.

This article is going to be light on details and pretty short. I popped positive on one COVID test and negative on another. I could take a third test, but I really just feel crappy and not all that bad. Still, it’s an excuse to have a nice and easy article! I like those sometimes! My proofreading may be sub-par.

Like I said, we’ll be using ‘rmdir’ and that only works on EMPTY directories. I can’t stress this enough! If there’s a file in the directory, any file, it will not delete the directory. 

Umm…

I do not know the design reason for making ‘rmdir’ (a standard tool in every Linux) only remove empty directories – but I suspect it’s a protection method. It’s protecting itself from you – meaning you must go in and confirm that it is empty and that there are no files remaining that you’d like to keep. After that, you can delete the directory.

That stands to reason and, frankly, it’s the only reason I can think of. If anyone knows the real reason, assuming I am wrong, please feel free to let me know in a comment. I’m quite curious. Ah well… Onto the article!

Remove A Directory:

This article requires an open terminal, like many other articles on this site. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

Now, with your terminal open, let’s use ‘rmdir‘ and learn how to remove a directory. In its basic form, the command is:

rmdir <directory>

This works if it’s a path you want to specify:

rmdir /path/to/dir

If you want, you can use the -v switch and get a verbose output, telling you what is being deleted. This is probably for the best. So, it’d look like:

rmdir -v /path/to/dir

But wait, there’s more! What if you wanted to remove all of that, you want to remove /path /path/to and /path/to/dir? Well, you can do that:

rmdir -p /path/to/dir

Of course, if you want confirmation of what’s being deleted, try:

rmdir -p -v /path/to/dir

Assuming all those directories are empty, the command will complete itself and remove all the directories in that path. Those are basically all the rmdir commands you’re likely to need, but you can check man rmdir for more information.

Closure:

See? Nice and easy. See? You even get an article when I’m dying from the plague! Don’t worry, I’m sure I’ll be just fine. Statistics are my friend at times like this, so my worry level is exactly 0%. The odds of it incapacitating me are negligible and I’m a pretty big fan of math.

I’m vaxxed to the max and even boosted – which only lessens your chance. I’ll be fine. After all, only the good die young! (I am not young, now that I think about it!) I’ll quarantine at home and hope for the best, while monitoring for degradation.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

KGIII

Retired mathematician, residing in the mountains of Maine. I may be old and wise, but I am not infallible. Please point out any errors. And, as always, thanks again for reading.

View Comments

    • It gets worse! I tend to look to see if I'm duplicating a topic, but I've actually covered this topic before.

      Where I included 'rm' and 'rmdir'... Though it was a fairly short article and this one covers rmdir better than the last one.

      LOL I did not realize it was duplicating a previous article. In my defense, I've written hundreds of articles.

Recent Posts

Setting Up Coding Environments on Linux for Educational Use

With so many strong attributes, such as robustness and flexibility, Linux stands as a powerful…

27 mins ago

View Detailed Hardware Information

There are many tools for showing your hardware information and today we'll get to view…

2 days ago

How To: Install Wine In Lubuntu

Today's article isn't going to be complicated or long as we are going to cover…

4 days ago

Update Python Packages (PIP)

We've had a run of Python packages recently and you can tell that I'm a…

6 days ago

Save A Command’s Output To A File (While Showing It In The Terminal)

The title is the best I can come up with to describe this exercise as…

1 week ago

Demystifying journalctl: A Comprehensive Guide to Linux System Logging

It was suggested that I write an article about journalctl, which seemed like a large…

1 week ago