bash 查找与通配符匹配的文件总大小的简短命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24919568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:56:15  来源:igfitidea点击:

Short command to find total size of files matching a wild card

linuxbash

提问by atxdba

I could envision a simple shell script that would accomplish what I want by just iterating through a list of files in a directory and summing the individual size but was wondering if there was already a more concise way to do that.

我可以设想一个简单的 shell 脚本,它可以通过遍历目录中的文件列表并总结单个大小来完成我想要的,但想知道是否已经有更简洁的方法来做到这一点。

something like

就像是

ls -lh *.jpg

that gives me the total size of just all the jpg files in the directory

这给了我目录中所有 jpg 文件的总大小

回答by Cyrus

Try duto estimate file space usage:

尝试du估算文件空间使用情况:

du -csh *.jpg

Output (for example):

输出(例如):

8.0K sane-logo.jpg
16K sane-umax-advanced.jpg
28K sane-umax-histogram.jpg
24K sane-umax.jpg
16K sane-umax-standard.jpg
4.0K sane-umax-text2.jpg
4.0K sane-umax-text4.jpg
4.0K sane-umax-text.jpg
104K total

回答by Kasramvd

You can use this function :

您可以使用此功能:

dir () { ls -FaGl "${@}" | awk '{ last_size += ; print }; END { print last_size }'; }

also you can use this command this is shorter and give you better result!

你也可以使用这个命令,它更短,给你更好的结果!

find YOUR_PATH -type f -name '*.jpg' -exec du -ch {} +

回答by Nabi K.A.Z.

For don't show files list, and just show total size, use this:

对于不显示文件列表,只显示总大小,请使用:

du -ch *.php.* | grep total

Output:

输出:

196M    total