2

Why are files listed alphabetically, ignoring file name length in the terminal?

Perhaps I shouldn't say "ignoring" file name length, but rather, why is there a difference in displaying files in the terminal vs. a GUI.

This is certainly a trivial question, but I've been a bit curious about this one for a while.

In the terminal, a vanilla ls command using the -l option (with no other sorting options specified) lists files in alphabetical order starting from the top line of the list moving down. Say I have a directory full of files created with the following:

$ touch file1{1..16}

ls in that same directory would display the following:

-rw-r--r--. 1 user user  0 May 24 11:14 file1
-rw-r--r--. 1 user user  0 May 24 11:14 file10
-rw-r--r--. 1 user user  0 May 24 11:14 file11
-rw-r--r--. 1 user user  0 May 24 11:14 file12
-rw-r--r--. 1 user user  0 May 24 11:14 file13
-rw-r--r--. 1 user user  0 May 24 11:14 file14
-rw-r--r--. 1 user user  0 May 24 11:14 file15
-rw-r--r--. 1 user user  0 May 24 11:14 file16
-rw-r--r--. 1 user user  0 May 24 11:14 file2
-rw-r--r--. 1 user user  0 May 24 11:14 file3
-rw-r--r--. 1 user user  0 May 24 11:14 file4
-rw-r--r--. 1 user user  0 May 24 11:14 file5
-rw-r--r--. 1 user user  0 May 24 11:14 file6
-rw-r--r--. 1 user user  0 May 24 11:14 file7
-rw-r--r--. 1 user user  0 May 24 11:14 file8
-rw-r--r--. 1 user user  0 May 24 11:14 file9

My question is why does "file10" follow "file1" in this way in the terminal? When viewing files in a details or list view ordered by name or type in a GUI environment, those same files are listed as "file1", "file2", "file3", etc.

Lists of files in a GUI seem to prioritize alphabetical order by file name length, listing files from smallest length to largest. Is this correct? Is there a more technical reason for this? Is the ls command "going out of its way" to order files the way it does, or likewise with a GUI?

0

1 Answer 1

3

The default ordering for ls is alphabetical. In this scenario, digits are not numbers just characters. So file1 is a shorter name than file10, but otherwise identical, and therefore comes before it in the list.

If you want natural versioned order you could try ls -l --sort=version (or ls -lv)

-rw-r--r--+ 1 roaima 0 May 24 18:50 file0
-rw-r--r--+ 1 roaima 0 May 24 18:50 file1
-rw-r--r--+ 1 roaima 0 May 24 18:50 file2
...
-rw-r--r--+ 1 roaima 0 May 24 18:50 file9
-rw-r--r--+ 1 roaima 0 May 24 18:50 file10
-rw-r--r--+ 1 roaima 0 May 24 18:50 file11
-rw-r--r--+ 1 roaima 0 May 24 18:50 file12
-rw-r--r--+ 1 roaima 0 May 24 18:50 file13

There are a number of other sorting options available in ls; see man ls for details.

4
  • 3
    "Lexicographic" rather than "alphabetic", but it's just words.
    – Kusalananda
    May 24, 2018 at 17:54
  • @Kusalananda thank you. if I could work out how to add that without adding unnecessary complexity for potentially non-native English speakers I would. (Feel free to edit if you can see how.) May 24, 2018 at 17:55
  • 1
    @roaima Ah, thank you! I see that files are listed in order by characters at the end of each string, up until the "next" character in alphabetical or numerical order. So files listed with a ton of 1's in their file name will continue until something like "file2" is displayed.
    – allenv
    May 24, 2018 at 17:56
  • 2
    @allenv exactly, yes. But actually it's slightly more complicated than that; you'll come across references to locale soon enough, which is the umbrella term that discusses how to sort non-English characters such as ø á and even Kanji. Some characters even have no sort order. May 24, 2018 at 17:58

You must log in to answer this question.

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