[英]How to calculate the total size of certain files only, recursive, in linux
I've got a bunch of files scattered across folders in a layout, eg:我有一堆文件分散在布局中的文件夹中,例如:
dir1/somefile.gif
dir1/another.mp4
dir2/video/filename.mp4
dir2/some.file
dir2/blahblah.mp4
And I need to find the total disk space used for the MP4 files only .我需要找到只用于MP4文件的总磁盘空间。 This means it's gotta be recursive somehow.
这意味着它必须以某种方式递归。
I've looked at du
and fiddling with piping things to grep
but can't seem to figure out how to calculate just the MP4 files no matter where they are.我看过
du
并摆弄管道到grep
东西,但似乎无法弄清楚如何只计算 MP4 文件,无论它们在哪里。
A human readable total disk space output is a must too, preferably in GB, if possible?如果可能的话,人类可读的总磁盘空间输出也是必须的,最好以 GB 为单位?
Any ideas?有任何想法吗? Thanks
谢谢
For individual file size:对于单个文件大小:
find . -name "*.mp4" -print0 | du -sh --files0-from=-
For total disk space in GB:对于以 GB 为单位的总磁盘空间:
find . -name "*.mp4" -print0 | du -sb --files0-from=- | awk '{ total += $1} END { print total/1024/1024/1024 }'
You can simply do :你可以简单地做:
find -name "*.mp4" -exec du -b {} \; | awk 'BEGIN{total=0}{total=total+$1}END{print total}'
The -exec option of find command executes a simple command with {} as the file found by find. find 命令的 -exec 选项执行一个简单的命令,其中 {} 作为 find 找到的文件。 du -b displays the size of the file in bytes.
du -b 以字节为单位显示文件的大小。 The awk command initializes a variable at 0 and get the size of each file to display the total at the end of the command.
awk 命令将变量初始化为 0 并获取每个文件的大小以在命令末尾显示总数。
这将以字节为单位总结所有 mp4 文件的大小:
find ./ -name "*.mp4" -printf "%s\n" | paste -sd+ | bc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.