Which command is used to remove a non empty directory (Linux)?

Many time on Linux operating system we got a situation where we want to remove some folder but the OS does not allow us. The reason: the directory is not empty that’s why the Linux prompt message “rmdir: ‘dir’: Directory not empty” and could not be get deleted at all.

We generally use rm command to delete files and empty folder, however, the command for deleting full directory will be different.

For example, If I want to delete an XYZ folder on Ubuntu then the command I used will be:

sudo rmdir xyz

But this will not work if the folder is not empty.

So, to remove the directory on Ubuntu or any other Linux that has files or folders, use the below command:

rm -rf {folder name}

In the above command, the folder name has to be replaced with the directory name which you want to be get deleted on Linux.

For example, in the below image you can see that I used the rm -rf command to remove a folder called php. When I had used it without -rf flag it said the deletion couldn’t be possible because the directory contains the file. So, then I used rm -rf and it successfully deleted it. So, the command will be:

rm -rf php

Where php was my folder name and you have to use your directory name that you want to delete.

how to remove full directory with folder in Linux

Note: The above-given command is very powerful when used with root privilege, it will delete all files and subfolders present in a directory without intimating. I mean it will not be going to prompt even once before deleting any file. So, make sure you know what you are doing…

Other Useful Resources:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.