6 Methods to Remove a Directory in Linux (+Safety Tips)
Removing a directory in Linux is one of the most basic tasks, but it is important for system management, maintenance, and organization. For a system administrator, developer, or even an average user, knowing how to delete directories efficiently is critical for keeping a system clean and optimized. However, it’s essential to do it correctly to avoid accidental data loss.
This tutorial will explore all possible methods to remove a directory in Linux, including basic commands, advanced options, and safety tips.
How to Remove a Directory in Linux
Method 4: rm Command with Wildcards
Safety Tips for Removing Directories
How to Remove a Directory in Linux
Whether you’re freeing up space, organizing files, or ensuring compliance, knowing how to delete directories correctly is a valuable task for any Linux user. Let’s explore methods one by one:
Method 1: rmdir Command
The most basic method of deleting an empty directory in Linux is with the rmdir command. It works only in case the directory contains neither files nor subdirectories.
Syntax:
rmdir [directory_name]
Example 1:
Consider an empty folder called example_dir. The rmdir command with one argument will remove the directory:
rmdir example_dir
Example 2:
For example, if there are three empty directories known as dir1, dir2 and dir3 then the following command can be typed:
rmdir dir1 dir2 dir3
Key Points:
- Use with caution: The rmdir command will only work in these specific situations and does not work in directories: If the main directory has files or subdirectories, it will fail.
- Error Handling: In this case, if the directory is not empty, you would see something like an error rmdir: failed to remove ‘example_dir’: Directory not empty.
Method 2: rm Command
The rm command can remove non-empty directories as well as files. It is the command that is most frequently used for directory deletion.
Syntax:
rm -r [directory_name]
Example 1:
To delete a folder called example_dir along with everything inside it:
rm -r example_dir
Example 2:
To remove multiple directories and their contents:
rm -r dir1 dir2 dir3
Options:
- -r (Recursive): Deletes a folder and everything inside it.
- -f (Force): Deletions are carried out with no verification prompt.
- -v (Verbose): Displays step-by-step removal of files.
Example with Options:
To forcefully and recursively remove a directory:
rm -rf example_dir
Key Points:
- Use with caution: be careful when using the removal of files because using the -f option can result in losing files by accident.
- Recursive Deletion: you will need to use -r to delete folders that have files in them.
Method 3: find Command
The find command helps delete folders in bulk. When you know the specifics about the folder such as its name, size, or the last time it was modified the find command is helpful.
Syntax:
find [path] -type d -name [directory_name] -exec rm -r {} +
Example 1:
To delete every directory called example_dir in the /home/ubuntu/collection ending:
find /home/ubuntu/collection -type d -name “example_dir” -exec rm -r {} +
Example 2:
To delete every empty directory within a designated path:
find /home/ubuntu/collection -type d -empty -exec rmdir {} +
Key Points:
- Flexible: Able to direct the deletion to certain directories depending on multiple factors.
- Safe: Gives the chance to look at the directories before deleting them.
Method 4: rm Command with Wildcards
Using a wildcard is useful when you need to delete a lot of directories very quickly. Wildcards, when used, can delete an unlimited number of directories in a single action.
Syntax:
rm -r [pattern]
Example 1:
To delete every directory which begins with example_:
rm -r example_*
Example 2:
To delete every directory which ends with _backup:
rm -r *_backup
Key Points:
- Use with caution: Wildcards can and will match more directories than targeted.
- Double-Check: Always check the directories you are about to run the command on.
Method 5: trash-cli Command
The trash-cli command moves folders to Trash rather than deleting them straight away. This aids with safety.
Installation:
sudo apt install trash-cli
Syntax:
trash-put [directory_name]
Example 1:
Moving a directory example_dir to the trash:
trash-put example_dir
Example 2:
Moving a few directories to the trash:
trash-put dir1 dir2 dir3
Key Points:
- Safe Deletion: Safe deletion means restoration of the deleted directories is possible.
- Cross-Desktop: Works with any desktop environment.
Method 6: GUI File Managers
If you prefer a visual user interface, you can delete files from GUI file managers on most LINUX operating systems (GNOME, KDE etc.).
To do so, start your file manager. Go to the directory that you need to remove/delete. You can then right-click on the directory such as collection and click on Delete or Move to Trash:
Key Points:
- User-Friendly: Useful for the ones that are just starting.
- Reversible: Moving it to the trash makes it possible to recover the directory before it is permanently deleted.
Safety Tips for Removing Directories
- Backup Important Data: Always backup critical files before deleting a directory.
- Double-Check Commands: Ensure the folder name and directory path are correct before using commands that delete files.
- Avoid rm -rf /: This command can delete the entire filesystem, rendering your system unusable.
- Use -i for Interactive Deletion: The -i option with rm prompts for confirmation before each deletion.
rm -ri example_dir
That is all.
Conclusion
In Linux, removing a directory is an easy exercise, but selecting the appropriate approach can be critical. This guide is useful if you opt for the rmdir command on empty folders, use the rm command to remove files with a parent directory, or prefer the graphical file manager’s simpler interface. In addition, this guide provides you with the necessary instructions and precautions to safely and effectively remove directories in Linux.













