How to Delete Directories on Linux
On Linux, there are various command-line utilities used to remove one or multiple directories. The rmdir and rm commands are commonly used to delete directories through CLI. These utilities come with various options to effectively remove directories from Linux.
A directory or folder is a fundamental part of every operating system that is used to store and organize files. They may contain some important data; therefore, caution should be taken before deleting them.
In the following sections, I will cover command line utilities used to delete directories on Linux and how to fix errors you may encounter while deleting them.
Note: When deleting a directory on Linux using the GUI, the directories are automatically moved to the Trash Bin, thereby facilitating the recovery process in the event of an accidental deletion. However, if you are using Linux in CLI mode, then you need to be careful before performing this operation. Moreover, depending on directory permissions, you may need sudo privileges to delete them.
Deleting a Directory on Linux using rmdir Command
The rmdir command line tool is designed to delete the empty directory only. It is a useful command to delete directories when you are not sure about whether a directory is empty or not.
The general syntax to remove a directory using rmdir command is mentioned below.
rmdir [directory/path_of_directory]
For instance, to remove a directory named dir1 from the current working directory, use the command given below.
rmdir dir1
In case the directory has any files or folders inside it, an error will be thrown.
Multiple directories can also be removed with rmdir command. For instance, to delete dir1 and dir2, use the command in the following way.
rmdir dir1 dir2
The -p option is used to remove a child directory and its ancestor directories only if they are empty.
Let’s understand it with an example. In the following example, I am deleting the dir3 directory and I also want to delete its ancestral directories, then I will use the rmdir command with the -p or –parent option.
rmdir -p dir1/dir2/dir3
Running the above command without the -p option will give an error. To get a verbose output use the -v or –verbose option.
The command first deleted the dir3, dir2, and then dir1.
If any of the directories is not empty, then we get the following output.
Because the dir2 is containing a file.
Deleting a Directory on Linux using rm Command
On Linux, you can also delete files and directories using the rm command. The rm command does not delete the directories by default. To delete a directory with rm command option -r or –recursive is used. It essentially deletes the mentioned directories and its content. Some other options that are used to remove the directories using the rm command are listed below.
-f | –force | This option forcefully removes the files or directories without prompting |
-r or -R | –recursive | This option is used to delete directories and their content recursively |
-d | –dir | This option is used to delete the empty directory |
-i | –interactive=always | This option is used to get a prompt before the deletion of each file or directory |
I | –interactive=once | This option is used to get a prompt only once before deleting directories recursively |
-v | –verbose | This option prints the details about the undergoing process in standard output |
The general syntax to remove a directory using the rm command is mentioned below:
rm [options] [directory]
Deleting an Empty Directory
Use the rm command with the -d or –dir option to remove the empty directory. For example, to delete an empty directory dir1, execute the command mentioned below:
rm -d dir1
An error will be printed to the standard output if the directory contains files.
Deleting a Non-Empty Directory & Subdirectories
To delete a non-empty directory with its subdirectories, the -r or –recursive option is used with the rm command directory path.
Let’s take the example of dir1, which contains a file and two more directories, dir2 and dir3.
To delete the directory dir1 with its content use the command given below:
rm -r dir1
The rm command first removed the dir3, dir2 then file.txt and eventually the dir1.
Note: Use this option carefully, the directories deleted using these commands cannot be recovered.
Deleting Multiple Directories
To delete multiple directories, the rm command will be used with the -r option and the names of the directories. For instance, to delete dir1, dir2, and dir3 in the current working directory.
rm -r dir1 dir2 dir3
Note that, the r in the command given above stands for recursive.
Deleting Directories with Prompt
The rm command should be used cautiously because the files and directories cannot be recovered once they are deleted. The rm utility offered two options to prompt you about the files or directories that are about to be deleted. The first one is -i which prompts the deletion of each piece of content of the directory.
Let’s use the -i option to recursively delete a directory dir1 and its content.
rm -r -i dir1
You will be asked to descend to the last directory in the directory tree and then remove each directory and file with prompts. Type yes and no to proceed with the process.
The other option is -I, which is also used to print prompts before deleting the directories recursively.
rm -r -I dir1
The only difference between the -i and the -I is that the -I option will prompt only once, while -i prompts on each action.
The -I option is less instructive, but still a good option for securely deleting the directories.
How to Fix /bin/rm: cannot execute [Argument list too long] Error
If you use the rm command to remove a lot of files and directories in a directory, you might get an argument too long error. This happens because the rm command can only have a limited number of arguments, and exceeding this limit causes this error.
To fix, the argument too long error, we need to delete all the files in that directory using the find command.
Assume the dir1 is giving this error. Use the find command to delete all the files in the dir1 directory and its child directories.
find dir1 -type f -delete
The command above finds and deletes all the files in the parent directory and its subdirectories. The -type f indicates the file type and while -delete option is deleting them.
Now, all the files are deleted, and only directories remain. To remove those directories and subdirectories, use the rm command.
rm -r dir1
The above process can also be done in one step using the && operator.
find dir1 -type f -delete && rm -r dir1
Deleting a Directory on Linux using trash Command
The trash is a third-party command line utility used to delete files and directories and move them to trash. The trash utility is the CLI version of the graphical trash folder. This tool essentially remembers the name and the path of the file and directory.
Deleting directories using the trash utility is quite a safe way. Any directory or files deleted by the trash command can be restored. It does not come by default on Linux, and it has to be installed.
Use the command given below to install the trash-cli on Ubuntu and Debian.
sudo apt install trash-cli
For Arch Linux and the distributions based on Arch.
sudo pacman -S trash-cli
For the latest Fedora, CentOS, and RHEL.
sudo dnf install trash-cli
To delete a directory, use the name or path of the directory with the trash command.
trash dir1
To list the deleted files and directories.
trash-list
To restore the deleted directory.
trash-restore
The command will list all the deleted files and directories with numbers assigned to them. Type the number of the directory to restore.
Conclusion
Deleting directories on Linux should always be performed with caution because the action is irreversible. To delete an empty directory, use the rmdir command, this is a safe approach because if the directory is not empty it will prompt in the standard output.
With the -r option, the rm command can be used to delete a directory or directories. To securely delete the directories, I would recommend using the rm -r command with the -i option, so you get a prompt on each action the command performs to delete the directories.