简体   繁体   English

如何在linux中仅递归计算某些文件的总大小

[英]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.

相关问题 如何在Linux中列出目录中的文件和文件夹及其总大小? - How to list the files and folders in a directory with its total size in Linux? Linux 中的一个脚本,用于查看已删除文件的总大小 - A script in Linux to view total size of removed files Linux:如何列出有关文件或目录的信息(大小,权限,按类型划分的文件数?) - Linux:How to list the information about file or directory(size,permission,number of files by type?) in total 如何计算 Linux ELF 二进制文件中的总基本块数 - How to calculate the total basic block number in a Linux ELF binary 如何使用Linux CLI查找一行代码并从特定/目录中的所有文件中删除该行 - How to find a line of code and delete that line from all files in a certain /directory only, using Linux CLI Linux Bash 脚本:获取给定目录中“png”和“gif”文件的总大小,然后比较大小 - Linux Bash Scripting: Obtain the total size of 'png' and 'gif' files in a given Directory then compare the size 在Linux中下载文件之前如何知道文件的总大小 - How to know the total size of a file before downloading it in linux 如何在Linux中获取C中的可用内存总大小? - How to get total size of free memory in C in linux? 如何在Linux中通过某些进程修改文件 - How to get files modified by certain process in Linux shell / linux脚本仅选择某些文件到FTP - shell/linux scripting only selecting certain files to FTP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM