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