Why does the `ls` command sort files like this?Why does 'sort' ignore special characters, like the asterisk?Can I sort files A-Z and at the same time Z-A?Lotus notes 8.5 Inbox return to default sort and select next unread emailUnix shell: Multi-level sort in opposite directionsWhy does here-string argument to echo omit first word?What does “>> file command” do, and how does it differ from “command >> file”?Why does plink or bash not recognize command on first line of remote commands file?

Is a memoized pure function itself considered pure?

Hangman game in Python - need feedback on the quality of code

LINQ for generating all possible permutations

What's the point of fighting monsters in Zelda BoTW?

Why does the `ls` command sort files like this?

3D cryptic featuring Mao, Stalin and Simba's uncle

How many petaflops does it take to land on the moon? What does Artemis need with an Aitken?

Should open strings on guitar be tuned to the key of the song played?

If I said I had $100 when asked, but I actually had $200, would I be lying by omission?

Unlock your Lock

How can I draw lines between cells from two different tabulars to indicate correlation?

Is the internet in Madagascar faster than in UK?

Is there a word or phrase that means "use other people's wifi or Internet service without consent"?

Joining lists with same elements

Why is the UK so keen to remove the "backstop" when their leadership seems to think that no border will be needed in Northern Ireland?

Retroactively modifying humans for Earth?

Changing JPEG to RAW to use on Lightroom?

74S vs 74LS ICs

Open subspaces of CW complexes

Do you pay one or two mana to bounce a transformed Delver of Secrets with Repeal?

Why did my folder names end up like this, and how can I fix this using a script?

Weighted smooth histogram

How to prevent a hosting company from accessing a VM's encryption keys?

Why does matter stays collapsed following the supernova explosion?



Why does the `ls` command sort files like this?


Why does 'sort' ignore special characters, like the asterisk?Can I sort files A-Z and at the same time Z-A?Lotus notes 8.5 Inbox return to default sort and select next unread emailUnix shell: Multi-level sort in opposite directionsWhy does here-string argument to echo omit first word?What does “>> file command” do, and how does it differ from “command >> file”?Why does plink or bash not recognize command on first line of remote commands file?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








6















As I was trying to reverse engineer the ls command, I came upon an interesting behavior. When I make 3 files, foo.png, foopa.png, and fooqa.png, ls sorts them as foopa.png, foo.png, and fooqa.png. I also tried it using the .gif extension and it seems to be that it happens when p and q are replaced by the first letter of the extension and the next letter in the alphabet; so in the case of .gif it would be g and h. (fooga.gif, then foo.gif, then fooha.gif)



Why does it order the output this way?










share|improve this question









New contributor



mooncat39 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    Don't post links to images of text.

    – RalfFriedl
    8 hours ago











  • @RalfFriedl Oh sorry about that, I'll fix it

    – mooncat39
    8 hours ago

















6















As I was trying to reverse engineer the ls command, I came upon an interesting behavior. When I make 3 files, foo.png, foopa.png, and fooqa.png, ls sorts them as foopa.png, foo.png, and fooqa.png. I also tried it using the .gif extension and it seems to be that it happens when p and q are replaced by the first letter of the extension and the next letter in the alphabet; so in the case of .gif it would be g and h. (fooga.gif, then foo.gif, then fooha.gif)



Why does it order the output this way?










share|improve this question









New contributor



mooncat39 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    Don't post links to images of text.

    – RalfFriedl
    8 hours ago











  • @RalfFriedl Oh sorry about that, I'll fix it

    – mooncat39
    8 hours ago













6












6








6


2






As I was trying to reverse engineer the ls command, I came upon an interesting behavior. When I make 3 files, foo.png, foopa.png, and fooqa.png, ls sorts them as foopa.png, foo.png, and fooqa.png. I also tried it using the .gif extension and it seems to be that it happens when p and q are replaced by the first letter of the extension and the next letter in the alphabet; so in the case of .gif it would be g and h. (fooga.gif, then foo.gif, then fooha.gif)



Why does it order the output this way?










share|improve this question









New contributor



mooncat39 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











As I was trying to reverse engineer the ls command, I came upon an interesting behavior. When I make 3 files, foo.png, foopa.png, and fooqa.png, ls sorts them as foopa.png, foo.png, and fooqa.png. I also tried it using the .gif extension and it seems to be that it happens when p and q are replaced by the first letter of the extension and the next letter in the alphabet; so in the case of .gif it would be g and h. (fooga.gif, then foo.gif, then fooha.gif)



Why does it order the output this way?







linux command-line sorting ls coreutils






share|improve this question









New contributor



mooncat39 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



mooncat39 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 8 hours ago







mooncat39













New contributor



mooncat39 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 9 hours ago









mooncat39mooncat39

314 bronze badges




314 bronze badges




New contributor



mooncat39 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




mooncat39 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 1





    Don't post links to images of text.

    – RalfFriedl
    8 hours ago











  • @RalfFriedl Oh sorry about that, I'll fix it

    – mooncat39
    8 hours ago












  • 1





    Don't post links to images of text.

    – RalfFriedl
    8 hours ago











  • @RalfFriedl Oh sorry about that, I'll fix it

    – mooncat39
    8 hours ago







1




1





Don't post links to images of text.

– RalfFriedl
8 hours ago





Don't post links to images of text.

– RalfFriedl
8 hours ago













@RalfFriedl Oh sorry about that, I'll fix it

– mooncat39
8 hours ago





@RalfFriedl Oh sorry about that, I'll fix it

– mooncat39
8 hours ago










2 Answers
2






active

oldest

votes


















2















It depends on the collation order of your locale:



>LANG=en_IE.UTF-8 ls -1 foo*
foopa.png
foo.png
fooqa.png

>LANG=C ls -1 foo*
foo.png
foopa.png
fooqa.png


C collation order is purely alphabetical (ASCII order). Other collation orders (such as English) may consider spaces and special characters such as dots as separators and either handle "words" separately or just ignore these separators (which appears to be the case here).



Note that the non-UTF-8 locale sorts using alphabetic ASCII, too:



>LANG=en_IE ls -1 foo*
foo.png
foopa.png
fooqa.png






share|improve this answer


































    1















    ls sorts alphabetically according to strcoll() unless you tell it otherwise. It doesn't treat the period specially, so it won't sort by the "filename extension" after any periods that are there.



    It is perplexing that period comes between p and q in the sort order, though! In the default C locale, period comes before any letters (man ascii).



    Here's a concise puzzle for other answerers, which I confirmed with ltrace -e strcoll /bin/ls on Ubuntu 14.04:

    Why does f.g come before fga.g,

    but f.gif comes after fga.gif?






    share|improve this answer






















    • 1





      This can be explained if the collation order for english just ignores the dots. You get the same order for fg fgag fgif fgagif.

      – xenoid
      7 hours ago













    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "3"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    mooncat39 is a new contributor. Be nice, and check out our Code of Conduct.









    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1475533%2fwhy-does-the-ls-command-sort-files-like-this%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2















    It depends on the collation order of your locale:



    >LANG=en_IE.UTF-8 ls -1 foo*
    foopa.png
    foo.png
    fooqa.png

    >LANG=C ls -1 foo*
    foo.png
    foopa.png
    fooqa.png


    C collation order is purely alphabetical (ASCII order). Other collation orders (such as English) may consider spaces and special characters such as dots as separators and either handle "words" separately or just ignore these separators (which appears to be the case here).



    Note that the non-UTF-8 locale sorts using alphabetic ASCII, too:



    >LANG=en_IE ls -1 foo*
    foo.png
    foopa.png
    fooqa.png






    share|improve this answer































      2















      It depends on the collation order of your locale:



      >LANG=en_IE.UTF-8 ls -1 foo*
      foopa.png
      foo.png
      fooqa.png

      >LANG=C ls -1 foo*
      foo.png
      foopa.png
      fooqa.png


      C collation order is purely alphabetical (ASCII order). Other collation orders (such as English) may consider spaces and special characters such as dots as separators and either handle "words" separately or just ignore these separators (which appears to be the case here).



      Note that the non-UTF-8 locale sorts using alphabetic ASCII, too:



      >LANG=en_IE ls -1 foo*
      foo.png
      foopa.png
      fooqa.png






      share|improve this answer





























        2














        2










        2









        It depends on the collation order of your locale:



        >LANG=en_IE.UTF-8 ls -1 foo*
        foopa.png
        foo.png
        fooqa.png

        >LANG=C ls -1 foo*
        foo.png
        foopa.png
        fooqa.png


        C collation order is purely alphabetical (ASCII order). Other collation orders (such as English) may consider spaces and special characters such as dots as separators and either handle "words" separately or just ignore these separators (which appears to be the case here).



        Note that the non-UTF-8 locale sorts using alphabetic ASCII, too:



        >LANG=en_IE ls -1 foo*
        foo.png
        foopa.png
        fooqa.png






        share|improve this answer















        It depends on the collation order of your locale:



        >LANG=en_IE.UTF-8 ls -1 foo*
        foopa.png
        foo.png
        fooqa.png

        >LANG=C ls -1 foo*
        foo.png
        foopa.png
        fooqa.png


        C collation order is purely alphabetical (ASCII order). Other collation orders (such as English) may consider spaces and special characters such as dots as separators and either handle "words" separately or just ignore these separators (which appears to be the case here).



        Note that the non-UTF-8 locale sorts using alphabetic ASCII, too:



        >LANG=en_IE ls -1 foo*
        foo.png
        foopa.png
        fooqa.png







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 6 hours ago

























        answered 7 hours ago









        xenoidxenoid

        5,3283 gold badges10 silver badges21 bronze badges




        5,3283 gold badges10 silver badges21 bronze badges


























            1















            ls sorts alphabetically according to strcoll() unless you tell it otherwise. It doesn't treat the period specially, so it won't sort by the "filename extension" after any periods that are there.



            It is perplexing that period comes between p and q in the sort order, though! In the default C locale, period comes before any letters (man ascii).



            Here's a concise puzzle for other answerers, which I confirmed with ltrace -e strcoll /bin/ls on Ubuntu 14.04:

            Why does f.g come before fga.g,

            but f.gif comes after fga.gif?






            share|improve this answer






















            • 1





              This can be explained if the collation order for english just ignores the dots. You get the same order for fg fgag fgif fgagif.

              – xenoid
              7 hours ago















            1















            ls sorts alphabetically according to strcoll() unless you tell it otherwise. It doesn't treat the period specially, so it won't sort by the "filename extension" after any periods that are there.



            It is perplexing that period comes between p and q in the sort order, though! In the default C locale, period comes before any letters (man ascii).



            Here's a concise puzzle for other answerers, which I confirmed with ltrace -e strcoll /bin/ls on Ubuntu 14.04:

            Why does f.g come before fga.g,

            but f.gif comes after fga.gif?






            share|improve this answer






















            • 1





              This can be explained if the collation order for english just ignores the dots. You get the same order for fg fgag fgif fgagif.

              – xenoid
              7 hours ago













            1














            1










            1









            ls sorts alphabetically according to strcoll() unless you tell it otherwise. It doesn't treat the period specially, so it won't sort by the "filename extension" after any periods that are there.



            It is perplexing that period comes between p and q in the sort order, though! In the default C locale, period comes before any letters (man ascii).



            Here's a concise puzzle for other answerers, which I confirmed with ltrace -e strcoll /bin/ls on Ubuntu 14.04:

            Why does f.g come before fga.g,

            but f.gif comes after fga.gif?






            share|improve this answer















            ls sorts alphabetically according to strcoll() unless you tell it otherwise. It doesn't treat the period specially, so it won't sort by the "filename extension" after any periods that are there.



            It is perplexing that period comes between p and q in the sort order, though! In the default C locale, period comes before any letters (man ascii).



            Here's a concise puzzle for other answerers, which I confirmed with ltrace -e strcoll /bin/ls on Ubuntu 14.04:

            Why does f.g come before fga.g,

            but f.gif comes after fga.gif?







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 7 hours ago

























            answered 8 hours ago









            Camille GoudeseuneCamille Goudeseune

            8721 gold badge15 silver badges31 bronze badges




            8721 gold badge15 silver badges31 bronze badges










            • 1





              This can be explained if the collation order for english just ignores the dots. You get the same order for fg fgag fgif fgagif.

              – xenoid
              7 hours ago












            • 1





              This can be explained if the collation order for english just ignores the dots. You get the same order for fg fgag fgif fgagif.

              – xenoid
              7 hours ago







            1




            1





            This can be explained if the collation order for english just ignores the dots. You get the same order for fg fgag fgif fgagif.

            – xenoid
            7 hours ago





            This can be explained if the collation order for english just ignores the dots. You get the same order for fg fgag fgif fgagif.

            – xenoid
            7 hours ago










            mooncat39 is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            mooncat39 is a new contributor. Be nice, and check out our Code of Conduct.












            mooncat39 is a new contributor. Be nice, and check out our Code of Conduct.











            mooncat39 is a new contributor. Be nice, and check out our Code of Conduct.














            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1475533%2fwhy-does-the-ls-command-sort-files-like-this%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            ParseJSON using SSJSUsing AMPscript with SSJS ActivitiesHow to resubscribe a user in Marketing cloud using SSJS?Pulling Subscriber Status from Lists using SSJSRetrieving Emails using SSJSProblem in updating DE using SSJSUsing SSJS to send single email in Marketing CloudError adding EmailSendDefinition using SSJS

            Кампала Садржај Географија Географија Историја Становништво Привреда Партнерски градови Референце Спољашње везе Мени за навигацију0°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.340°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.34МедијиПодациЗванични веб-сајту

            19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу