In your Linux journey, you’ll eventually want to delete directories as you do some housecleaning. Keeping your computer tidy makes you more efficient. Deleting directories you no longer need helps keep your system tidy.
It’s easy enough to manage and remove directories while you’re in your graphic file manager. Though sometimes, if you’re managing files in the terminal, you might just as well learn to manage directories while you’re in there. It’s not too terribly difficult.
As an example of why this is worth knowing, you may be working with a headless system, something without a GUI desktop environment. In that case, you will have no choice but to delete directories from the terminal. It’s just something you’ll come up against as you learn file management.
Either way, being able to delete directories from the terminal is a skill that comes in handy, a skill that you should know. And so it should be something you have in your toolbox.
The tools we’ll be using for this article are
rm. They’re both basic commands that have existed since the earliest of Unix days, and they’re both useful commands today.
The rmdir command describes itself as:
rmdir – remove empty directories
And rm describes itself as:
rm – remove files or directories
So, with that in mind, let’s jump into the exercise.
Delete Directories From The Terminal:
Deleting directories from the terminal obviously requires said open terminal. If you don’t know how to open the terminal, you can do so with your keyboard – just press
The first command is really simple. If it’s an empty directory that you want to delete, you can use the rmdir command. As you read in the intro, the directory MUST be empty for this command to work. It’s also really simple to run:
So long as the directory is empty, you should be all set to go. If the directory you want to delete is not empty, you have to use the rm command. It’d look like this:
rm -rf /path/to/dir/
If you’re curious, the
-r flag means recursive – it’ll even delete directories (and the files within them) within the directory. The
-f flag means force – it should delete all files and directories no matter what and without any confirmation.
If you’re curious about either of those two commands, and there should someday be an article about ‘rm’, and for now you can just check the manual. You’d use either
man rmdir or
man rm. There’s not much to the first, and the latter is complex enough to warrant its own eventual article.
Whoop! There it is! It’s another article in what’s turning into quite a long list of articles. If I’m going to keep this up for a full year, I might just as well have fun with it – and this was a nice, easy, and fun article. It’s mostly aimed at new users, as long-term users would hopefully already know how to delete directories from the terminal.
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.