Bash true numerical order

后端 未结 3 1304
广开言路
广开言路 2021-02-07 01:15

How can I order files in a directory by their true numeric order.

file1.txt
file2.txt
file11.txt
...

I think it\'s called : Natural Order

相关标签:
3条回答
  • 2021-02-07 01:46

    For this particular list of files, sort numerically starting at the fifth character of the first field.

    $ ls file*|sort -k1.5n
    file1
    file2
    file11
    file12
    
    0 讨论(0)
  • 2021-02-07 01:55

    If all filenames are fileSOMENUMBER.txt, try this:

    ls -1|sed 's:^[^0-9]*\([0-9]*\).*$:\1:g'|sort -n|sed 's:^\(.*\)$:file\1.txt:g'
    
    0 讨论(0)
  • 2021-02-07 02:04

    Use the -v option:

    ls -v file*
    file1
    file2
    file11
    file12
    

    Another option may be using sort -V, assuming that one is available on your platform:

    ls file* |sort -V
    
    0 讨论(0)
提交回复
热议问题