4

I have a folder of about 1,000 image files. I need to create a list of them. I saw somewhere that if I go into Terminal and cd into the folder, all I have to do is type in

ls > list.csv

to generate a list. The thing is, this list is not in the same order as the files I see in Finder. For example, in Finder, the first image is 16_left.jpg. However, the first image in the generated list.csv file is 10017_left.jpg, whilst 16_left.jpg is snuggled all the way down in between 15975_right.jpg and 16007_right.jpg.

I can see that in Finder, it sees the number 16 as being the smallest in the files and puts that top, whereas the list.csv file sorts the list not by the whole number itself but by each individual digit left-to-right.

How do I get list.csv to be in the same order as Finder?

5
  • Does print -rC1 -- *.jpg(NDn) give you the order you want? Sep 11, 2022 at 14:31
  • Sorry this is a really dumb question but I've just pasted that line into the terminal window and nothing has happened, it just went directly to the next line with >, it didn't even have the usual "(base) myname@MyMcacBook" bit in front of the cursor on the next line.. is it doing something?
    – brosefzai
    Sep 11, 2022 at 14:37
  • Did you maybe not enter the closing )? Sep 11, 2022 at 14:39
  • Haha! Yes! Perfect! This has worked a charm! Thank you! I just added the > list.csv to the end to spit out the file. This has been a great help. I am surprised the code is so enigmatic. Such is the magic of bash I suppose!
    – brosefzai
    Sep 11, 2022 at 14:47
  • 5
    No, that's zsh, not bash. bash has no equivalent. Sep 11, 2022 at 14:49

1 Answer 1

11

ls and shell globs sort file lists lexically by their name by default.

The GNU implementation of ls has a -v option for version sort which would help you here, but the ls in macos has no equivalent (its -v is for something else (verbatim)).

The GNU implementation of sort has a -V option also for version sort. macos' sort did copy that one (BSDs sort used to be GNU sort, so the ones that have rewritten theirs have kept the API).

So if none of the file names in the current working directory have newline characters in their name.

ls | sort -V

Would give you a numeric order.

In the zsh shell, the numericglobsort option (enabled with set -o numericglobsort) can be used for glob expansions to be sorted numerically by default, or you can use the n glob qualifier to select that order on a per-glob basis:

print -rC1 -- *.jpg(Nn)

(here also using the Nullglob qualifier to not complain if there's no match) would print raw on 1 Column the non-hidden files whose name ends in .jpg, sorted numerically.

To get the list in csv format, though, you'd need something like:

(){ print -rC1 -- \"${^argv//\"/\"\"}\"; } *.jpg(Nn)

That is, wrap the file names in quotes and escape the "s in the file names as "".

2
  • Note that the sort -V (regardless of the -V) example fails if filenames include newlines. This is what I came up with: find . -print0 | sort -Vz | tr '\0' '\n' which includes the dirname (here ./) in each entry. There are probably other methods Sep 12, 2022 at 16:03
  • @DennisWilliamson, I did mention the newline limitation. You could also do printf '%s\0' * | sort -Vz | tr '\0' '\n'. Sep 12, 2022 at 16:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .