![Bash 排序不正确?](https://linux22.com/image/195900/Bash%20%E6%8E%92%E5%BA%8F%E4%B8%8D%E6%AD%A3%E7%A1%AE%EF%BC%9F.png)
我在一个文件夹中收集了一些 pdf 文件。如果我做一个简单的操作,ls
它会返回:
'The Amazing Spider Man 2018 - Ch Issue - 10.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 32.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 54 - LR.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 11.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 33.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 54.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 12.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 34.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 55.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 13.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 35.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 56.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 14.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 36.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 57.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 15.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 37.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 58.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 16 - HU.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 38.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 59.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 16.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 39.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 60.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 17.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 40.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 61.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 18 - HU.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 41.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 62.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 18.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 42.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 63.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 19 - HU.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 43.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 64.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 19.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 44.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 65.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 20 - HU.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 45.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 66.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 20.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 46.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 67.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 21.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 47.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 68.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 22.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 48.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 69.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 23.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 49.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 70.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 24.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 50 - LR.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 71.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 25.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 50.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 72.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 26.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 51 - LR.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 73.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 27.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 51.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 7.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 28.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 52 - LR.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 8.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 29.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 52.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 9.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 30.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 53 - LR.pdf'
'The Amazing Spider Man 2018 - Ch Issue - 31.pdf' 'The Amazing Spider Man 2018 - Ch Issue - 53.pdf'
我的 ls 别名为ls --color=auto
.如果我做ls -l
:
total 2612448
-rw-r--r-- 1 root root 16871995 Sep 19 18:52 'The Amazing Spider Man 2018 - Ch Issue - 10.pdf'
-rw-r--r-- 1 root root 14629551 Sep 19 18:52 'The Amazing Spider Man 2018 - Ch Issue - 11.pdf'
-rw-r--r-- 1 root root 14533395 Sep 19 18:13 'The Amazing Spider Man 2018 - Ch Issue - 12.pdf'
-rw-r--r-- 1 root root 16091401 Sep 19 18:13 'The Amazing Spider Man 2018 - Ch Issue - 13.pdf'
-rw-r--r-- 1 root root 30307955 Sep 19 18:13 'The Amazing Spider Man 2018 - Ch Issue - 14.pdf'
-rw-r--r-- 1 root root 26266156 Sep 19 18:12 'The Amazing Spider Man 2018 - Ch Issue - 15.pdf'
-rw-r--r-- 1 root root 32629907 Sep 19 18:12 'The Amazing Spider Man 2018 - Ch Issue - 16 - HU.pdf'
-rw-r--r-- 1 root root 52303963 Sep 19 18:12 'The Amazing Spider Man 2018 - Ch Issue - 16.pdf'
-rw-r--r-- 1 root root 53030027 Sep 19 18:11 'The Amazing Spider Man 2018 - Ch Issue - 17.pdf'
-rw-r--r-- 1 root root 36320092 Sep 19 18:10 'The Amazing Spider Man 2018 - Ch Issue - 18 - HU.pdf'
-rw-r--r-- 1 root root 39587250 Sep 19 18:11 'The Amazing Spider Man 2018 - Ch Issue - 18.pdf'
-rw-r--r-- 1 root root 31695067 Sep 19 18:10 'The Amazing Spider Man 2018 - Ch Issue - 19 - HU.pdf'
-rw-r--r-- 1 root root 36792098 Sep 19 18:10 'The Amazing Spider Man 2018 - Ch Issue - 19.pdf'
-rw-r--r-- 1 root root 37887474 Sep 19 18:09 'The Amazing Spider Man 2018 - Ch Issue - 20 - HU.pdf'
-rw-r--r-- 1 root root 35546900 Sep 19 18:09 'The Amazing Spider Man 2018 - Ch Issue - 20.pdf'
-rw-r--r-- 1 root root 36030853 Sep 19 18:09 'The Amazing Spider Man 2018 - Ch Issue - 21.pdf'
-rw-r--r-- 1 root root 44588181 Sep 19 18:08 'The Amazing Spider Man 2018 - Ch Issue - 22.pdf'
-rw-r--r-- 1 root root 36318468 Sep 19 18:08 'The Amazing Spider Man 2018 - Ch Issue - 23.pdf'
-rw-r--r-- 1 root root 33704618 Sep 19 18:08 'The Amazing Spider Man 2018 - Ch Issue - 24.pdf'
-rw-r--r-- 1 root root 94979408 Sep 19 18:07 'The Amazing Spider Man 2018 - Ch Issue - 25.pdf'
-rw-r--r-- 1 root root 35088282 Sep 19 18:07 'The Amazing Spider Man 2018 - Ch Issue - 26.pdf'
-rw-r--r-- 1 root root 32831551 Sep 19 18:06 'The Amazing Spider Man 2018 - Ch Issue - 27.pdf'
-rw-r--r-- 1 root root 31605417 Sep 19 16:49 'The Amazing Spider Man 2018 - Ch Issue - 28.pdf'
-rw-r--r-- 1 root root 35579179 Sep 19 16:49 'The Amazing Spider Man 2018 - Ch Issue - 29.pdf'
-rw-r--r-- 1 root root 31926180 Sep 19 16:48 'The Amazing Spider Man 2018 - Ch Issue - 30.pdf'
-rw-r--r-- 1 root root 35196797 Sep 19 16:48 'The Amazing Spider Man 2018 - Ch Issue - 31.pdf'
-rw-r--r-- 1 root root 39459078 Sep 19 16:47 'The Amazing Spider Man 2018 - Ch Issue - 32.pdf'
-rw-r--r-- 1 root root 43153938 Sep 19 16:47 'The Amazing Spider Man 2018 - Ch Issue - 33.pdf'
-rw-r--r-- 1 root root 14995842 Sep 19 16:47 'The Amazing Spider Man 2018 - Ch Issue - 34.pdf'
-rw-r--r-- 1 root root 29721440 Sep 19 16:46 'The Amazing Spider Man 2018 - Ch Issue - 35.pdf'
-rw-r--r-- 1 root root 30803710 Sep 19 16:46 'The Amazing Spider Man 2018 - Ch Issue - 36.pdf'
-rw-r--r-- 1 root root 34555615 Sep 19 16:45 'The Amazing Spider Man 2018 - Ch Issue - 37.pdf'
-rw-r--r-- 1 root root 38462307 Sep 19 16:45 'The Amazing Spider Man 2018 - Ch Issue - 38.pdf'
-rw-r--r-- 1 root root 38087052 Sep 19 16:44 'The Amazing Spider Man 2018 - Ch Issue - 39.pdf'
-rw-r--r-- 1 root root 27318454 Sep 19 16:44 'The Amazing Spider Man 2018 - Ch Issue - 40.pdf'
-rw-r--r-- 1 root root 35596194 Sep 19 16:44 'The Amazing Spider Man 2018 - Ch Issue - 41.pdf'
-rw-r--r-- 1 root root 32942957 Sep 19 16:43 'The Amazing Spider Man 2018 - Ch Issue - 42.pdf'
-rw-r--r-- 1 root root 33348757 Sep 19 16:43 'The Amazing Spider Man 2018 - Ch Issue - 43.pdf'
-rw-r--r-- 1 root root 34543829 Sep 19 16:42 'The Amazing Spider Man 2018 - Ch Issue - 44.pdf'
-rw-r--r-- 1 root root 43394800 Sep 19 16:41 'The Amazing Spider Man 2018 - Ch Issue - 45.pdf'
-rw-r--r-- 1 root root 32758517 Sep 19 16:40 'The Amazing Spider Man 2018 - Ch Issue - 46.pdf'
-rw-r--r-- 1 root root 29004079 Sep 19 16:39 'The Amazing Spider Man 2018 - Ch Issue - 47.pdf'
-rw-r--r-- 1 root root 35241496 Sep 19 16:39 'The Amazing Spider Man 2018 - Ch Issue - 48.pdf'
-rw-r--r-- 1 root root 129578889 Sep 19 16:38 'The Amazing Spider Man 2018 - Ch Issue - 49.pdf'
-rw-r--r-- 1 root root 34180151 Sep 19 16:37 'The Amazing Spider Man 2018 - Ch Issue - 50 - LR.pdf'
-rw-r--r-- 1 root root 53914982 Sep 19 16:37 'The Amazing Spider Man 2018 - Ch Issue - 50.pdf'
-rw-r--r-- 1 root root 33678519 Sep 19 16:36 'The Amazing Spider Man 2018 - Ch Issue - 51 - LR.pdf'
-rw-r--r-- 1 root root 40562693 Sep 19 16:36 'The Amazing Spider Man 2018 - Ch Issue - 51.pdf'
-rw-r--r-- 1 root root 33051485 Sep 19 16:35 'The Amazing Spider Man 2018 - Ch Issue - 52 - LR.pdf'
-rw-r--r-- 1 root root 32827023 Sep 19 16:35 'The Amazing Spider Man 2018 - Ch Issue - 52.pdf'
-rw-r--r-- 1 root root 32796351 Sep 19 16:33 'The Amazing Spider Man 2018 - Ch Issue - 53 - LR.pdf'
-rw-r--r-- 1 root root 31140298 Sep 19 16:34 'The Amazing Spider Man 2018 - Ch Issue - 53.pdf'
-rw-r--r-- 1 root root 32511566 Sep 19 16:32 'The Amazing Spider Man 2018 - Ch Issue - 54 - LR.pdf'
-rw-r--r-- 1 root root 39262254 Sep 19 16:33 'The Amazing Spider Man 2018 - Ch Issue - 54.pdf'
-rw-r--r-- 1 root root 33282109 Sep 19 16:32 'The Amazing Spider Man 2018 - Ch Issue - 55.pdf'
-rw-r--r-- 1 root root 44222961 Sep 19 16:31 'The Amazing Spider Man 2018 - Ch Issue - 56.pdf'
-rw-r--r-- 1 root root 32511625 Sep 19 16:31 'The Amazing Spider Man 2018 - Ch Issue - 57.pdf'
-rw-r--r-- 1 root root 33338335 Sep 19 16:30 'The Amazing Spider Man 2018 - Ch Issue - 58.pdf'
-rw-r--r-- 1 root root 28227508 Sep 19 16:30 'The Amazing Spider Man 2018 - Ch Issue - 59.pdf'
-rw-r--r-- 1 root root 30885075 Sep 19 16:30 'The Amazing Spider Man 2018 - Ch Issue - 60.pdf'
-rw-r--r-- 1 root root 31512969 Sep 19 16:29 'The Amazing Spider Man 2018 - Ch Issue - 61.pdf'
-rw-r--r-- 1 root root 10587035 Sep 19 16:29 'The Amazing Spider Man 2018 - Ch Issue - 62.pdf'
-rw-r--r-- 1 root root 46442062 Sep 19 16:29 'The Amazing Spider Man 2018 - Ch Issue - 63.pdf'
-rw-r--r-- 1 root root 39214448 Sep 19 16:28 'The Amazing Spider Man 2018 - Ch Issue - 64.pdf'
-rw-r--r-- 1 root root 31629214 Sep 19 16:28 'The Amazing Spider Man 2018 - Ch Issue - 65.pdf'
-rw-r--r-- 1 root root 28471421 Sep 19 16:27 'The Amazing Spider Man 2018 - Ch Issue - 66.pdf'
-rw-r--r-- 1 root root 45699644 Sep 19 16:27 'The Amazing Spider Man 2018 - Ch Issue - 67.pdf'
-rw-r--r-- 1 root root 32315789 Sep 19 16:26 'The Amazing Spider Man 2018 - Ch Issue - 68.pdf'
-rw-r--r-- 1 root root 30073898 Sep 19 16:26 'The Amazing Spider Man 2018 - Ch Issue - 69.pdf'
-rw-r--r-- 1 root root 35060595 Sep 8 17:28 'The Amazing Spider Man 2018 - Ch Issue - 70.pdf'
-rw-r--r-- 1 root root 31914956 Sep 8 17:27 'The Amazing Spider Man 2018 - Ch Issue - 71.pdf'
-rw-r--r-- 1 root root 31107828 Sep 8 17:27 'The Amazing Spider Man 2018 - Ch Issue - 72.pdf'
-rw-r--r-- 1 root root 41371486 Sep 8 17:24 'The Amazing Spider Man 2018 - Ch Issue - 73.pdf'
-rw-r--r-- 1 root root 15704950 Sep 19 18:53 'The Amazing Spider Man 2018 - Ch Issue - 7.pdf'
-rw-r--r-- 1 root root 15863135 Sep 19 18:53 'The Amazing Spider Man 2018 - Ch Issue - 8.pdf'
-rw-r--r-- 1 root root 16313895 Sep 19 18:52 'The Amazing Spider Man 2018 - Ch Issue - 9.pdf'
为什么第 9 期出现在第 73 期之后?ls
默认情况下应该按字母顺序排序,不是吗?我也看了这个帖子(https://stackoverflow.com/questions/878249/unixs-ls-sort-by-name),那里的答案都没有将第 9 期移到第 73 期之前。我如何才能正确排序?只是为了我的观赏乐趣
答案1
因为ls
默认情况下按字典顺序排序。
您可以尝试以下两种方法之一:
1:
ls -v
这会将数字视为版本号,通常可以解决这样的问题。
2(如描述这里):
ls *{1..73}*pdf
ls -v
可以alias
插入 just ls
,使其成为默认行为。 bash 大括号扩展将按顺序枚举,但我不鼓励这样做,因为它需要一堆开销信息,例如最大数量等。
如果您需要更好的排序,我建议以这种方式管道ls
并sort
微调排序行为。
按照字典序权重,这种排序是有效的:
a1
a10
a100
a2
a200
b1
b10
b100
b2
b20
...
然而,如果您从更时间顺序的角度考虑这一点,它会寻找与版本编号相似的所有世界,因此ls -v
通常会处理它。
答案2
为最后 3 个条目放置一个占位符。
重命名示例
《超凡蜘蛛侠 2018 - Ch Issue - 7.pdf》
到
《超凡蜘蛛侠 2018 - Ch Issue - 07.pdf》