Searching and counting files in directories and su

2019-08-17 05:48发布

问题:

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 :)

回答1:

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)


标签: bash shell