In Linux, how do I remove a directory? This is a typical query from Linux novices. It's certain that as you get used to using the Linux operating system, you'll wish to delete a directory.
If you use the GUI, removing a directory in Linux is a rather straightforward process. However, you can also delete directories via terminal commands if you don't have access to the GUI.
In this article, we'll demonstrate how to use the command line or terminal window to remove a directory in Linux.
Although deleting directories is straightforward, you must not destroy any crucial data.
Before you start, you need a machine that runs a Linux distribution, a user account with sudo rights, and access to the command line or terminal window.
Additionally, if you want to learn more about removing directories in Linux and the various methods involved, you can check out our detailed guide on how to remove a directory in Linux.
How delete directory in linux?
How to use the rm command to delete a directory in Linux?
In Linux, the rm command is used to remove files and directories. This command can be used to completely delete a directory, which deletes the directory together with all of its files and subdirectories.
The syntax for this command is as follows:
rm [options] [files and/or directories]
Add several file or directory names, separated by blank spaces, to the rm command to remove numerous files or directories.
Don’t forget that an error message appears when attempting to remove a directory using the rm command without any parameters.
The rm command can be modified by adding the -d parameter to remove an empty directory:
rm -d Example
To remove a directory with files and subdirectories, use the -r flag.
When the -v flag is used, the output describes each stage of the operation, whereas the -r flag deletes the entire directory, including subdirectories and files:
rm -r -v Example
A prompt requesting you to confirm directory removal is displayed when you use the -i option. To confirm, enter Y and then hit Enter.
rm -d -i Example
When removed, write-protected directories demand user input. Make such a directory by using:
sudo mkdir Example
To delete a directory in Linux, use the following:
rm -d Example
To confirm the deletion, enter Y and then hit Enter. Use the -f switch or increase the command privileges to sudo to avoid the confirmation:
rm -d -f Example
sudo rm -d Example
Use the following command if there are other files and directories in the write-protected directory:
rm -rf <directory name>
rm -rf / makes your system unusable by forcing a recursive deletion on the root directory.
When removing folders, avoid using sudo and -f unless you are sure what you are doing.
Removing a directory in Linux using rmdir command
Empty directories can be removed with the rmdir command:
rmdir [options] [directory name]
The command equals rm with the -d flag: rm -d.
You receive the following error when using rmdir on a non-empty directory:
RMDIR: [folder]: Not an empty directory.
Use this command without any parameters to remove an empty directory:
rmdir test
The -p flag of the rmdir command enables you to remove a directory with its parent in the tree. For instance, imagine your file system looks like this:
> Test
---> Test22
The Test2 subfolder is located in the directory Test. The test becomes an empty directory if the Test2 directory is removed. Thus as opposed to:
rmdir Test/Test2 Test
# deleting Test2 and then Test
Use the -p flag as follows:
rmdir -p Test/Test2
With this command, Test2 and its parent, Test, will be deleted from the tree. But if either directory is not empty, this command will fail.
For more details on how to efficiently copy directories in Linux, you can refer to our guide on copy-directory-linux.
Removing a directory in Linux with find
With the help of the command-line tool named find, you may look for directories based on an expression and take action on each matching directory.
The most typical scenario is to delete directories based on a pattern using the find command.
Run the following command, for instance, to remove all folders in the current working directory that finish in the character _cache:
find . -type d -name '*_cache' -exec rm -r {} +
You can run the following to clear every empty directory in a directory tree:
find /dir -type d -empty –delete
Be careful when you decide to use the -delete option. If the -delete option is added first, the search command will erase everything below the beginning points you specified. The find command line is processed as an expression.
Use -delete as the last option and always test the command without it.
Use -delete as the last option and always test the command without it. If you accidentally delete important files, don't worry—there are ways to recover deleted files in Linux. Check out our guide on how to recover deleted files in Linux to ensure you can retrieve lost data safely.
Conclusion
After reading this guide, you should be able to use the terminal window or command line to delete a directory in Linux.
Linux allows you to remove directories using the commands rm and rmdir. Non-empty folders are deleted by rm. Empty directories are removed by the rmdir command. However, this command cannot delete a directory with files inside of it.
When you want to delete a folder, you might be used to dragging it to the trash on your computer. You can store the documents you're about to destroy in the trash. You have to empty your trash can before you remove the file.
However, you should be aware that there is no trash can on the Linux command line. After removing a directory in Linux, it’s permanently erased. This is particularly important when working with a Linux VPS. On a [Linux VPS], you might be managing critical directories, and accidental deletions could lead to significant data loss. Always double-check your commands to ensure you are deleting the correct directories.
If you’re also working with files and need to delete them individually, it’s important to understand the proper commands for removing files as well. For detailed steps on how to safely delete a file in Linux, you can check out our guide on how to delete a file in Linux.
Advanced FAQ: Mastering Directory Deletion in Linux
1. Can I recover a deleted directory in Linux?
Recovery is challenging as Linux does not have a built-in trash bin. You might need specialized recovery tools, but success isn't guaranteed.
2. How to delete directories with special characters in their names?
Use escape characters (\) before the special character or enclose the name in quotes.
3. What are common mistakes to avoid when deleting directories in Linux?
Avoid using rm -rf without proper checks, deleting system directories, or operating without backups.
4. How do I delete directories based on their age or last modified date?
Use the find command with -mtime option and -exec rm -rf {} \; to delete based on modification time.
5. Are there any GUI tools for safely deleting directories in Linux?
Most Linux desktop environments have file managers with graphical interfaces for safe deletion.
6. Can I use wildcards to delete multiple directories simultaneously?
Yes, use wildcards like * with rm -r to match and delete multiple directories.
7. How do I check disk space before and after deleting large directories?
Use the df command to check disk space before and after deletion.
8. What are the implications of deleting system directories?
Deleting system directories can lead to system instability or failure. Only proceed if you're sure of the directory's role.
9. How to schedule a directory deletion at a later time?
Use the at or cron job scheduler to execute a deletion command at a specific time.
10. What are the best practices for organizing directories to make deletion easier and safer?
Keep a clear structure, use descriptive names, and regularly backup important data.