Delete Non-Empty Directory In Linux – “rmdir directory not empty” Solution

The rmdir command is used to remove directories in Linux operating system. But when we try to use the rmdir command to remove the specified directory we get the “rmdir directory not empty” error. In order to remove a non-empty directory, what should I do? Can I use the rmdir in order to remove a directory with some folders or files?

The short answer is yes there is solution about the rmdir directory not emptyy” error but it is not with the rmdir command.

Delete Non-empty Directory with rm Command

rmdir and rm are similar commands but the rm provides more features and capabilities where rmdir only used to delete empty directories or folders. The rm command can be used to delete non-empty directories with some options. The most crucial option to remove a non-empty directory is the -r option which means recursive remove. Recursive remove means delete given directories and subdirectories with their files. Also the -f option can be provided to force the removal of some minor warnings.

rmdir -r -f temporary

Alternatively the -r and -f options can be specified like below.

rmdir -rf temporary

Delete Non-empty Directory with Confirmation

By default the “rm -rf” command deletes or removes the given directory and all its contents without asking any question or confirmation. This can be very harsh in some cases. We can delete files and folders in a more secure way by using confirmations for every deleted file or directory. The -i option is used for confirmation of the given directory and file delete.

rm -rfi nmap-7.91

The output will be like below. We can see that for every removal or deletion a confirmation is required which can be provided with “y” or “yes“.

rm: descend into directory 'nmap-7.91'? y
 rm: remove regular file 'nmap-7.91/FingerPrintResults.cc'? y
 rm: remove regular file 'nmap-7.91/nse_openssl.cc'? y
 rm: remove regular file 'nmap-7.91/NmapOps.cc'? y
 rm: remove regular file 'nmap-7.91/TargetGroup.cc'? y
 rm: remove regular file 'nmap-7.91/nse_nsock.cc'? y
 rm: remove regular file 'nmap-7.91/portreasons.h'? y
 rm: remove regular file 'nmap-7.91/timing.cc'? y
 rm: descend into directory 'nmap-7.91/liblinear'? y
 rm: remove regular file 'nmap-7.91/liblinear/tron.cpp'? y
 rm: remove regular file 'nmap-7.91/liblinear/predict.c'? y
 rm: remove regular file 'nmap-7.91/liblinear/README'? y
 rm: remove regular file 'nmap-7.91/liblinear/train.c'? y
 rm: remove regular file 'nmap-7.91/liblinear/COPYRIGHT'? y
 rm: remove regular file 'nmap-7.91/liblinear/tron.h'? y
 rm: remove regular file 'nmap-7.91/liblinear/Makefile.win'? y
 rm: remove regular file 'nmap-7.91/liblinear/linear.cpp'? y
 rm: remove regular file 'nmap-7.91/liblinear/Makefile'? y
 rm: remove regular file 'nmap-7.91/liblinear/liblinear.vcxproj'? y
 rm: remove regular file 'nmap-7.91/liblinear/linear.h'? y
 rm: remove regular file 'nmap-7.91/liblinear/linear.def'? y
 rm: descend into directory 'nmap-7.91/liblinear/blas'? y

Delete Non-empty Directory with Full/Absolute Path

Also, the non-empty directories can be provided as a full or absolute path. This is a more secure way to delete non-empty directories because the path is absolute and can not specify with the different directory.

rm -rf /home/ismail/nmap

Print Deleted Directories and Files In Verbose Mode

The deleted non-empty directories can be also printed with the -v option. The -v option is the short form of the verbose mode.

rm -rfv nmap-7.91

The output is like below.

removed directory 'nmap-7.91/liblinear/blas'
 removed directory 'nmap-7.91/liblinear'
 removed 'nmap-7.91/nmap-protocols'
 removed 'nmap-7.91/nmap.h'
 removed 'nmap-7.91/nse_utility.h'
 removed 'nmap-7.91/CHANGELOG'
 removed 'nmap-7.91/nse_utility.cc'
 removed 'nmap-7.91/checklibs.sh'
 removed 'nmap-7.91/missing'
 removed 'nmap-7.91/nse_main.cc'
 removed 'nmap-7.91/service_scan.cc'
 removed 'nmap-7.91/INSTALL'
 removed 'nmap-7.91/NewTargets.h'
 removed 'nmap-7.91/.lgtm.yml'
 removed 'nmap-7.91/nse_fs.cc'
 removed 'nmap-7.91/nbase/nbase_addrset.c'
 removed 'nmap-7.91/nbase/CHANGELOG'
 removed 'nmap-7.91/nbase/nbase_ipv6.h'
 removed 'nmap-7.91/nbase/nbase_winunix.h'
 removed 'nmap-7.91/nbase/snprintf.c'
 removed 'nmap-7.91/nbase/nbase_str.c'
 removed 'nmap-7.91/nbase/nbase_winconfig.h'
 removed 'nmap-7.91/nbase/nbase_time.c'
 removed 'nmap-7.91/nbase/configure'
 removed 'nmap-7.91/nbase/nbase_winunix.c'
 removed 'nmap-7.91/nbase/nbase_rnd.c'
 removed 'nmap-7.91/nbase/test/nmakefile'
 removed 'nmap-7.91/nbase/test/test-escape_windows_command_arg.c'
 removed directory 'nmap-7.91/nbase/test'
 removed 'nmap-7.91/nbase/inet_ntop.c'
 removed 'nmap-7.91/nbase/configure.ac'

Leave a Comment