Unix script to check for subfolders.

cbones
cbones used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
nociSoftware Engineer
Distinguished Expert 2018

Commented:
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.

Author

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 Engineer
Distinguished Expert 2018

Commented:
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
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

murugesandinsShell_script Automation /bin/bash /bin/bash.exe /bin/ksh /bin/mksh.exe AIX C C++ CYGWIN_NT HP-UX Linux MINGW32 MINGW64 SunOS Windows_NT

Commented:
@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

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
@noci: i think there is a missing -maxdepth 1 param in the find inside the script
i like this better

find DIRECTORY -type d | sed 's:\(.*\)/[^/]*:\1:' | sort | uniq -c | grep '^[[:space:]]*[0-9][0-9]'

example

find /var/log -type d | sed 's:\(.*\)/[^/]*:\1:' | uniq -c | grep '^[[:space:]]*[0-9][0-9]'
     15 /var/log/boot-sav/log/2013-10-28__18h01boot-repair55
     15 /var/log/boot-sav/log/2013-10-28__18h34boot-repair09
     13 /var/log/boot-sav/log/2013-10-28__12h45boot-repair48
     14 /var/log/boot-sav/log/2013-10-28__18h35boot-repair29
     13 /var/log/boot-sav/log/2013-10-28__17h54boot-repair20
     13 /var/log/boot-sav/log/2013-10-28__17h36boot-repair35
     10 /var/log
     10 /var/log/dist-upgrade

Open in new window


i m getting tired. there is probably a missing sort before the uniq
nociSoftware Engineer
Distinguished Expert 2018

Commented:
hm. not sure about the missing maxdepth..., there was no mentioning it to be restricted to one level ...
thousands of folders does seem to imply multi level ...  i had in the first draft then read the Q again and dropped it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial