bash 在bash中按数字对文件进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13360925/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort files numerically in bash
提问by Crazy_Bash
I need to sort .flv files numerically and i was able to do it with the following command:
我需要以数字方式对 .flv 文件进行排序,并且可以使用以下命令进行排序:
ls *\.flv | sort --version-sort -f
but with many files(hundreds) it's not sorting correctly.
但是对于许多文件(数百个),它没有正确排序。
ls *\.flv | sort --version-sort -f | tail -n 20
e680.flv
e681.flv
e682.flv
e683.flv
e684.flv
e685.flv
e686.flv
e687.flv
e688.flv
e689.flv
e690.flv
e691.flv
e692.flv
e693.flv
e694.flv
e695.flv
**e696.flv**
s572.flv
s602.flv
s654.flv
but the strange this is, if i'm ruining the command without "*.flv"it's working. 
i could use just ls but i have other file types in the folder.
但奇怪的是,如果我在没有"*.flv"工作的情况下破坏了命令。 
我可以只使用 ls 但我在文件夹中有其他文件类型。
ls | sort --version-sort -f | tail -n 20
e680.flv
e681.flv
e682.flv
e683.flv
e684.flv
e685.flv
e686.flv
e687.flv
e688.flv
e689.flv
e690.flv
e691.flv
e692.flv
e693.flv
e694.flv
e695.flv
e696.flv
what i've tried so far:
到目前为止我尝试过的:
    ls | sort --version-sort -f | grep "flv"
    ls *.flv | sort --version-sort -f
    ls *\.flv | sort --version-sort -f
    ls *.flv | sort -f
回答by Kamil ?rot
I would try following code. Works on my testing scenario:
我会尝试以下代码。适用于我的测试场景:
ls -1 *\.flv | sort -n -k1.2
The lslists flv files 1 on each line, sorttakes first (and only one) word on each line starting on second character (start of the number). Sorts numerically
ls每行的列表 flv 文件 1,sort从第二个字符(数字的开头)开始,在每行上取第一个(并且只有一个)单词。按数字排序
回答by ccpizza
Given a folder with sequentially named files from 1.flvto 9999.flv
给定一个文件夹,其中包含从1.flv到的顺序命名的文件9999.flv
ls -v1 *.flv
will output:
将输出:
1.flv
2.flv
...
10.flv
...
90.flv
...
100.flv
101.flv
...
9999.flv
From the man page:
从手册页:
    -v     natural sort of (version) numbers within text
    -1     list one file per line
For brevity, the two flags above can be clubbed together as -v1.
为简洁起见,上面的两个标志可以组合在一起作为-v1.
回答by Nahuel Fouilleul
to sort numerically after first character, try this :
在第一个字符后按数字排序,试试这个:
sort -k1.2n

