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>
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>
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>
4. Remove non-empty directories using rm command
To remove non-empty directories '-r'
option must be used.
rm -r <DIR_NAME>
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>
The explanation of steps is as follows:
- Create a directory using mkdir command.
- Create an empty file inside the directory using touch command.
- Remove write permissions from the empty file using chmod command.
- Check the permissions of the file using
ls
command. - Try to remove the directory using the standard removal procedure, but shows a prompt.
- Forced removal of directory using
'-f'
option.
Note: Similar to
rmdir
, the directories and files deleted byrm
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>
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>
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.
The above command 'rm -r folder?'
matches all the three directories and removes them.
The above command 'rm -ri folder*'
matches all the directories starting with ‘folder’.
Conclusion
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.