Shell 获取目录中选定的扩展文件计数和大小

Shell 获取目录中选定的扩展文件计数和大小,shell,sh,Shell,Sh,如何在文件夹中仅打印.TXT或.csv文件及其可读大小 例如: file type | total count | total size .TXT 30 40MB 到目前为止我所做的: $ find . -type f -name "*.TXT|csv" | wc -l 不确定如何在单个命令中进行累积大小+文件计数,但您可以使用以下命令,将它们分配给变量,然后以您想要的任何表格格式打印结果 具有多个扩展名的累积文件大小: find . -type f -r

如何在文件夹中仅打印
.TXT
.csv
文件及其可读大小

例如:

file type | total count | total size
.TXT        30            40MB
到目前为止我所做的:

$ find . -type f -name "*.TXT|csv" | wc -l

不确定如何在单个命令中进行累积大小+文件计数,但您可以使用以下命令,将它们分配给变量,然后以您想要的任何表格格式打印结果

具有多个扩展名的累积文件大小:

find . -type f -regex '.*\.\(txt\|csv\)' -exec du -ch {} + | grep total$ | cut -f1
具有单个扩展名的累积文件大小:

find . -type f -name '*.txt' -exec du -ch {} + | grep total$ | cut -f1
具有多个扩展名的文件计数:

find -regex '.*\.\(txt\|csv\)' | wc -l
具有单个扩展名的文件计数:

find . -type f -name "*.txt" | wc -l
sed -E 's/([0-9]+)\s+.*(\..*)/\1 \2/g' |

这将很艰难:

查找具有扩展名的所有文件,打印每个文件的字节数和名称:

find . -type f -regex '.*\.\(txt\|csv\)' -exec du -b {} \; |
仅保留字节数和扩展名:

find . -type f -name "*.txt" | wc -l
sed -E 's/([0-9]+)\s+.*(\..*)/\1 \2/g' |
使用AWK计算每个扩展的总计数和大小(也可以在shell中调用
numfmt
命令,使其可读),并格式化表格:

awk 'BEGIN{print "file type\ttotal count\ttotal size"} {totalSize[$2] += $1; count[$2]++} END {for (ext in count){ "numfmt --to=iec "totalSize[ext] | getline readableSize ;print ext"\t\t"count[ext]"\t\t"readableSize}}'
完全命令:

find . -type f -regex '.*\.\(txt\|csv\)' -exec du -b {} \; | 
sed -E 's/([0-9]+)\s+.*(\..*)/\1 \2/g' | 
awk 'BEGIN{print "file type\ttotal count\ttotal size"} {totalSize[$2] += $1; count[$2]++} END {for (ext in count){ "numfmt --to=iec "totalSize[ext] | getline readableSize ;print ext"\t\t"count[ext]"\t\t"readableSize}}'
输出看起来是这样的,如果你摆弄awk打印,它很容易标准化:

file type   total count total size
gradle      109         89K
txt         2283        680M

尽管如此,我一看到它就觉得脏兮兮的。这远远超出了python更具可读性的范围。

我试图在.sh文件中运行该命令,但得到了错误的选项regex错误。如何解决此问题?@TesCix-您的查找的版本是什么(
find--version
)?这是在GNU find 4.7.0Add
LANG=en_US.UTF-8
before命令上测试的,如果您使用的是非英语语言环境