Linux Commands

How to remove a directory in Linux

Performing CRUD Operations in Linux are daily routine tasks like creating, reading, updating, and deleting a file or directory. This post will walk through a detailed guide on how to remove a directory in Linux from the terminal. Removing a directory in lynx is not as easy as removing a file. A directory may be an empty directory or may include multiple files in itself or further subdirectories in it.

There can be various ways to remove a directory in any Linux Operating system. For example, if you are using a GUI of any Linux Operating system like GNOME or KDE, you can remove any directory by just right-clicking on the directory. But, when you are working with a stand-alone Linux server and only have a terminal to work with the Linux Operating system, this guide will help you remove a directory in the Linux Operating system’s terminal.

There are multiple commands for removing a directory in Linux, based on the need and different purposes. This post will let you know about all these commands and perform all of them on Ubuntu 20.04 LTS system. The commands that we are going learn and are usually used to remove a directory in Linux are

  • rmdir
  • rm

So, let’s dive in and learn how to remove or delete a directory in Linux and the different ways to use the above-mentioned commands.

Remove a directory using the ‘rmdir’ command

This rmdir command-line tool is specifically used for removing or deleting a directory in the Linux Operating system.

The syntax of using the rmdir command is pretty simple and easy. Just provide the directory name after the rmdir command, and the directory will be removed only if it is empty.

$ sudo rmdir directory_name

If the directory is empty, it will be removed immediately. Otherwise, it will prompt an error something like “failed to remove ‘directory_name’: Directory not empty”. This message ultimately means that the directory includes some files or subdirectories.

So, if you want to remove the directory and everything in it, you need to switch to the rm command.

Remove a directory using the ‘rm’ command

The rm command does not only used to remove or delete directories, but it is also used to delete both directories and files. It also comes with some additional flags which help in performing different tasks according to the requirement.

For example, to remove a directory and its subdirectories, the -r flag is used to remove a directory, its subdirectories, and files recursively.

$ sudo rm -r directory_name

This command will remove the directory and all the subdirectories and files in it immediately, only if the directory is not write-protected.

If the directory is write-protected, it will prompt for confirming the deletion process of a write-protected file. If you do not want to get prompted for the confirmation, you need to use the -f option for deleting a write-protected directory in Linux forcefully.

So, the command for deleting a write-protected directory and all the files and subdirectories in it will go like this:

$ sudo rm -rf directory_name

Using the rm command, you can remove multiple directories in a single command as well. The command for deleting multiple directories would be like this:

$ sudo rm -r directory_name_1 directory_name_2 directory_name_3

This is how simply you can remove multiple directories in a single command using the rm command.


This post comprises detailed guidance on removing a directory in Linux using the rmdir and rm command. We have learned about various flags like a r flag that can recursively delete files and subdirectories. The -f flag can be used to delete a write-protected directory forcefully.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.