How to Remove (Delete) Directory on Linux
Introduction
Before we begin talking about how to remove (delete) a directory on Linux, let's briefly understand – What is a directory?
A directory is an organized database storing information about files and folders. It acts as a roadmap, helping users navigate through their system and locate specific files or folders quickly. When using a desktop file manager to remove a directory, the directory is moved to the Trash and can be readily restored.
When deleting files or directories via the command line, use caution because the directory cannot be entirely recovered once erased using the procedures described in this article. Deleting a directory on most Linux file systems requires writing permission on the directory and its contents. Otherwise, the error "Operation not permitted" will appear. A backslash (\) must be used to escape directory names that contain a space.
In this tutorial, you will remove (delete) a directory on Linux. We will also address a few FAQs on how to remove (delete) a directory on Linux.
Advantages of Directory
- Efficient file organization: Directories help users categorize and arrange files, making it easier to locate and manage data.
- Easy navigation: Users can navigate through their system and access specific files or folders quickly using directories.
- Improved searchability: Directories aid in searching for files by providing a logical structure and grouping related data.
- Enhanced file management: With directories, users can perform tasks like renaming, moving, or deleting multiple files simultaneously.
- Streamlined collaboration: Directories facilitate collaboration by allowing multiple users to access and work on files within a shared folder.
Removing Directories with rmdir
The command-line program rmdir
is used to delete empty directories. It's beneficial when you just want to delete a directory if it's empty, rather than checking whether it's empty or not.
Type rmdir
followed by the name of the directory you wish to remove to delete it. To delete a directory named dir1
, for example, type:
rmdir dir1
If the directory is not empty, the following error will occur:
rmdir: failed to remove 'dir1/': Directory not empty
Before you can delete the directory, you'll need to use the rm
command or manually remove the contents.
Removing Directories with rm
The command-line utility rm
is used to delete files and directories. The rm
command, unlike rmdir
, can delete both empty and non-empty directories.
When used without any options, rm
does not destroy folders by default. Use the -d
(--dir
) option to remove an empty directory, and the -r
(--recursive
or -R
) option to delete a non-empty directory and all of its contents.
To delete a directory named dir1
and all of its contents, for example, type:
rm -r dir1
You will be asked to confirm the deletion of a write-protected directory or file within the directory. Use the -f
option to remove a directory without being prompted:
rm -rf dir1
Use the rm
command, followed by the names of the directories separated by a space, to remove multiple directories at once. The following command will delete all the listed directories and their contents:
rm -r dir1 dir2 dir3
The -i
option causes rm
to ask for confirmation before deleting each subfolder and file. If the directory includes numerous files, you might want to use the -I
option, which will only prompt you once before proceeding with the deletion.
rm -rI dir1
Type y
and press Enter
to remove the directory.
Output
rm: remove 1 argument recursively? y
Regular expansions can also be used to match and remove several folders. For example, you could use the following command to remove any first-level folders in the current directory that end in _bak
:
rm -r *_bak
When eliminating directories, using standard expansions can be dangerous. Before performing the rm
command, it is recommended that you first list the folders with the ls command to check which ones will be destroyed.
Removing Directories with find
find is a command-line utility that searches for files and directories based on a given expression and performs an action on each one that matches.
Using the find
command to delete folders based on a pattern is the most prevalent case. To delete all folders in the current working directory that end in _cache
, for example, run:
find . -type d -name '*_cache' -exec rm -r {} +
Let's look at the command again:
/dir
- Search in the current working directory recursively (.
).-type d
- The search is limited to directories.-name '*_cache'
- Only look for directories that end with_cache
.-exec
- Performs an external command, in this caserm -r
, with optional arguments.{} +
- The discovered files are appended to the conclusion of therm
command.
Removing all empty directories
To get rid of all empty folders in a directory tree, type:
find /dir -type d -empty -delete
The following is an explanation of the options:
/dir
- Search the/dir
directory recursively.-type d
- The search is limited to directories.-empty
- Limits the search to empty folders only.-delete
- Deletes all found empty directories in the subtree.-delete
can delete only empty directories.
With extreme caution, use the -delete
option. When you add the -delete
option to the search command line, it will erase everything below the beginning points you set.
Always run the command without the -delete
option first, then add it as a last option.
/bin/rm: Argument list too long
When you run the rm
command to remove a directory with a large number of files, this error message occurs. This occurs when the number of files exceeds the command line argument size limit set by the system.
This problem can be solved in a variety of ways. For example, you can cd
to the directory and remove sub-directories one by one manually or using a loop
.
The simplest solution is to use the find
command to delete all files in the directory before deleting the directory:
find /dir -type f -delete && rm -r /dir
FAQs to Remove (Delete) Directory on Linux
Can I recover a deleted directory on Linux?
No, once a directory is deleted, it is permanently removed from the system. It is recommended to have backup systems in place to mitigate data loss.
Are there any precautions I should take before removing a directory?
Verify the directory and its content before removing it, as the action cannot be undone. Ensure you have a backup if there is any important data stored within.
What if the directory I want to remove is not empty?
For directories with content, use the "rm -r" command to recursively remove the directory and its contents.
How can I remove a directory and its content forcefully?
To remove a directory and all its content without asking for confirmation, use the "rm -rf" command, but exercise caution as this action is irreversible.
Can I remove multiple directories at once?
Yes, you can remove multiple directories simultaneously by specifying their names separated by spaces after the "rm -r" or "rm -rf" command.
Is there a way to remove a directory and its content but keep certain files?
Yes, you can exclude specific files from removal by using the "--exclude" flag with the "rsync" command.
Can I remove directories with special characters or spaces in their names?
Yes, you can remove directories with special characters or spaces by enclosing their names in quotes ("") or preceding special characters with a backslash () in the command.
Conclusion
With rm
and find
, you may quickly and efficiently delete directories based on various criteria.
Although deleting directories is a basic and straightforward procedure, you must exercise caution to avoid erasing essential data.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.