nikigx2
nikigx2

Reputation: 63

Searching and counting files in directories and subdirectories [BASH]

I have one question about searching and counting files in directories and subdirectories. I tried to do something like this

for i in $(find $TEST_DIR -type d | wc -l ); do
        for j in $(find $i -type f | wc -l); do
            FILES=$[FILES+j]
        done    
        DIRS=$[DIRS+i]
    done

but it doesn't work. I just have to count files in every directory and subdirectory after that I have to compare quantity of files and directories(subdirectories) Thanks for your help :)

Upvotes: 0

Views: 701

Answers (1)

kev
kev

Reputation: 161684

The find command will find all files/dirs recursively, so the for...loop is not needed:

FILES=$(find $TEST_DIR -type f | wc -l)
DIRS=$(find $TEST_DIR -type d | wc -l)

If your filename may contains newline, try this:

FILES=$(find $TEST_DIR -type f -printf x | wc -c)

Upvotes: 3

Related Questions