How do I remove all empty directories using bash


#1

hi guys,

I have multiple empty directories. I would like to traverse entire directory structure and remove automatically all directories. Is there an easy way to do this rather then going one by one? The command should be recursive and remove all subdirectories as well.

thank you


#2

Hi Caroline,

Please used following bash command to accomplish required task

find /var/www/ -type d -empty -print0 -exec rmdir {} ;

i assume your have empty directories in /var/www tree

if you want to remove empty directories in current directory you can use find -type d -empty -print0 -exec rmdir {} ;

Let me get into little more details

ok if you want to search for empty directory in /var/www tree you can use

find /var/www/ -type d -empty -print0

if you want to delete then silently with out any prompt use -exec

find /var/www/ -type d -empty -print0 -exec rmdir {} ;

if you want to delete then and see a prompt so you can skip some off you should use use -ok rather than using -exec

find /var/www/ -type d -empty -print0 -ok rmdir {} ;

Thanks

bnhashmi


#3

Hi Caroline,

Please used following bash command to accomplish required task

find /var/www/ -type d -empty -print0 -exec rmdir {} ;

i assume your have empty directories in /var/www tree

if you want to remove empty directories in current directory you can use find -type d -empty -print0 -exec rmdir {} ;

Let me get into little more details

ok if you want to search for empty directory in /var/www tree you can use

find /var/www/ -type d -empty -print0

if you want to delete then silently with out any prompt use -exec

find /var/www/ -type d -empty -print0 -exec rmdir {} ;

if you want to delete then and see a prompt so you can skip some off you should use use -ok rather than useing -exec

find /var/www/ -type d -empty -print0 -ok rmdir {} ;

Thanks

bnhashmi


#4