In Linux, removing directories is a straightforward task, but what if the directory is not empty? It can be challenging sometimes to delete a directory as the system tends to prevent it by default. Moreover, the output could have some other errors even if you are using the correct command syntax. In this article, I will discuss how you can remove a non-empty directory in Linux using CLI & GUI. Also, I will explain some possible causes that may create failures in removing a non-empty directory & give solutions with examples.
Key Takeaways
- Learning to remove non-empty directories using both CLI & GUI.
- Getting solutions for the problems that may cause the failure in removing non-empty directories.
Requirements
- To change or remove a directory in Ubuntu, you must have root/sudo privileges.
- Also, you need the necessary permissions to modify a directory along with its contained files & sub-folders to remove it.
Process Flow Chart
[Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS]
Downloads
Removing a directory using CLI is a permanent action. For practice purposes, you can either create a test directory or download the one I used in this article:
Download the contents used in this ArticleWatch 2 Methods to Remove Non-Empty Directory in Linux
2 Methods to Remove Non-Empty Directory in Linux
In Linux, deleting an empty directory is an easy task using the rmdir command. However, if you try to remove a non-empty directory using that command it shows the following error: In the following article, I will discuss how you can delete a directory that is not empty using both Command Line Interface (CLI) & Graphical User Interface (GUI).
Anyway, you can read our Comparative Analysis of Methods to distinguish between these two methods and best pick one for your need.
Method 01: Using Terminal to Remove Non-Empty Directory in Linux
In Linux, you can easily & quickly remove any non-empty directory using the Command Line Interface (CLI). The command that removes a non-empty directory recursively is the rm command with the command option –recursive.
Moreover, you can use some other command options to modify the directory removal. Read the following examples for detailed information on the cases.
Example 01: Using the rm Command to Recursively Remove Non-empty Directory in Linux
You can simply remove any directory that’s not empty along with its contents using the rm (stands for remove) command with the command option –recursive or -r. Follow the below steps to remove a non-empty directory.
Removing any non-empty directory using CLI will permanently remove that directory along with all of its contents from your system. So be careful while executing the command, and check before removing.
Steps to Follow >
➊ At first, open your Ubuntu terminal application.
➋ Then, type the following command in the command prompt to view the long list of contents in the home directory:
ls -l
- ls: Lists directory contents
- -l or –long: Option of the ls command where -l gives long lists.
➌ Press the ENTER key. The command output is listing out all the directories present in my system.
➍ Afterward, let’s dive inside the directory you want to delete & view its contents:
tree schedule
- tree: Prints directory contents recursively in-depth tree-like format.
- schedule: One of the system directory names.
➎ Next, search for the ‘directory’ that you want to remove, in this case, I will remove the ‘schedule’ directory:
ls -l | grep schedule
- grep: Looks for a specific pattern given to it as an argument.
- schedule: One of the system directory names.
➏ Subsequently, type the following command to remove the directory & all of its contents:
rm -r schedule
- rm: Removes files & directories in Linux.
- –recursive or -r: Command option to recursively remove directory & its contents.
- schedule: One of the system directory names.
➐ Press the ENTER button.
➑ At this point, check if the directory has been removed successfully by searching the directory again:
ls -l | grep schedule
➒ Finally, press the ENTER key again.
First, the grep command searches & prints the ‘schedule’ directory information. Next, the rm command removes the entire directory. Subsequently, you can check if the directory has been removed successfully by running the grep command again to search for its name. Since you can see the command is returning an empty command prompt, that means it is no longer available on your system.
Read More: 2 Cases of Recursive Remove of Directory in Linux
Example 02: Use the rm Command to Interactively Remove a Non-Empty Directory in Linux
So far, you now know that the rm command permanently removes an entire directory. As a result, sometimes it is a bit risky to use the command. Moreover, you may not want to remove all files & sub-directories. In that case, you can use the command with the interactive option. This will interact with each action before executing them & will ask for confirmation. Go through the following steps to interactively delete a directory:
Steps to Follow >
➊ First, open your Ubuntu terminal application.
➋ Then, type the following command in the command prompt to interactively remove the directory along with each & every file & sub-directory of it:
rm -ri schedule
- rm: Removes files & directories in Linux.
- –recursive or -r & –interactive or -i: –recursive or -r: Command options to recursively & interactively remove directory & its contents.
- schedule: One of the system directory names.
➌ Tap the ENTER key.
- At first, the command asks whether it will enter inside the directory.
- Next, It asks for permission before deleting each & every file & sub-directory by mentioning its name. You can type ‘Y’ for yes & ‘N’ for no.
- Moreover, the command asks for confirmation to even descend into the sub-directory. You can skip any interaction just by pressing the ENTER button & the command will skip that action & move to the next one.
- Finally, if you think you don’t need to delete any other files or just want to end the command execution, just type ‘CTRL+C’ & a prompt will appear.
Read More: 3 Ways to Remove Write Protected Regular File Using the rm command
Example 03: Print the rm Command Output in Verbose Mode
In this case, when you don’t want any command interaction but the detailed output messages about what the command is doing, you can use the rm command option –verbose or -v. Act according to the following steps to check:
Steps to Follow >
➊ Start by opening your Ubuntu terminal application.
➋ Then, type the following command in the command prompt to remove the directory along with each & every file & sub-directory of it & print the verbose output of command actions:
rm -vr schedule
- rm: Removes files & directories in Linux.
- –verbose or -v & –recursive or -r: Command options to recursively remove the directory & its contents & print the verbose
- schedule: One of the system directory names.
➌ Finally, hit the ENTER button. From the output image, see the command is displaying the output message of each action after executing & performing.
Method 02: Using GUI to Remove Non-Empty Directory in Linux
When you are not sure which command to use or getting unexpected errors while using CLI to remove a directory recursively that’s not empty, you can always use the Graphical User Interface (GUI). Even though it could be a little more time-consuming, it is handy for beginners without CLI knowledge.
Steps to Follow >
➊ First, go to your file manager. Select the directory you want to delete along with its contents. Next, right-click on it & select the ‘Move to Trash’ option from the menu. And the directory will be deleted. Check the following image, where I am deleting my “schedule” directory. ➋ Afterward, you can check the deleted directory in the “Trash” folder. As you can see from the following image, I have my deleted “schedule” directory & it’s listed inside the trash folder.
With these two simple steps, you can remove any directory recursively on your system using GUI. Moreover, you do not need any separate task to confirm directory deletion as it is visible in the trash folder that you can see simply by using GUI.
Comparative Analysis of Methods
In this article, I showed you how to remove a directory using GUI & CLI. Anyway, check the following table where I have comparatively analyzed the pros & cons of both methods.
Methods | Pros | Cons |
---|---|---|
Method 1 |
|
|
Method 2 |
|
|
In summary, both methods have their pros and cons. If you have good CLI knowledge and feel more comfortable using it over GUI, use method 1. On the other hand, if you are unsure about some commands or facing unknown errors, use method 2.
- Remove All Files from Current Directory in Linux [2 Methods]
- How to Remove a User and Home Directory in Linux [2 Methods]
Complementary Information
Besides, knowing how to remove non-empty directories recursively, you will find the below information helpful.
Solutions For “Failed to Remove a Directory Not-Empty” in Linux
There could be several reasons for not being able to remove a non-empty directory using the rm command with the –recursive option. For instance,
A. Lack of Permissions to Remove a Non-Empty Directory
Sometimes when you try to remove a read-only directory the command output shows an error. See the following image as an example, where I tried to remove a write-protected directory. As a solution, you can change the permission of the directory using the chmod command. Check out the following picture:
After changing the permissions, the non-empty directory is removable.
B. Not an Owner of the Non-Empty Directory
When you try to remove a directory that you are not the owner of, for instance, belongs to another user. There will be an error. In that case, if you are a member of the sudo group, then use the sudo command before the rm command. Like this: The sudo command grants superuser privileges & let you remove a directory of another user.
C. Incorrect Directory Location Path
While you are removing a directory that is inside your current working directory, you don’t need to mention the directory location path. However, when the directory is situated in another directory, in that case, give the current location path as an argument with the rm command.
D. The Directory that is Currently Being Used
In cases, where you are trying to remove a directory that is open somewhere or being used by any program in your system, won’t be removed. Such as mounted directories. Hence, before removing the directory, try to unmount it first. Also, make sure to close all the opened tabs that are using the directory.
From the image, you can see, after unmounting the directory, I am able to remove it.
E. Hidden Files Inside the Directory that is Being Used
Another problem could be engaged hidden files. You can check all files including hidden ones using the ‘ls -la’ command & which program is using them with the lsof command.
How to Remove an Empty Directory in Ubuntu
Along with the rmdir command, you can also use the rm command to remove an empty directory. For that, just use the command option –dir or -d. Check out the following steps to see how it works.
Steps to Follow >
➊ At first, open your Ubuntu terminal application.
➋ Then, type the following command in the command prompt to view the empty directory:
tree null
- tree: Prints directory contents recursively in-depth tree-like format.
- null: One of the system directory names.
➌ Now, tap the ENTER key & type the below command to remove the empty directory:
rm --dir null
- rm: Removes files & directories in Linux.
- –dir: Command option that removes the empty directories.
- null: One of the system directory names.
➍ Press ENTER & afterward search for the deleted directory t check the succession of directory deletion:
ls -l | grep null
- ls: Lists directory contents
- -l or –long: Option of the ls command where -l gives long lists.
- Pipe (|): Redirects the standard output.
- grep: Looks for a specific pattern given to it as an argument.
- null: One of the system directory names.
➎ Finally, hit the ENTER button again. First, the tree command is showing inside the ‘null’ directory, which is empty. Next, the rm command removes the empty directory. Since it has been removed, therefore the grep command can not find the directory in the database & returns a new command prompt.
Read More: How to Find and Delete Directory in Linux [3 Methods]
Conclusion
To sum up, removing a non-empty directory in Linux can be a tricky task, but with the right command and command options, it can be done safely & efficiently. Throughout the article, I discussed some possible ways, you can use to remove non-empty as well as empty directories from your system. Hope this guide helps you!
People Also Ask
How do I force delete a directory in Linux?
How do I delete a non empty directory in the command prompt?
What is the function to remove a non empty directory?
How do I find non empty files in a directory Linux?
Why I Cannot remove a directory in Linux?
Why can't you remove the directory Linux?
How to delete directory in Linux using find command?
How to remove write protected file in Linux?
Related Articles
Great work, please keep it up
Thank you!