如何找到某些指定文件的大小?

时间:2022-06-21 04:46:59

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.

Ubuntu解决方案。感谢Awk-end到艾曼。

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

4 个解决方案

#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.

stat用于获取文件的大小。 awk用于汇总所有文件大小。

Edit:

A solution that does not fork stat:

一个没有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 :-)

可能会更快,然后通过管道捕获文件。 wc -c不计算字节数,它从inode中检索大小...或者我的hdd读取速度为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

猫* .dmg | wc -c

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

cat将所有文件复制到stdout,wc计算转储的大小。什么都没有写入磁盘。

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

效率不高,但即使我能理解它:)

#4


Enhanced Ayman's non-forking command:

增强了Ayman的非分叉命令:

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.

感谢Ayman纠正我的初步答复。

#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.

stat用于获取文件的大小。 awk用于汇总所有文件大小。

Edit:

A solution that does not fork stat:

一个没有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 :-)

可能会更快,然后通过管道捕获文件。 wc -c不计算字节数,它从inode中检索大小...或者我的hdd读取速度为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

猫* .dmg | wc -c

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

cat将所有文件复制到stdout,wc计算转储的大小。什么都没有写入磁盘。

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

效率不高,但即使我能理解它:)

#4


Enhanced Ayman's non-forking command:

增强了Ayman的非分叉命令:

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.

感谢Ayman纠正我的初步答复。