0

I am trying to count the number of file in each directory/subdirectory, add them together and get a total which I can then compare to another directory:

#!/bin/bash

echo "Checking directories for proper extensions"

for LOOP in 1 2 3 4; 
do
    if [[ $LOOP -eq 1 ]]; then
        find ./content/documents -type f ! \( -name \*.txt -o -name \*.doc -o -name \*.docx \)
        count1=$(find ./content/documenets -type f) | wc -l
        #count number of files in directory and add to counter
    elif [[ $LOOP -eq 2 ]]; then
        find ./content/media -type f ! -name \*.gif 
        count2=$(find ./content/media -type f) | wc -l
        #count number of files in directory and add to counter
    elif [[ $LOOP -eq 3 ]]; then
        find ./content/pictures -type f ! \( -name \*.jpg -o -name \*.jpeg \) 
        count3=$(find ./content/pictures -type f) | wc -l
        #count number of files in directory and add to counter
    else
        count4=$(find /home/dlett/content/other -type f) | wc -l
        #count number of files in directory and add to counter
    fi

    #list the files in each subdirectory in catalog and put into an array
    #count the number of items in the array
    #compare number of item in each array
    #if the number of item in each array doesn't equal 
        #then print and error message
    content_Count=$(( count1+count2+count3+count4 ))
    echo $content_Count
done
1

Your question doesn't say where the previous known-good values come from. I'm going to assume you have a parallel tree to the content tree called ../oldcontent. Adjust to suit:

#!/bin/bash

echo "Checking directories for proper extensions"

for d in documents media pictures other
do
    nfc=$(find content/$d -type f | wc -l)
    ofc=$(find ../oldcontent/$d -type f | wc -l)
    if [ $nfc -eq $ofc ]
    then
         echo The "$d" directory has as many files as before.
    elif [ $nfc -lt $ofc ]
    then
         echo There are fewer files in the content directory than before.
    else
         echo There are more files in the content directory than before.
    fi
done

This code is a lot shorter because it doesn't try to give a different find command on each loop. If you really need that, you could use an associative array to pair directory names and find arguments:

declare -A dirs=(
    [documents]="-name \*.txt -o -name \*.doc -o -name \*.docx" 
    [media]="-name \*.gif"
    [pictures]="-name \*.jpg -o -name \*.jpeg"
    [other]=""
)

Then the for loop becomes:

for d in "${!dirs[@]}"
do
    nfc=$(find content/$d -type f ${dirs[$d]} | wc -l)
    ofc=$(find ../oldcontent/$d -type f ${dirs[$d]} | wc -l)

...

That only works in Bash 4 and up, though. There's a less powerful associative array mechanism in Bash 3, but it's pretty much broken by design. If you don't have Bash 4 on hand, I'd encourage you to switch to something like Perl, Python or Ruby rather than try to arm-twist Bash 3 associative arrays into service for something like this.

Beware that this does not tell you that the content tree contains the same files as ../oldcontent, only that each subdirectory has the same number of files. If you're trying to detect changes to files within each tree, you'll need to use rsync or my "MD5 a directory" solution here on Unix.SE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.