Fix: “remove Directory not empty”

In Linux, file management has a key role to play in the smooth functioning of the system. Primarily, the creation and deletion are the dominant operations on directories/files. While deleting a nonempty directory, the user may face an error “remove Directory not empty”. In today’s post, we will demonstrate the possible reasons and solutions to fix the error.

Reason: Using rmdir Utility to Remove the Non-Empty Directory

In Linux, it is an easy process to delete empty directories using the “rmdir” command. If the non-empty directory is tried to remove using the “rmdir” utility, then the following error occurs:

Solution: Use the “rm r” Command

To avoid the error mentioned above, the “rm” utility is used  The syntax of the “rm” command is provided below:

Syntax:

$ rm -r <directory>

In the syntax, the “r” flag shows the recursive nature of the command which ensures the deletion of all the content inside a directory.

For better understanding, a few examples are practiced to remove non-empty directories.

Example 1: Remove a Single Non-Empty Directory

To remove non-empty directories, use the “rm” command with the “r” and “v” options. The “v” option only prints the actions being performed by the command, while the main catalyst is the “r” option (recursive):

$ rm -rv Test-directory

The directory with all its content has been deleted.

Example 2: Remove Multiple Non-Empty Directories

In Linux, users can remove multiple non-empty directories in one command. To remove multiple non-empty directories is obtained as follows:

$ rm -rv Non-Empty-Directory1 Non-Empty-Director2

In our scenario, two directories have been deleted, which can be seen in the above image.

Example 3: Confirm Before Removing the Directory and its Content

To remove a non-empty directory with every directory and files confirmation, use the “i” option with the “rm” command:

$ rm -vri dir1

In the image, Linux will ask you before deleting any files or folders in the directory. Users can give the answers with “y” to delete the file or folder and with “n” to stop deleting the file or folder.

Note: if the user answers “n” on any subdirectory or file, the whole directory will not be deleted. Only those subdirectories or files will be deleted answered with “y

For more details on the “rm” command, click here to read our detailed article on this utility.

Conclusion

The error “remove Directory not empty” occurs while using the “rmdir” command to delete the non-empty directories. To solve this, the command “rm -r” is used. This post has outlined the reason and the solution to fix the error “remove Directory not empty”. For better understanding, various examples are also demonstrated to remove non-empty directories.