How to remove a non empty directory in Linux

Removing directory in Linux is an easy thing. While trying to remove a directory using a command like rmdir seems to be the only one needed.

In real world the directory in question is not empty. There are some files or other directories inside it. You will get warnings like ‘Directory not empty’ and will not be able to delete the folder.
If you want to remove a directory which is not empty you should use rm command:

rm -r exampledir

where exampledir is the directory you want to remove.  The above command with -r option will remove the exampledir with all its content, both files and directories within. Nevertheless,  it will ask you for permission to delete every single file. To avoid this and speed up the execution use:

rm -rf mydir

The -f option tells the command to delete everything withing the exampledir without asking for confirmation.

 

Leave a Comment

Your email address will not be published. Required fields are marked *