To remove a non-empty directory in Linux, you can use rm -rf directory_name in your command line. You can also delete the non-empty folder using the Graphical User Interface (GUI). However, Removing a directory using the Command Line Interface (CLI) is a permanent action. This article will discuss different methods to remove non-empty directory in Linux. For practice purposes, you can either create a test directory or download the one I used in this article:
Process flow chart to remove a non-empty directory in Linux:
[Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS]
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 Comparison of Methods to distinguish between these two methods and pick one for your needs.
1. Using Terminal to Remove Non-Empty Directory in Linux
To change or remove a directory in Ubuntu, you must have root/sudo privileges. When you have appropriate permission, 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.
A. Recursively Remove Non-empty Directory Using “rm” Command
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:
Warning: 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.
-
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
EXPLANATION- ls: Lists directory contents
- -l or –long: Option of the ls command where -l gives long lists.
-
Press the ENTER key. The command output lists all the directories present in my system.
-
Afterward, let’s dive inside the directory you want to delete & view its contents:
tree schedule
EXPLANATION- 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
EXPLANATION- 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
EXPLANATION- 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.
Output AnalysisFirst, 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.
The tree command presents all directory contents in a tree-like format. You can see there are 2 sub-directories & 10 files inside the ‘schedule’ directory.
B. Interactively Remove a Non-Empty Directory Using “rm” Command
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:
-
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
EXPLANATION- 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.
Output Analysis- 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.
C. Print “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:
-
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
EXPLANATION- rm: Removes files & directories in Linux.
- –recursive or -r & –verbose or -v: Command options to recursively remove the directory & its contents & print the verbose output.
- 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.
2. 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.
To remove non-empty directory in Linux using GUI, please follow the below steps:
-
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.
Comparison of Methods to Remove Non-Empty Directory
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]
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 & lets 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 that after unmounting the directory, I can 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:
- First, open your Ubuntu terminal application.
- Then, type the following command in the command prompt to view the empty directory:
tree null
EXPLANATION- 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
EXPLANATION- 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
EXPLANATION- 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.
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?
In Linux, to force delete a directory use the rm command with the command options –recursively or -r
& –force or -f
. Hence, use the command syntax rm -rf /location/path/to/directory
.
How do I delete a non empty directory in the command prompt?
To delete a non-empty directory use the rm command with the option –recursively or -r
. It will permanently delete your directory, along with all the directory contents. Therefore, be careful while using the command.
What is the function to remove a non empty directory?
In Linux, the command to remove a non-empty directory is the rm command, which stands for ‘remove’. Use the command option –recursive with the command to delete the directory along with its inside contents, for instance, all files & sub-directories.
How do I find non empty files in a directory Linux?
You can find non-empty files in a directory, using the find command in Linux. Make sure to use command options -type f to specify that you want to search for files, and -size option with added operand +0 to search for files greater than zero. Hence, use the command syntax find /path/to/directory -type f -size +0
.[/su_spoiler]
Why I cannot remove a directory in Linux?
When you have no permission or you are trying to delete a non-empty directory, you will see an error message. Moreover, if the directory is a mount point & is currently being used or is mounted as read-only, could lead to the fact that you can’t remove it. Try to find out the real reason & solve it accordingly.
Why can’t you remove the directory Linux?
There could be several reasons why you are unable to remove a directory in Linux, for instance, no permission, non-empty, the directory is a mount point, the file system is read-only, special characters in the filename, and other processes are currently using files of the directory.
How to delete directory in Linux using find command?
You can delete a directory using the find command in combination with the rm command. For that, search the directory path & then pipe or use the exec command to send the output to the rm command to execute. Hence, use the command syntax, find /path/to/directory -depth -type d -name ‘directory-name’ -exec rm -r {} /;
.
How to remove write protected file in Linux?
To delete a write-protected directory, first, you have to change the directory permission. You can do that using the sudo chmod a+w /path/to/directory
command syntax. Then use the rm command with the –recursive option to remove it. If it still fails then use the –force option too. The full command syntax is rm -rf /path/to/directory
.
Related Articles
- How to Force Remove Directory in Linux? [Step-by-Step]
- Remove All Files in a Directory with the Prompt in Linux
- How to Undelete Folder in Ubuntu? [With Solutions]
- 3 Ways to Remove All Files in a Linux Directory
FUNDAMENTALS A Complete Guide for Beginners
Great work, please keep it up
Thank you!