Linux 如何在bash中按文件扩展名和大小对目录进行排序
我想制作一个脚本,它将按扩展名打印文件夹和子文件夹中的所有文档,计算有多少文件类型和大小Linux 如何在bash中按文件扩展名和大小对目录进行排序,linux,bash,file,unix,scripting,Linux,Bash,File,Unix,Scripting,我想制作一个脚本,它将按扩展名打印文件夹和子文件夹中的所有文档,计算有多少文件类型和大小 Example: file type | total count | total size pdf 30 4.0k txt 90 60.0k 差不多吧。除了尺寸部分外,我已经知道了怎么做。有什么建议吗 使用GNUfind和GNUawk: find . -type f -printf '%s %f\n' | awk '{ size = $1; ext = ""; if(su
Example:
file type | total count | total size
pdf 30 4.0k
txt 90 60.0k
差不多吧。除了尺寸部分外,我已经知道了怎么做。有什么建议吗 使用GNU
find
和GNUawk
:
find . -type f -printf '%s %f\n' | awk '{ size = $1; ext = ""; if(sub(/.*\./, "") != 0) { ext = $0 }; total[ext] += size; ++ctr[ext] } END { PROCINFO["sorted_in"] = "@ind_str_asc"; for(ext in total) { print ext " " ctr[ext] " " total[ext] } }'
这里
打印每个文件的大小及其名称,而不打印其路径的目录部分,awk代码的工作方式如下:
{ # for each line in find's output
size = $1 # remember the size
ext = "" # isolate the extension
if(sub(/.*\./, "") != 0) { # if the sub returns 0, there was no . in the
ext = $0 # file name, so it has no extension
}
total[ext] += size # tally up the size and file counters
++ctr[ext]
}
END { # in the end: print the tallies.
# The PROCINFO bit for sorted output is GNU-
# specific. In case that's a worry, print
# unsorted and pipe through sort afterwards.
PROCINFO["sorted_in"] = "@ind_str_asc"
for(ext in total) {
print ext " " ctr[ext] " " total[ext]
}
}
解释
find . -type f -print0
查找子目录中的所有文件并打印它们,以空字符分隔(somefile.abc
)
对于每个文件,它以千字节为单位打印文件大小(12somefile.abc
)
仅选择以点和一些扩展名结尾的文件(12somefile.abc
)
按字符反转每行(cba.elifemos21
)
删除点和之间的所有字符(cba21
)
按字符反转每行(12abc
)
根据扩展名对行进行求和如果有帮助,可以使用
du-b文件
或stat-c%s文件
以字节为单位获取文件大小。谢谢,我知道,但是du-b,但我很难通过分组文件类型来确定如何使用它
find . -type f -print0 | xargs -0 du -k | grep "\.[a-zA-Z]*$" | rev | sed -e "s/\..*\t/\t/g" | rev | awk '{SUM[$2]=+$1} END{for (x in SUM) print x,SUM[x]}' | sort
find . -type f -print0
| xargs -0 du -k
| grep "\.[a-zA-Z0-9]*$"
| rev
| sed -e "s/\..*\t/\t/g"
| rev
| awk '{SUM[$2]=+$1} END{for (x in SUM) print x,SUM[x]}'