Sample Header Ad - 728x90

How do I make `ls` sort the same way Thunar sorts files?

9 votes
2 answers
954 views
The title is a summary of what I'm trying to achieve, but I'll give an example to illustrate what my problem is and how I've been trying to solve it. ### Example folder Let's say I've got a folder on a Linux system with the following files: .a, .A, .b, .B, a, A, b and B. ### Thunar When I open the folder in Thunar, my file manager of choice, the files display in this order:
.a
.A
.b
.B
a
A
b
B
This is an output that makes sense to me; first the hidden files (or directories), then sorted alphabetically (where the case is taken into account). Preferably, I'd have the upper-case files sorted before the lower-case ones, but it's not too bad. In other words, this is the output that I'm trying to achieve with ls. ### ls When I want to list the files of this folder via ls, this is what I get:
$ ls -lA
total 0
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 .a
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 a
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 .A
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 A
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 .b
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 b
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 .B
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 B
Here, the hidden files aren't sorted to the top, but the files overall are sorted in a 'sensible' alphabetical order. ### Experimenting with LC_ALL=C and LC_COLLATE=C A couple of solutions for sorting the hidden files to the top are to temporarily set either LC_ALL or LC_COLLATE to C (I'm struggling to see the difference between the two, so an explanation there would be much appreciated):
$ LC_ALL=C ls -lA
total 0
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 .A
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 .B
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 .a
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 .b
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 A
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 B
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 a
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 b
$ LC_COLLATE=C ls -lA
total 0
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 .A
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 .B
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 .a
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 .b
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 A
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 B
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 a
-rw-r--r-- 1 lucas lucas 0 Jan 26 14:58 b
As you can see, this does solve the hidden file problem, but the behaviour of the alphabetical sort is now inconsistent with how Thunar sorts files alphabetically. ### Questions So this begs the question: how do I get ls to sort in the same way as Thunar? Preferably, I want to avoid piping ls to another command like sort, since I'd like to alias this new command to "ls" itself. And if this isn't possible, how can I get Thunar to sort files the way ls would sort files (the LC_ALL=C/LC_COLLATE=C method seems nice enough to me)? By extension, I'd like to ask what the best practices are when sorting files alphabetically. The behaviour I've just described is just what seems sensible to me, but maybe it isn't after all?
Asked by Apollucas (93 rep)
Jan 26, 2025, 02:28 PM
Last activity: Jan 27, 2025, 10:23 AM