简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM