获取目录的所有扩展名:easy。 获取特定扩展的文件数量:简单。
但获取所有文件扩展名和他们各自的文件计数暗指我。
例如。
+ dir + abc.txt + def.txt + abc.pdf * def.pov
应该返回类似于:
.txt 2 .pdf 1 .pov 1
这个练习的目的是我想找出哪个文件扩展名在某个目录中很受欢迎。
提前致谢
/var/cache$ sudo find ./ -type f | grep -E ".*\.[a-zA-Z0-9]*$" | sed -e 's/.*\(\.[a-zA-Z0-9]*\)$/\1/' | sort | uniq -c | sort -n 1 .6 1 .cache 1 .noconf 1 .php 1 .sl 2 .bin 2 .el 2 .tdb 4 .baseA 4 .baseB 4 .dat 4 .DB 27 .db 221 .deb
这里是解释:
find ./ -type f
只find文件,而不是目录
grep -E ".*\.[a-zA-Z0-9]*$"
过滤文件的扩展名
sed -e 's/.*\(\.[a-zA-Z0-9]*\)$/\1/'
删除path和文件名,只保存扩展名
sort | uniq -c | sort -n
sorting,uniq和sorting