Unix / Linux: Remove Nonempty Directory Command

last updated in Categories ,

I am trying to remove a directory called /opt/source/httpd with the rm command. I am greeted with the following message:

rmdir: failed to remove `/opt/source/httpd’: Directory not empty

How do I delete directory called /opt/source/httpd under Linux or Unix operating systems?

The rmdir command Remove the DIRECTORY(ies), if they are empty. The syntax is:

rmdir dirName
rmdir [optipns] dirName

The rmdir utility removes the directory entry specified by each directory argument, provided it is empty. To remove non-empty directory use the rm command as follows (warning: the following command can delete all the files and its subdirectories specified by each directory argument)

rm -rf /path/to/dir
rm -rf /opt/source/httpd

If you can not use rm -rf command, try the following commands:

cd /opt/source/httpd
### Delete all files from all directories ### 
find . -name \* -type f -exec rm {} \;
### Delete all sub-directories ###
find . -name \* -type d -exec rmdir {} \;
### Finally delete /opt/source/httpd ###
cd ~ 
rmdir /opt/source/httpd

Posted by: Vivek Gite
