-1

I have a collection of pdfs in a folder. If I do a simple ls it returns this:

'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'

I have ls aliased to ls --color=auto. If I do 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'

Why does Issue 9 come up after Issue 73? ls is supposed to sort alphabetically by default, isn't it? I also looked at this post (https://stackoverflow.com/questions/878249/unixs-ls-sort-by-name), none of the answers there move Issue 9 before Issue 73. How would I get this to sort correctly? Just for my viewing pleasure

ZackT
  • 13
  • 1
    Relating https://unix.stackexchange.com/q/33909/117549 – Jeff Schaller Sep 19 '21 at 23:23
  • 2
    "ls is supposed to sort alphabetically by default, isn't it?" -- well, "9" comes after "73" in the same way "b" comes after "apple". If you want numeric sort, see Jeff's link. – Chris Down Sep 19 '21 at 23:39
  • 1
    It is sorting alphabetically. Use 0 padded numbers always and you never have this issue. Note that if you have more than 99, you have to zero pad with two 0's or you hit the same issue when number goes over 100. Just get in the habit of always using zero padding on numbers and you will never have this sorting issue. – Lizardx Sep 20 '21 at 00:40
  • You can rename them to zero-padded filenames with the perl rename utility. e.g. rename -n 's/(\d+)((?: - [A-Z]{2})?\.pdf)$/sprintf("%02i%s",$1,$2)/e' *.pdf (that's for 2-digit zero-padding. for 3, use %03i). The -n is for a dry-run, remove that option (or replace it with -v for verbose output) when you're sure it's going to do what you want. – cas Sep 20 '21 at 04:31

2 Answers2

2

Because ls by default sorts lexicographically.

You could try one of two things:

№1:

ls -v

this will treat the numbers as version numbers, and that usually solves things like this.

№2 (as described here):

ls *{1..73}*pdf

ls -v can be alias'd into just ls, to make this the default behavior. The bash brace expansion will enumerate in order, but I wouldn't encourage it, since it requires a bunch of overhead information, like the maximum number, etc.

If you need even better sorting, I'd suggest piping ls into sort and fine-tune the sorting behavior that way.

By lexicographic weight, this sorting is valid:

a1
a10
a100
a2
a200
b1
b10
b100
b2
b20
...

However, if you think about this in a more chronological sense, it looks for all the world similar to version numbering, hence ls -v often takes care of it.

polemon
  • 11,431
-1

Put a place holder for the last 3 entries.

Example Rename

'The Amazing Spider Man 2018 - Ch Issue - 7.pdf'

to

'The Amazing Spider Man 2018 - Ch Issue - 07.pdf'