bash 汇总测量某些文件类型的磁盘空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1358920/
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
Measure disk space of certain file types in aggregate
提问by Dan
I have some files across several folders:
我在几个文件夹中有一些文件:
/home/d/folder1/a.txt
/home/d/folder1/b.txt
/home/d/folder1/c.mov
/home/d/folder2/a.txt
/home/d/folder2/d.mov
/home/d/folder2/folder3/f.txt
How can I measure the grand total amount of disk space taken up by all the .txt files in /home/d/?
如何测量 /home/d/ 中所有 .txt 文件占用的磁盘空间总量?
I know du will give me the total space of a given folder, and ls -l will give me the total space of individual files, but what if I want to add up all the txt files and just look at the space taken by all .txt files in one giant totalfor all .txt in /home/d/ including both folder1 and folder2 and their subfolders like folder3?
我知道du 会给我一个给定文件夹的总空间,而ls -l 会给我单个文件的总空间,但是如果我想把所有的 txt 文件加起来,看看all 占用的空间怎么办?对于 /home/d/ 中的所有 .txt文件,包括文件夹 1 和文件夹 2 以及它们的子文件夹(如文件夹 3)。
采纳答案by ennuikiller
This will do it:
这将做到:
total=0
for file in *.txt
do
space=$(ls -l "$file" | awk '{print }')
let total+=space
done
echo $total
回答by Barry Kelly
find folder1 folder2 -iname '*.txt' -print0 | du --files0-from - -c -s | tail -1
find folder1 folder2 -iname '*.txt' -print0 | du --files0-from - -c -s | tail -1
回答by Barn
This will report disk space usage in bytes by extension:
这将按扩展名以字节为单位报告磁盘空间使用情况:
find . -type f -printf "%f %s\n" |
awk '{
PARTSCOUNT=split( , FILEPARTS, "." );
EXTENSION=PARTSCOUNT == 1 ? "NULL" : FILEPARTS[PARTSCOUNT];
FILETYPE_MAP[EXTENSION]+=
}
END {
for( FILETYPE in FILETYPE_MAP ) {
print FILETYPE_MAP[FILETYPE], FILETYPE;
}
}' | sort -n
Output:
输出:
3250 png
30334451 mov
57725092729 m4a
69460813270 3gp
79456825676 mp3
131208301755 mp4
回答by EvilRick
Simple:
简单的:
du -ch *.txt
If you just want the total space taken to show up, then:
如果您只想显示占用的总空间,则:
du -ch *.txt | tail -1
回答by Paused until further notice.
Here's a way to do it (in Linux, using GNU coreutils duand Bash syntax), avoiding bad practice:
这是一种方法(在 Linux 中,使用 GNU coreutilsdu和 Bash 语法),避免不良做法:
total=0
while read -r line
do
size=($line)
(( total+=size ))
done < <( find . -iname "*.txt" -exec du -b {} + )
echo "$total"
If you want to exclude the current directory, use -mindepth 2with find.
如果要排除当前目录,请使用-mindepth 2with find。
Another version that doesn't require Bash syntax:
另一个不需要 Bash 语法的版本:
find . -iname "*.txt" -exec du -b {} + | awk '{total += } END {print total}'
Note that these won't work properly with file names which include newlines (but those with spaces will work).
请注意,这些对于包含换行符的文件名将无法正常工作(但带有空格的文件名将起作用)。
回答by ppuschmann
macOS
苹果系统
- use the tool
duand the parameter-Ito exclude all other files
- 使用工具
du和参数-I排除所有其他文件
Linux
Linux
-X, --exclude-from=FILE
exclude files that match any pattern in FILE
--exclude=PATTERN
exclude files that match PATTERN
回答by ghostdog74
GNU find,
GNU 发现,
find /home/d -type f -name "*.txt" -printf "%s\n" | awk '{s+=#!/bin/bash
# calc_space
echo SPACE USED IN MEGABYTES
echo
total=0
while read FILE
do
du -m "$FILE"
space=$(du -m "$FILE"| awk '{print }')
let total+=space
done
echo $total
}END{print "total: "s" bytes"}'
回答by John Minkle
Building on ennuikiller's, this will handle spaces in names. I needed to do this and get a little report:
建立在 ennukiller 的基础上,这将处理名称中的空格。我需要这样做并得到一份小报告:
find -type f -name "*.wav" | grep export | ./calc_space
find -type f -name "*.wav" | grep 出口 | ./calc_space
for i in $(find . -type f | perl -ne 'print if m/\.([^.\/]+)$/' | sort -u); do echo "$i"": ""$(du -hac **/*."$i" | tail -n1 | awk '{print ;}')"; done | sort -h -k 2 -r
回答by texasflood
A one liner for those with GNU tools on bash:
为那些在 bash 上使用 GNU 工具的人准备的单行:
shopt -s extglob
You must have extglob enabled:
您必须启用 extglob:
shopt -s dotglob
If you want dot files to work, you must run
如果你想让点文件工作,你必须运行
d: 3.0G
swp: 1.3G
mp4: 626M
txt: 263M
pdf: 238M
ogv: 115M
i: 76M
pkl: 65M
pptx: 56M
mat: 50M
png: 29M
eps: 25M
Sample output:
示例输出:
find . -name "*.txt" -print0 |xargs -0 du -ch
etc
等等
回答by boes
I like to use find in combination with xargs:
我喜欢将 find 与 xargs 结合使用:
find . -name "*.txt" -print0 |xargs -0 du -ch | tail -n1
Add tail if you only want to see the grand total
如果您只想查看总计,请添加尾部
##代码##
