Unix script to check for subfolders.

Hi,

Need a unix script to loop through thousands of folders and write a file listing the folders that contain less than x amount of subfolders.

Thank you.
cbonesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nociSoftware EngineerCommented:
First create this script:  
#!/bin/bash

CNT=$( find $1 -type d -print | wc -l )
CNT=$(( $CNT - 1 ))    # subtract one to skip the own directory
if [ "$CNT" -lt 10 -a "$CNT" -ne "0" ]
then
        echo $1 
fi

Open in new window


save this as count.sh
and do chmod 755 count.sh

then:

find / -type d -print | xargs -n 1 count.sh

Should provide the list of directories.
cbonesAuthor Commented:
Hi,

I ran this but it is returning all folders and subfolders in the directory.  The above is looking for subdirectories with less than 10 subfolders?

Thank you
nociSoftware EngineerCommented:
The find will search for folders. The count.sh will take a folder and report if >0 <10 directories are found there.
It will skip all folders having no subfolders.

#!/bin/bash
X=$1

CNT=$( find "$2" -type d -print | wc -l )
CNT=$(( $CNT - 1 ))    # subtract one to skip the own directory
if [ "$CNT" -lt "$X" -a "$CNT" -ne "0" ]
then
        echo $1 
fi

Open in new window


If you want a different value for 10 you can change that see new script: The below command will produce anything with limit <20.

find / -type d -print | xargs -n 1 count.sh  20
Murugesan NagarajanShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NTCommented:
@cbones
#!/bin/bash
# Proceed based on comment from noci
# Also handle other exceptions/usage:
# EXCEPTION1
if [ 0 -eq $# ]
then
        echo "Usage:"
        echo "$0 Number Directory_Name"
else
        # EXCEPTION2
        echo "$1" | /bin/grep -E "^[0-9]" >/dev/null 2>&1
        Ret=$?
        if [ 0 -eq $Ret ]
        then
                # EXCEPTION3
                echo "$1" | /bin/grep -E "[0-9]$" >/dev/null 2>&1
                Ret=$?
        fi
        if [ 0 -ne $Ret ]
        then
                echo "Usage:"
                echo "$0 NUMBER Directory_Name"
        else
                X=$1
                # EXCEPTION4 to use full path.
                CNT=$( /usr/bin/find $2 -type d -print | /usr/bin/wc -l )
                CNT=$(( $CNT - 1 ))    # subtract one to skip the own directory
                if [ "$CNT" -lt "$X" -a "$CNT" -ne "0" ]
                then
                        echo CNT $CNT -lt $X and "$CNT" -ne "0" matches.
                        echo $1
                else
                        # Display EXCEPTION5
                        echo CNT $CNT -lt $X and "$CNT" -ne "0" mismatch.
                fi
        fi
fi

Open in new window

simon3270Commented:
I assume that you want to count only the immediate subdirectories of a directory, not ones within those subdirectories.

Have a script count.sh:
#!/bin/bash

n=$(find "$2" -mindepth 1 -maxdepth 1 -type d | wc -l)

if [ $n -lt $1 -a $n -gt 0 ]; then
  # Display the number of subdirectories, and the name of the directory they are in
  echo $n $2
fi

Open in new window

Then, to start the search from the directory "/starting/directory":
cd /starting/directory; find . -type d -print | xargs -n 1 count.sh  20

This displays the number of subdirectories, and the name of the directory containing them. If you don't want the number, take the "$n" out of the "echo" line
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.