按给定字符的出现次数排序文件名?

时间:2016-01-21 05:35:26

标签: bash filenames

我想在目录中输出文件名列表,其中列表应按每个文件名中给定字符的外观数量降序排列。我怎么能用bash做到这一点?

1 个答案:

答案 0 :(得分:2)

假设您希望按照" a"的出现进行排序。在文件名中:

for i in *; do; echo "`grep -o "a" <<< "$i" | wc -l` $i"; done | sort -r

<强>结果

$ ls
carla
elaine
guybrush
herman
largo
leamon-head
lechuck
max
meathook
ozzie
sam
stan
voodoo

$ for i in *; do; echo "`grep -o "a" <<< "$i" | wc -l` $i"; done | sort -r
2 leamon-head
2 carla
1 stan
1 sam
1 meathook
1 max
1 largo
1 herman
1 elaine
0 voodoo
0 ozzie
0 lechuck
0 guybrush