Cronk
Cronk

Reputation: 473

Counting subdirectories within a directory

Using Mac OS X 10.8.5. Figured that doing this on the command line would make the most sense, but other suggestions are welcome.

I have a directory of various backups. There are about 45 backups here, each an incremental stage run periodically. The directory structure of each backup dir is the same, but the top/parent directory of each of them has a different name (the time stamp). Example:

2013_9_12_0500/Files/MoreFiles/SomethingElse/Folder/subfolder/morefolders
2013_9_12_0600/Files/MoreFiles/SomethingElse/Folder/subfolder/morefolders
2013_9_12_0700/Files/MoreFiles/SomethingElse/Folder/subfolder/morefolders
2013_9_12_0800/Files/MoreFiles/SomethingElse/Folder/subfolder/morefolders
2013_9_12_0900/Files/MoreFiles/SomethingElse/Folder/subfolder/morefolders
2013_9_12day/Files/MoreFiles/SomethingElse/Folder/subfolder/morefolders
2013_9_13day/Files/MoreFiles/SomethingElse/Folder/subfolder/morefolders
2013_9_14day/Files/MoreFiles/SomethingElse/Folder/subfolder/morefolders
2013_9_10week/Files/MoreFiles/SomethingElse/Folder/subfolder/morefolders

etc.

The 'subfolder' directory contains many many subdirectories itself. I want to count how many sub-directories are within EACH 'subfolder' directory. Example:

2013_9_12_0500/Files/MoreFiles/SomethingElse/Folder/subfolder/  =>  884 subdirs
2013_9_12_0600/Files/MoreFiles/SomethingElse/Folder/subfolder/  =>  1423 subdirs
2013_9_12_0700/Files/MoreFiles/SomethingElse/Folder/subfolder/  =>  540 subdirs
2013_9_12_0800/Files/MoreFiles/SomethingElse/Folder/subfolder/  =>  378 subdirs

etc...

I have had some luck with this command:

 find ./*/*/*/*/*/subfolder -type d | wc -l

The problem is that it only shows me the overall total of ALL 'subfolders' and not the count within EACH individual subfolder. E.g. "13543" instead of as listed above.

Yes, I could do this manually one at a time, but were is the excitement in learning something new in that? :)

Thanks, C

Upvotes: 1

Views: 2000

Answers (2)

tripleee
tripleee

Reputation: 189948

Why don't you do

for d in ./*/*/*/*/*/subfolder; do
    printf "%s\t" "$d"
    find "$d" -type d -printf "%i\n" | wc -l
done

The -printf option to the find command is a safeguard in case you have directory names with newlines or whatever.

Upvotes: 1

beroe
beroe

Reputation: 12326

A lot of the shenanigans below are to deal with spaces in folder names, but I think this function will generate your output...

Usage: countsubdirs PUT/PATH/HERE

countsubdirs(){
    ORIGIFS=$IFS
    IFS=$(echo -en "\n\b")
    DIRS=$(find "$1" -maxdepth 1 -mindepth 1 -type d )
    for D in $DIRS; do echo $D "=>" $(find $D -maxdepth 1 -mindepth 1 -type d | wc -l) subdirs; done
    IFS=$ORIGIFS
}

Output:

temp/hello =>        1 subdirs
temp/ntest 4 =>        0 subdirs
temp/temp =>        0 subdirs
temp/test =>        2 subdirs
temp/test 4 =>        0 subdirs
temp/test3 =>        1 subdirs
temp/tester =>        0 subdirs
temp/xl =>        1 subdirs

Upvotes: 1

Related Questions