How to remove a directory in Linux?

Remove Directory Featured Image

Every new Linux-user stumbles upon the question – How to remove a directory in Linux using the command line? This question is followed up by questions like what about a non-empty directory, or can the directories be regained?

Different Ways to Remove a Directory in Linux

This article will answer any such question in detail and with examples.

1. Remove empty directories using rmdir command

As the name suggest, rmdir is used to remove directories in Linux, only if they are empty. To achieve this, we run:

rmdir <DIR_NAME>
Remove Directory Rmdir
Removing a directory using ‘rmdir’

This only works when we are present in the parent directory of the directory to be removed. Otherwise, the relative path to the directory must be added. In the above image, the “remove_folder” is present in the home directory and so is the current working directory of the terminal.

The terminal throws an error if we try to remove a non-empty directory using rmdir.

2. Remove nested empty directories

In order to remove multiple empty directories inside one another, we can remove all of them with a single command, instead of removing them one by one. This task is achieved by '-p' option with rmdir command.

rmdir -p <DIR_NAME>/<DIR_NAME>
Remove Directory Parent
Remove nested directories

In the above snippet, we are removing all the empty folders, starting from the innermost (remove_folder_2) to the outermost (remove_folder). This only happens when each of them is empty (no files).

Note: It is quite important to know that directories deleted using rmdir command are deleted permanently.

3. Removing directories with rm command

The rm command is used to remove files as well as directories in Linux. Therefore it can easily be used to remove non-empty directories. In order to delete an empty directory with rm, we need to add '-d' option with it.

rm -d <DIR_NAME>
Remove Directory Rm
Removing a directory using ‘rm’

4. Remove non-empty directories using rm command

To remove non-empty directories '-r' option must be used.

rm -r <DIR_NAME>
Remove Directory Non Empty
Removing non-empty directory

The '-r' option stands for recursive removal of files and directories inside the specified directory. In the above snippet, ls command is used to display the contents of the directory to be removed.

Note: If we wish to remove multiple directories using rm command, then we can add them in the command separated by a space like: 'rm -r <DIR1_NAME> <DIR2_NAME> <DIR3_NAME>'.

5. Forced removal of a directory

It may happen that some files require permission to remove them, therefore a prompt is shown for confirmation for removal of files. It can be easily bypassed using '-f' option.

rm -rf <DIR_NAME>
Remove Directory Force
Forced Removal of a directory

The explanation of steps is as follows:

  1. Create a directory using mkdir command.
  2. Create an empty file inside the directory using touch command.
  3. Remove write permissions from the empty file using chmod command.
  4. Check the permissions of the file using ls command.
  5. Try to remove the directory using the standard removal procedure, but shows a prompt.
  6. Forced removal of directory using '-f' option.

Note: Similar to rmdir, the directories and files deleted by rm are deleted permanently.

6. Interactive removal of a directory

Sometimes we are unsure of the exact contents of a directory, therefore we can have a prompt before removing any item of a directory. This can be achieved by using '-i' option along with the rm command.

rm -ri <DIR_NAME>
Remove Directory Interactive
Remove directory using ‘-i’ option

In the above figure, we first create a folder with three files in it. Then we do an interactive removal of the newly-created directory. As it is clear from the figure, the command keeps on asking for confirmation during each step of removal. Pressing 'y' after the question executes the statement, whereas pressing 'n' denies the removal.

If there are a huge number of files and directories then using the '-I' option reduces the number of prompts to only major events.

rm -rI <DIR_NAME>
Remove Directory Interactive I
Remove directory using ‘-I’ option

7. Removing multiple folders using expressions

Using the wildcard symbols, '*' and '?', the rm command can match directories and remove them one by one. The '*' symbol matches any number of characters, whereas the '?' symbol matches only a single character.

Remove Directory Wildcard
Removing directories using ‘?’

The above command 'rm -r folder?' matches all the three directories and removes them.

Remove Directory Wildcard1
Remove directories using ‘*’

The above command 'rm -ri folder*' matches all the directories starting with ‘folder’.


Removing directories using the Graphical User Interface might be tedious if there are numerous nested directories. The rm and rmdir commands are a necessity for every Linux-user. Feel free to comment below for any queries or feedback.