我在这里找到了一个相关问题:
How can I find all of the distinct file extensions in a folder hierarchy?
但我的情况略有不同。在 Ubuntu 14.04 Linux 上使用 bash,如果我有一堆如下所示的文件:
ls -1 | sort -V
fileA.foo.bar.txt.gz
fileA.foo.foo.txt.gz
fileA.xyz.bar.txt.gz
fileB.foo.bar.txt.gz
fileB.foo.foo.txt.gz
fileB.xyz.bar.txt.gz
我想知道从找到的第一个分隔符中获得了每个扩展名的多少个文件(例如示例中的\.
)。所以它会是:
2 .foo.bar.txt.gz
2 .foo.foo.txt.gz
2 .xyz.bar.txt.gz
这不是我在上面的问题中找到的最佳答案:
find . -type f | sed -e 's/.*\.//' | sed -e 's/.*\///' | sort | uniq -c | sort -rn
6 gz
find . -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort | uniq -c
6 gz
最佳答案
cwd 中文件的所有 bash 解决方案:
declare -A a # declare an associative array a
for f in *.* # loop all filenames with a .
do
((a[${f#*.}]++)) # increment array elements value
done
输出计数:
for k in "${!a[@]}" # loop all array keys
do
echo ${a[$k]} $k # output value and key
done
1 zip
2 txt
1 txt~
关于linux - 如何在文件夹层次结构中找到所有不同的贪婪文件后缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49049110/