How to loop all files in sorted order in Bash?

后端 未结 7 1043
离开以前
离开以前 2020-12-05 01:51

I am looping all files in directory with following command:

for i in *.fas; do some_code; done;

However, I get order like this:

<         


        
相关标签:
7条回答
  • 2020-12-05 02:37

    With option sort -g it compares according to general numerical value

     for FILE in `ls ./raw/ | sort -g`; do echo "$FILE"; done
    

    0.log 1.log 2.log ... 10.log 11.log

    This will only work if the name of the files are numerical. If they are string you will get them in alphabetical order. E.g.:

     for FILE in `ls ./raw/* | sort -g`; do echo "$FILE"; done
    

    raw/0.log raw/10.log raw/11.log ... raw/2.log

    0 讨论(0)
提交回复
热议问题