How can I find the Size of some specified files?

不问归期 提交于 2019-12-11 04:07:16

问题


The command tries to sum up the sizes:

find . -iname "*.dmg" -exec du -sh '{}' \; 3&> /dev/null |
    awk '{print $1}' | summming_up_program???

Can you find a simpler solution?

Ubuntu Solution. Thanks for the Awk-end to Ayman.

find . -iname "*.dmg" -printf '%b\n' |
    awk 'BEGIN { s = 0 } {s += $1 } END { print "%dMB", s / 2^20 }'

回答1:


find . -iname '*.dmg' -exec stat -f '%z' '{}' \; |
     awk 'BEGIN { s = 0 } {s += $1 } END { print s }'

stat is used to obtain the size of a file. awk is used to sum all file sizes.

Edit:

A solution that does not fork stat:

find . -iname '*.dmg' -ls | 
     awk 'BEGIN { s = 0 } {s += $7 } END { print s }'



回答2:


wc -c *.pyc | tail -n1 | cut -f 1 -d ' '

Might be faster then cat'ing the files through the pipe. wc -c does not count the bytes, it retrieves the size from inode... or my hdd has a reading speed of 717 GB/s :-)

$ time wc -c very-big.pcap
5394513291 very-big.pcap
real    0m0.007s
user    0m0.000s
sys     0m0.000s



回答3:


cat *.dmg | wc -c

cat copies all the files to stdout, and wc counts the size of what was dumped. Nothing gets written to disk.

Not as efficient, but even I can understand it :)




回答4:


Enhanced Ayman's non-forking command:

find . -iname '*.dmg' -ls 3&> /dev/null | 
      awk 'BEGIN { s = 0 } {s += $7 } END { print "%dGB", s / 2^30 }'

Thanks to Ayman for correcting my initial reply.



来源:https://stackoverflow.com/questions/902233/how-can-i-find-the-size-of-some-specified-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!