linux - 如何在文件夹层次结构中找到所有不同的贪婪文件后缀?

标签 linux bash ubuntu-14.04

我在这里找到了一个相关问题:

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/

相关文章:

ruby-on-rails - 尝试加载 gem 'yard' 时出错

linux - 期望脚本运行 shell 命令

python - 有没有办法在 Python 中编写脚本来更改 Linux 中的用户密码?如果是这样,如何?

linux - 如何编写shell脚本查找PID并Kill

linux - 从具有父目录的每个子文件夹复制特定文件

linux - 打开多个选项卡并在 shell 脚本中执行命令

linux - bash shell 中的管道和重定向

windows - Bash中手动输入Windows路径,如何转换为POSIX路径?

Bash - 检查多个环境变量并列出所有缺失的变量

linux - 使用 sudo chmod 命令将/usr 文件权限更改为 0744