Unix / Linux: Remove Nonempty Directory Command

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

ADVERTISEMENTS

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:

Tutorial details
Difficulty Easy (rss)
Root privileges No/Yes
Requirements rm utility
Time N/A
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
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.