简体   繁体   中英

Output file size for all files of certain type in directory recursively?

I am trying to get a sum of the total size of PDF files recursively within a directory. I have tried running the command below within the directory, but the recursive part does not work properly as it seems to only report on the files in the current directory and not include the directories within. I am expecting the result to be near 100 GB in size however the command is only reporting about 200 MB of files.

find . -name "*.pdf" | xargs du -sch

Please help!

Use stat -c %n,%s to get the file name and size of the individual files. Then use awk to sum the size and print.

$ find . -name '*.pdf' -exec stat -c %n,%s {} \; | awk -F, '{sum+=$2}END{print sum}'

In fact you don't need %n , since you want only the sum:

$ find . -name '*.pdf' -exec stat -c %s {} \; | awk '{sum+=$1}END{print sum}'

You can get the sum of sizes of all files using:

find . -name '*.pdf' -exec stat -c %s {} \; | tr '\n' '+' | sed 's/+$/\n/'| bc

First find finds all the files as specified and for each file runs stat, which prints the file size. Then I supstitude all newline with a '+' using tr , then I remove the trailing '+' for the newline back and pass it to bc, which prints the sum.

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