How to Remove a Directory in Linux
Posted on
System administrators should become proficient with the Linux command line. Linux commands are a more effective way to handle servers than hosting control panels.
Managing the system’s data is one of the many fundamental jobs for which these commands come in handy.
If you use the GUI, removing a directory in Linux is a rather easy job. On the other hand, terminal commands can also be used to remove directories if you are not able to use the GUI.
This guide will teach you how to use the terminal window or command line to remove a directory in Linux.
Required conditions
- device with a Linux distribution installed.
- account that has sudo access.
- ability to use the command line or terminal window.
How can I delete a directory in Linux?
There are two Linux commands available for removing a directory from the terminal window or command line:
- The rm command is capable of removing entire directories, along with any subdirectories and files contained within.
- The rmdir command is used to delete empty directories.
It’s worth mentioning that the rm and rmdir commands delete directories permanently without moving them to the Trash directory. It is not possible to recover a directory that has been deleted using these commands.
NB: Keep in mind that while rm and rmdir can permanently delete files and directories, it is still possible for skilled users to potentially recover some of the deleted files.
How to Delete a Directory in Linux Utilising the rm Command
The rm command in Linux is used to delete files and directories.
The syntax employed is as follows:
rm [options] [name of the file or directory]
Important: When using the rm command to delete multiple files or directories, make sure to include all the file or directory names, separated by spaces.
Here are some of the options available for the rm command:
- -f: Forces the removal of all files or directories.
- -i: Prompts for confirmation before removing.
- -I: Prompts once before removing more than three files or when removing recursively.
- -r: Removes directories and their content recursively.
- -d: Removes empty directories.
- -v: Provides a verbose output.
- –help: Displays the help text.
- –version: Displays the command version.
If you attempt to use the rm command without any options to delete a directory, you will receive an error message:
When attempting to delete a directory using the rm command without any options, an error message will appear.
To remove an empty directory, simply include the -d flag when using the rm command:
rm -d Example
Just a heads up: In case you need to delete a directory that begins with a hyphen (-), you can use either the rm — [directory name] or rm ./[directory name] syntax.
Here’s an example that demonstrates how the rm command, when used with the -d flag, can remove the Example directory:
Deleting an empty directory using the rm command
To delete a directory that contains subdirectories and files, you can use the -r flag.
Here is a visual representation of the tree structure of the Example directory. It includes Dir1 and Dir2 subdirectories, each containing several text files:
A demonstration of a directory and file hierarchy
When the -r flag is used, the entire directory, along with its subdirectories and files, is removed. On the other hand, the -v flag provides a detailed output by listing each step of the process.
Delete the “Example” directory and display the process:
rm -r -v Example
When using the -i option, a prompt will appear asking for confirmation before removing the directory. Please confirm by typing Y and pressing Enter.
Delete the directory named “Example” with confirmation:
rm -d -i Example
When a directory is removed, a prompt will be displayed as the output.
Deleting files from write-protected directories prompts for user confirmation. Set up a directory using the following steps:
Create a new directory called “Example” using the sudo command:
sudo mkdir Example
To delete the directory, use:
Delete the directory named “Example”:
rm -d Example
Delete directory Protected directory detected. Please confirm deletion by typing Y and pressing Enter. To bypass the confirmation, utilise the -f flag or elevate the command privileges to sudo:
Delete the “Example” directory forcefully:
rm -d -f Example
Delete the Example directory using the sudo rm -d command:
sudo rm -d Example
To handle a write-protected directory that contains additional files and directories, you can utilise the following command:
Delete the specified directory and all its contents:
rm -rf <directory name>
Caution: Executing the command “rm -rf /” on a Linux system can have severe consequences as it initiates a recursive deletion of the root directory, resulting in the system becoming inoperable.
It is advisable to exercise caution when removing directories and avoid using the -f and sudo options unless you are familiar with their implications.
Deleting a directory in Linux Utilising the rmdir Command
The rmdir command in Linux is specifically designed to delete directories that are empty. The command utilises the following syntax:
rmdir [options] [folder name]
The rmdir command has a variety of options available:
- –ignore-fail-on-non-empty: Doesn’t show an error message when trying to remove a non-empty directory.
- -p: Removes the directory along with its parent in the hierarchy.
- -v: Provides a verbose output.
- –help: Displays help text.
- –version: Displays the command version.
When attempting to use the rmdir command on a directory that is not empty, an error will be generated.
When attempting to remove a directory that is not empty, the rmdir command will not work.
In this scenario, the Example directory includes the Test subdirectory:
Displaying the structure of the Example directory
To delete these directories using the rmdir command, make sure to add them in the opposite order of their hierarchy. The -v option will display each step of the process as the output.
Remove the directory “Example/Test Example” and display a verbose output.
Deleting multiple directories using the rmdir command:
rmdir -v Example/Test Example
Another way to accomplish this task is by utilising the -p option alongside the name of the subdirectory. This deletes both the subdirectory and its hierarchical parent.
Delete the directory “Example/Test” and display the process and results:
rmdir -p -v Example/Test
With the rmdir command, you can easily delete multiple directories that have similar names by using wildcards. For example, if you need to delete directories named Example1, Example2, and Example3:
Delete the Example* directory and display the process details:
rmdir -v Example*
Employing wildcards in the rmdir command allows for the removal of multiple directories simultaneously.
In summary
In conclusion, removing directories in Linux is a straightforward process that can be accomplished using the command line. The rm and rmdir commands provide users with the necessary tools to delete both empty and non-empty directories. However, exercising caution when using these commands is important, as they permanently delete directories without moving them to the Trash directory. By following the steps outlined in this guide, users can effectively manage their system’s data and maintain a clean and organized file structure. With practice, using the command line to remove directories can become a quick and efficient way to manage directories in Linux.
By following this tutorial, you will gain the necessary skills to effectively delete directories in Linux using commands in the terminal window or command line.