Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding Empty Directories

Status
Not open for further replies.

kenrbnsn

Technical User
Jun 7, 2002
606
US
I need help finding and listing empty directories.

Yes, I know I can do a ls -R | more and eye ball the list, but I am likely to miss at least one if the list is long.

Thanks in advance.

Ken
 
Just rmdir all the directories. If there's anything in them the removal will fail.
 
No, I don't want to find them to remove them. I want to get a list of them.
 
I don't know where I got that from... in any case, you'll have to write a script that walks the directory tree and looks to see if directory is empty.

This works in bash and ksh:
Code:
find $1 -type d | while read dir ; do
    [[ $( ls -1a "$dir" | wc -l ) == 2 ]] && echo $dir is empty
done
 
I had this problem myself awhile back:

#!/bin/ksh

function lls { ls -1 ${1:+"$@"} 2>/dev/null; }

function is_dir_empty { [[ ! -d $1 || -z "$(lls -A $1)" ]] ;}


find . -type d | while read dir ; do
is_dir_empty $dir && echo $dir is empty
done
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top