如何一个接一个地获取带有模式的文件名

时间:2014-02-21 18:21:17

标签: bash shell sh

如何使用shell脚本在循环中逐个获取带有模式f-1,f-5,f-4,f-3,f-2的文件的名称。 我需要以no 1,2,3,4,5

的顺序检索文件名
#!/bin/bash
ls f-* | sort | while read m
do
cat $m
done

3 个答案:

答案 0 :(得分:0)

for i in 1 2 3 4 5
do
  cat f-$i
done

ls f-* | sort | while read f
do
  cat $f
done

答案 1 :(得分:0)

如果它只是您拥有众多选项后的文件名,请在下面添加几个:

for f in f-*; do echo "$f"; done

ls f-* | while read f; do echo "$f"; done

#Total overkill for this example
find . -name "f-*" | while read f; do
  echo "$f"
done

答案 2 :(得分:0)

这有帮助吗?

#!/bin/bash

echo ls: $(ls)
echo ""

seqmax=0
for file in $(ls ./f-* ); do
    char=$( echo $file | wc -c )
    if [ $char > $seqmax ];then
        seqmax=$char
    fi
done
i=0
for count in $(seq 1 1 $seqmax);do
    for file in $(ls | egrep "^f\-[0-9]{$count}$");do
        completearray[$i]=$file
        i=$(expr $i + 1 )
    done
done

echo ${completearray[@]}

,这应打印如下。

$ ./script.sh
ls: f-1 f-15 f-16 f-2 f-3 f-one f-two g-1 g-2 g-3 script.sh

f-1 f-2 f-3 f-15 f-16