Although you might rarely do it, deleting directories in a Linux system is an important step in ensuring that the system remains in order and functional. With proper deletion of directories, you might avoid errors and protect your data, not to mention maintaining the integrity of your system. Get to know why and how to remove a directory in Linux using the `rmdir
` and `rm
` commands through this article.
Get to Know Linux Directories
Understanding the definition of a directory is useful before discussing removal methods. In Linux, a directory is just a folder that holds files or other directories. They facilitate effective data organisation. The root directory (/)
is at the top of the hierarchical structure that Linux directories are a part of.
Every directory and file in Linux has attributes, such as permissions and ownership, that determine how one can interact with those directories and files, including removing them.
Basic Commands to Remove a Directory
The command line in Linux is very powerful, and there are several ways how to remove a directory in linux. Below are most common methods:
1. The rmdir Command
A simple technique for eliminating empty folders is the rmdir
command. It is easy to use and safe, but it has restrictions because it only works with folders that are empty of files and subdirectories.
2. The rm
Command
A more flexible method for eliminating files and directories, including ones that hold data, is the rm command. It is not limited to empty folders like rmdir
is.
Step-by-Step Guide on How to Use the rmdir Command
Directories can be scary for many Linux users in the beginning. However, simple commands like rmdir
can help you delete empty directories without much hassle. Here’s a step-by-step guide to help you confidently use this rmdir
command to remove a directory in Linux.
Step 1: Open a Terminal
The first step here is to simply open the Terminal application within your Linux system. You might be able to locate it directly in your application menu or use one of the available keyboard shortcuts such as Ctrl + Alt + T.
Step 2: Navigate to the Directory
To find the directory you wish to delete, use the cd
command. For example, if the directory you wish to delete is located in ~/parent_dir/empty_dir
, change into its parent directory by typing:
cd ~/parent_dir
This puts you in the correct position to run the rmdir
command.
Step 3: Confirm the Contents of the Directory
Before removing the directory, check if it’s truly empty. Use the ls
command followed by the directory name to list its contents without entering it:
ls empty_dir
If the directory is empty, no output will be displayed. If there are files or subdirectories, rmdir
will not work, and you’ll need to either move or delete the contents manually before proceeding.
Step 4: Remove the Directory
Once you are sure that the directory is empty, use the rmdir
command for deletion. For example:
rmdir empty_dir
This will remove the specified empty directory.
Step 5: Deal with Errors (If Any)
If the directory is not empty or if it has been created without having access rights, you may encounter such an error as follows
rmdir: failed to remove 'empty_dir': Directory not empty
In such scenarios, make a directory listing with the ls
command, or use the rm
command to delete non-empty directories but be careful about it.
Step 6: Remove Confirmations
Run the rmdir
command. Verify that the given directory has been successfully deleted. List the parent directory contents using the following ls command:
ls ~/parent_dir
Replace ~/parent_dir
with the path to the parent directory. If the directory has been removed, it will no longer appear in the list.
Example
Let’s walk through a practical example:
- Open the Terminal.
- Navigate to the directory where the empty directory is located:
cd ~/documents/projects
- Check the contents of the directory you want to remove:
ls archive_folder
- If no files or subdirectories are listed, remove the directory:
rmdir archive_folder
- Verify that the directory has been removed:
ls ~/documents/projects
Step-by-Step Guide on How to Use the rm
Command
The rm command in Linux is one of the most powerful commands for deleting files and directories. Unlike rmdir
, it can delete non-empty directories, which makes it very useful for removal tasks of any complexity. But with such power comes responsibility be careful to avoid accidental loss of data.
Here’s how to use rm command the right way to remove a directory in linux.
Step 1: Open a Terminal
Start by opening your Linux-based system’s Terminal application. This can be accessed by using shortcut keys like Ctrl + Alt + T or by using your application menu.
Step 2: Change into Directory
You will need to use the cd
command if you want to access a directory that contains the file or folder you wish to delete. This is what it will look like, assuming the directory is at ~/documents
:
cd ~/documents
This will put you in the right place to run the rm
command.
Step 3: Verify the Contents of the Directory
Before you delete anything, you want to know what’s in the directory. You can list files and subdirectories with the ls
command to remove a directory in linux:
ls
This will give you a clear view of what you are about to delete.
Step 4: Remove a Directory
However, if there are errors, like permission issues, you’ll receive an error message such as:
rm -r dir_to_delete
Put the name of the directory you wish to delete in place of dir_to_delete
.
Step 5: Handle Errors and Permissions
There will be no output if the removal is successful. However, you’ll get an error message like this if there are problems, like permission issues:
rm: cannot remove 'file_name': Permission denied
You might need elevated privileges to remedy this. To execute the command as a superuser, use sudo:
sudo rm -r dir_to_delete
For verification, you will be asked to enter your password.
Step 6: Confirm Deletion
Listing the contents of the parent directory after executing the command will confirm that the directory has been removed:ls
The list should no longer contain the deleted directory.
Example
- Launch the Terminal software.
- If the directory you want to delete is located at
~/downloads/projects
, move to its parent directory:cd ~/downloads
- Before deleting, verify what’s inside the directory:
ls projects
- Once you’re sure, delete the directory and its contents:
rm -r projects
- Confirm the directory has been deleted by checking the contents of the parent directory:
ls ~/downloads
Difference Between rm and rmdir
Though both rm
and rmdir
commands are used to remove a directory in Linux, the two are intended for different purposes and different tasks:-
rmdir: Deleting Empty Directories
- Used to delete only empty directories.
- Cannot remove a directory which has files or subdirectories within it.
- Very useful in rapidly deleting unnecessary directories with nothing inside.
rm: For files and directories
- A general command that can delete files and directories.
- Can remove directories and all their contents, including files and subdirectories, using the recursive option (-r).
- Ideal for handling non-empty directories or files that need to be deleted in bulk.
rmdir
is best used when deleting empty directories, while removing non-empty directories or files would be using rm
. Still, be extremely careful when using rm
to remove a directory in Linux, and especially the recursive option since it permanently deletes files and directories without asking further.
Common Mistakes
- If as all possible, refrain from executing instructions as root: Avoid running removal commands with the higher privileges (sudo) if at all possible. Inadvertent root deletions could seriously impact your system.
- Be Accurate with Paths: When targeting directories, always use the whole path to prevent misidentifying which directory needs to be destroyed.
- Watch Out for Careless Use of Wildcards: Using wildcards could result in the permanent deletion of unwanted files or folders. Therefore, use them with caution and be sure to check their scope in advance.
In conclusion
You can safely and effectively remove a directory in Linux by following these best practices. Check twice, backup important data, and tread with caution when it comes to recursive removals. If you’re automating, then validate your targets, provide logging, and test your scripts. These are the ways of keeping a Linux system clean and tidy while avoiding accidental loss of data.
FAQs
What is the difference between rm and rmdir?
rmdir
: deletes only an empty directoryrm
: can delete both files and directories
Can you undo deleting a directory on Linux?
No. Since rm or rmdir
cannot be undeleted, deleted directories cannot be recovered. One should double-check the directory contents before deleting it and regularly keep backups.
What are the major delete commands for a directory in Linux?
There are two major delete commands for a directory in Linux. They are as follows:
rmdir: Used to remove empty directories.
rm: Used to remove non-empty directories, files, and subdirectories with the recursive option (-r).