How to run a bash script only on directories that have no subdirectories

bashbash-scriptingcommand line

I would like to process every directory in my parent directory but only on a condition that these directories have no subdirectories in them.

Right now I have a following directory structure:


And the following script

while read -r dir
    echo "$dir"
     /Users/rusl/.cliapps/MQA_identifier "$dir" | tee "$dir"/mqa.log
done < <(find . -type d)

All it does is checks if every music file in a directory was encoded using MQA and logs its output like this:

1   MQA 44.1K   10 - Bury A Friend [MQA 24_44].flac

It works and creates the logs I want in directories like Record_A1 and CD_1.

But it also creates a lot of redundant files. For example, it creates a log in Band_A directory, containing output for all files in all subdirectories in Band_A directory or it creates a log in Band_B and then Record_B1, again containing output for all files in respective subdirectories.

So how can I run a script and generate the logs ONLY for those directories that have no nested directories?

EDIT: also I think this script processes every subdirectory as many times as it is nested inside the parent top most directory.
Not that it is critical, but still not efficient.

Best Answer

The basic idea would be to have go through all directories, test if there are subdirectories and if so, run a part of the script.

while read -r dir ; do
    subdircount=$(find "$dir" -maxdepth 1 -type d | wc -l)
    if [[ "$subdircount" -eq 1 ]] ; then
        echo "$dir"
        /Users/rusl/.cliapps/MQA_identifier "$dir" | tee "$dir"/mqa.log
done < <(find . -type d)