1

In Apple Finder.app special characters come before numerals which then are followed by alpha characters. It appears this is called "Natural Sort Order".

EDIT To clarify, I am using ls because I am trying to copy file names from Finder to a text document and have them sorted as Finder sorts .

To test I created 7 files with the names below using touch {[,1,2,3,A,B,C,}

[
1
2
3
A
B
C

Using ls -f the bracket ends up after numerals and before alpha characters instead of at the beginning.

0-zsh% ls 1 2 3 A B C [

0-zsh% ls -f . .. 1 A C 3 [ B 2

I have tried changing the locale in the shell without luck.

This is my locale:

0-zsh% locale                                  
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="C"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL="C"

These are possible english locale options

0-zsh% locale -a | grep -i en
en_NZ
en_US.US-ASCII
en_US.UTF-8
en_NZ.ISO8859-1
en_AU.US-ASCII
en_US
en_NZ.UTF-8
en_AU.ISO8859-15
en_US.ISO8859-15
en_NZ.ISO8859-15
en_AU.UTF-8
en_CA
en_NZ.US-ASCII
en_GB.ISO8859-1
en_CA.US-ASCII
en_CA.ISO8859-15
en_US.ISO8859-1
en_GB.UTF-8
en_GB.US-ASCII
en_AU
en_GB
en_CA.UTF-8
en_IE.UTF-8
en_CA.ISO8859-1
en_AU.ISO8859-1
en_IE
en_GB.ISO8859-15

https://www.engadget.com/2008-06-24-mac-101-finder-filename-sorting.html

1 Answer 1

2

This should work for you on the command line.

I used echo instead of ls in the following command to produce the list.

echo -e "C\nA\nB\n2\n1\n]\nb\n3\nD\n[\na"

C
A
B
2
1
]
b
3
D
[
a

echo -e "C\nA\nB\n2\n1\n]\nb\n3\nD\n[\na" |sort -df produces…

[
]
1
2
3
A
a
B
b
C
D

echo -e "C\nA\nB\n2\n1\n]\nb\n3\nD\n[\na" |sort -d produces…

[
]
1
2
3
A
B
C
D
a
b
1
  • If you change your answer to use ls instead of echo I will mark it correct. I clarified in the original post that this was for needed for Finder file names. I used ls | sort -d and that indeed sorted as Finder does. Thanks!
    – John
    Oct 19, 2022 at 14:24

You must log in to answer this question.

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