Dec 13, 2004 #1 kenrbnsn Technical User Joined Jun 7, 2002 Messages 606 Location 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
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
Dec 13, 2004 #2 ericbrunson Technical User Joined Jan 9, 2004 Messages 2,092 Location US Just rmdir all the directories. If there's anything in them the removal will fail. Upvote 0 Downvote
Dec 13, 2004 Thread starter #3 kenrbnsn Technical User Joined Jun 7, 2002 Messages 606 Location US No, I don't want to find them to remove them. I want to get a list of them. Upvote 0 Downvote
Dec 13, 2004 #4 ericbrunson Technical User Joined Jan 9, 2004 Messages 2,092 Location US 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 Upvote 0 Downvote
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
Dec 13, 2004 #5 olded Programmer Joined Oct 27, 1998 Messages 1,065 Location US 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 Upvote 0 Downvote
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