/bin/ls sorts differently than just ls





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







6















$ ls |sort
xyz-0.0.1-1554490900.rpm
xyz-0.0.1-1554745305.rpm
xyz-0.0.1-1554751021.rpm
xyz-0.0.1-1555513460.rpm
xyz-0.0.1-1555951745.rpm
xyz-0.0.1-1554323568.rpm

$ /bin/ls |sort
xyz-0.0.1-1554323568.rpm
xyz-0.0.1-1554490900.rpm
xyz-0.0.1-1554745305.rpm
xyz-0.0.1-1554751021.rpm
xyz-0.0.1-1555513460.rpm
xyz-0.0.1-1555951745.rpm

$ which ls
alias ls='/bin/ls --color'
/bin/ls


Note that the sorting is different between the two commands (ls |sort results in incorrect sorting). This must be due to the color flag, but why?










share|improve this question





























    6















    $ ls |sort
    xyz-0.0.1-1554490900.rpm
    xyz-0.0.1-1554745305.rpm
    xyz-0.0.1-1554751021.rpm
    xyz-0.0.1-1555513460.rpm
    xyz-0.0.1-1555951745.rpm
    xyz-0.0.1-1554323568.rpm

    $ /bin/ls |sort
    xyz-0.0.1-1554323568.rpm
    xyz-0.0.1-1554490900.rpm
    xyz-0.0.1-1554745305.rpm
    xyz-0.0.1-1554751021.rpm
    xyz-0.0.1-1555513460.rpm
    xyz-0.0.1-1555951745.rpm

    $ which ls
    alias ls='/bin/ls --color'
    /bin/ls


    Note that the sorting is different between the two commands (ls |sort results in incorrect sorting). This must be due to the color flag, but why?










    share|improve this question

























      6












      6








      6


      1






      $ ls |sort
      xyz-0.0.1-1554490900.rpm
      xyz-0.0.1-1554745305.rpm
      xyz-0.0.1-1554751021.rpm
      xyz-0.0.1-1555513460.rpm
      xyz-0.0.1-1555951745.rpm
      xyz-0.0.1-1554323568.rpm

      $ /bin/ls |sort
      xyz-0.0.1-1554323568.rpm
      xyz-0.0.1-1554490900.rpm
      xyz-0.0.1-1554745305.rpm
      xyz-0.0.1-1554751021.rpm
      xyz-0.0.1-1555513460.rpm
      xyz-0.0.1-1555951745.rpm

      $ which ls
      alias ls='/bin/ls --color'
      /bin/ls


      Note that the sorting is different between the two commands (ls |sort results in incorrect sorting). This must be due to the color flag, but why?










      share|improve this question














      $ ls |sort
      xyz-0.0.1-1554490900.rpm
      xyz-0.0.1-1554745305.rpm
      xyz-0.0.1-1554751021.rpm
      xyz-0.0.1-1555513460.rpm
      xyz-0.0.1-1555951745.rpm
      xyz-0.0.1-1554323568.rpm

      $ /bin/ls |sort
      xyz-0.0.1-1554323568.rpm
      xyz-0.0.1-1554490900.rpm
      xyz-0.0.1-1554745305.rpm
      xyz-0.0.1-1554751021.rpm
      xyz-0.0.1-1555513460.rpm
      xyz-0.0.1-1555951745.rpm

      $ which ls
      alias ls='/bin/ls --color'
      /bin/ls


      Note that the sorting is different between the two commands (ls |sort results in incorrect sorting). This must be due to the color flag, but why?







      linux command-line bash unix






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 6 hours ago









      Josh M.Josh M.

      67451227




      67451227






















          2 Answers
          2






          active

          oldest

          votes


















          10














          Do:



          /bin/ls --color > file1
          /bin/ls > file2


          and compare content, you'll see the difference.



          Aliasing ls to /bin/ls --color=auto is likely better idea, it will stop ls from using color codes when not writing directly to terminal (like when piping to next program or writing to a file).






          share|improve this answer
























          • Thank you. I assume this is just a "known thing" and people understand ls + sorting should be done directly via /bin/ls vs. ls.

            – Josh M.
            5 hours ago






          • 4





            @JoshM. Well, sorting should be done by ls itself, if possible. Sorting with sort is parsing, not recommended in general.

            – Kamil Maciorowski
            5 hours ago






          • 2





            @JoshM., rather than using /bin/ls, change your alias to what Tomak suggested and you will get the better behavior automatically.

            – John1024
            5 hours ago






          • 1





            @JoshM. You can also use ls for an unaliased ls or command ls for /bin/ls.

            – Freddy
            5 hours ago








          • 2





            Yes, @Freddy, very true. But, using alias ls='/bin/ls --color' is just a recipe for trouble.

            – John1024
            5 hours ago



















          5














          In the sorted colored output ls|sort, we can see that the last line xyz-0.0.1-1554323568.rpm is the first line
          of the non-colored output. The other lines are sorted equally.



          If we have at a look at the colored escape codes (non-sorted), we can see that the first
          line starts with a different escape code ^[[0m. This is causing the wrong order when sorted (^[[01 before ^[[0m).



          $ /bin/ls --color xyz* | cat -A
          ^[[0m^[[01;31mxyz-0.0.1-1554323568.rpm^[[0m$
          ^[[01;31mxyz-0.0.1-1554490900.rpm^[[0m$
          ^[[01;31mxyz-0.0.1-1554745305.rpm^[[0m$
          ^[[01;31mxyz-0.0.1-1554751021.rpm^[[0m$
          ^[[01;31mxyz-0.0.1-1555513460.rpm^[[0m$
          ^[[01;31mxyz-0.0.1-1555951745.rpm^[[0m$





          share|improve this answer
























            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
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1428322%2fbin-ls-sorts-differently-than-just-ls%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









            10














            Do:



            /bin/ls --color > file1
            /bin/ls > file2


            and compare content, you'll see the difference.



            Aliasing ls to /bin/ls --color=auto is likely better idea, it will stop ls from using color codes when not writing directly to terminal (like when piping to next program or writing to a file).






            share|improve this answer
























            • Thank you. I assume this is just a "known thing" and people understand ls + sorting should be done directly via /bin/ls vs. ls.

              – Josh M.
              5 hours ago






            • 4





              @JoshM. Well, sorting should be done by ls itself, if possible. Sorting with sort is parsing, not recommended in general.

              – Kamil Maciorowski
              5 hours ago






            • 2





              @JoshM., rather than using /bin/ls, change your alias to what Tomak suggested and you will get the better behavior automatically.

              – John1024
              5 hours ago






            • 1





              @JoshM. You can also use ls for an unaliased ls or command ls for /bin/ls.

              – Freddy
              5 hours ago








            • 2





              Yes, @Freddy, very true. But, using alias ls='/bin/ls --color' is just a recipe for trouble.

              – John1024
              5 hours ago
















            10














            Do:



            /bin/ls --color > file1
            /bin/ls > file2


            and compare content, you'll see the difference.



            Aliasing ls to /bin/ls --color=auto is likely better idea, it will stop ls from using color codes when not writing directly to terminal (like when piping to next program or writing to a file).






            share|improve this answer
























            • Thank you. I assume this is just a "known thing" and people understand ls + sorting should be done directly via /bin/ls vs. ls.

              – Josh M.
              5 hours ago






            • 4





              @JoshM. Well, sorting should be done by ls itself, if possible. Sorting with sort is parsing, not recommended in general.

              – Kamil Maciorowski
              5 hours ago






            • 2





              @JoshM., rather than using /bin/ls, change your alias to what Tomak suggested and you will get the better behavior automatically.

              – John1024
              5 hours ago






            • 1





              @JoshM. You can also use ls for an unaliased ls or command ls for /bin/ls.

              – Freddy
              5 hours ago








            • 2





              Yes, @Freddy, very true. But, using alias ls='/bin/ls --color' is just a recipe for trouble.

              – John1024
              5 hours ago














            10












            10








            10







            Do:



            /bin/ls --color > file1
            /bin/ls > file2


            and compare content, you'll see the difference.



            Aliasing ls to /bin/ls --color=auto is likely better idea, it will stop ls from using color codes when not writing directly to terminal (like when piping to next program or writing to a file).






            share|improve this answer













            Do:



            /bin/ls --color > file1
            /bin/ls > file2


            and compare content, you'll see the difference.



            Aliasing ls to /bin/ls --color=auto is likely better idea, it will stop ls from using color codes when not writing directly to terminal (like when piping to next program or writing to a file).







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 6 hours ago









            TomekTomek

            48936




            48936













            • Thank you. I assume this is just a "known thing" and people understand ls + sorting should be done directly via /bin/ls vs. ls.

              – Josh M.
              5 hours ago






            • 4





              @JoshM. Well, sorting should be done by ls itself, if possible. Sorting with sort is parsing, not recommended in general.

              – Kamil Maciorowski
              5 hours ago






            • 2





              @JoshM., rather than using /bin/ls, change your alias to what Tomak suggested and you will get the better behavior automatically.

              – John1024
              5 hours ago






            • 1





              @JoshM. You can also use ls for an unaliased ls or command ls for /bin/ls.

              – Freddy
              5 hours ago








            • 2





              Yes, @Freddy, very true. But, using alias ls='/bin/ls --color' is just a recipe for trouble.

              – John1024
              5 hours ago



















            • Thank you. I assume this is just a "known thing" and people understand ls + sorting should be done directly via /bin/ls vs. ls.

              – Josh M.
              5 hours ago






            • 4





              @JoshM. Well, sorting should be done by ls itself, if possible. Sorting with sort is parsing, not recommended in general.

              – Kamil Maciorowski
              5 hours ago






            • 2





              @JoshM., rather than using /bin/ls, change your alias to what Tomak suggested and you will get the better behavior automatically.

              – John1024
              5 hours ago






            • 1





              @JoshM. You can also use ls for an unaliased ls or command ls for /bin/ls.

              – Freddy
              5 hours ago








            • 2





              Yes, @Freddy, very true. But, using alias ls='/bin/ls --color' is just a recipe for trouble.

              – John1024
              5 hours ago

















            Thank you. I assume this is just a "known thing" and people understand ls + sorting should be done directly via /bin/ls vs. ls.

            – Josh M.
            5 hours ago





            Thank you. I assume this is just a "known thing" and people understand ls + sorting should be done directly via /bin/ls vs. ls.

            – Josh M.
            5 hours ago




            4




            4





            @JoshM. Well, sorting should be done by ls itself, if possible. Sorting with sort is parsing, not recommended in general.

            – Kamil Maciorowski
            5 hours ago





            @JoshM. Well, sorting should be done by ls itself, if possible. Sorting with sort is parsing, not recommended in general.

            – Kamil Maciorowski
            5 hours ago




            2




            2





            @JoshM., rather than using /bin/ls, change your alias to what Tomak suggested and you will get the better behavior automatically.

            – John1024
            5 hours ago





            @JoshM., rather than using /bin/ls, change your alias to what Tomak suggested and you will get the better behavior automatically.

            – John1024
            5 hours ago




            1




            1





            @JoshM. You can also use ls for an unaliased ls or command ls for /bin/ls.

            – Freddy
            5 hours ago







            @JoshM. You can also use ls for an unaliased ls or command ls for /bin/ls.

            – Freddy
            5 hours ago






            2




            2





            Yes, @Freddy, very true. But, using alias ls='/bin/ls --color' is just a recipe for trouble.

            – John1024
            5 hours ago





            Yes, @Freddy, very true. But, using alias ls='/bin/ls --color' is just a recipe for trouble.

            – John1024
            5 hours ago













            5














            In the sorted colored output ls|sort, we can see that the last line xyz-0.0.1-1554323568.rpm is the first line
            of the non-colored output. The other lines are sorted equally.



            If we have at a look at the colored escape codes (non-sorted), we can see that the first
            line starts with a different escape code ^[[0m. This is causing the wrong order when sorted (^[[01 before ^[[0m).



            $ /bin/ls --color xyz* | cat -A
            ^[[0m^[[01;31mxyz-0.0.1-1554323568.rpm^[[0m$
            ^[[01;31mxyz-0.0.1-1554490900.rpm^[[0m$
            ^[[01;31mxyz-0.0.1-1554745305.rpm^[[0m$
            ^[[01;31mxyz-0.0.1-1554751021.rpm^[[0m$
            ^[[01;31mxyz-0.0.1-1555513460.rpm^[[0m$
            ^[[01;31mxyz-0.0.1-1555951745.rpm^[[0m$





            share|improve this answer




























              5














              In the sorted colored output ls|sort, we can see that the last line xyz-0.0.1-1554323568.rpm is the first line
              of the non-colored output. The other lines are sorted equally.



              If we have at a look at the colored escape codes (non-sorted), we can see that the first
              line starts with a different escape code ^[[0m. This is causing the wrong order when sorted (^[[01 before ^[[0m).



              $ /bin/ls --color xyz* | cat -A
              ^[[0m^[[01;31mxyz-0.0.1-1554323568.rpm^[[0m$
              ^[[01;31mxyz-0.0.1-1554490900.rpm^[[0m$
              ^[[01;31mxyz-0.0.1-1554745305.rpm^[[0m$
              ^[[01;31mxyz-0.0.1-1554751021.rpm^[[0m$
              ^[[01;31mxyz-0.0.1-1555513460.rpm^[[0m$
              ^[[01;31mxyz-0.0.1-1555951745.rpm^[[0m$





              share|improve this answer


























                5












                5








                5







                In the sorted colored output ls|sort, we can see that the last line xyz-0.0.1-1554323568.rpm is the first line
                of the non-colored output. The other lines are sorted equally.



                If we have at a look at the colored escape codes (non-sorted), we can see that the first
                line starts with a different escape code ^[[0m. This is causing the wrong order when sorted (^[[01 before ^[[0m).



                $ /bin/ls --color xyz* | cat -A
                ^[[0m^[[01;31mxyz-0.0.1-1554323568.rpm^[[0m$
                ^[[01;31mxyz-0.0.1-1554490900.rpm^[[0m$
                ^[[01;31mxyz-0.0.1-1554745305.rpm^[[0m$
                ^[[01;31mxyz-0.0.1-1554751021.rpm^[[0m$
                ^[[01;31mxyz-0.0.1-1555513460.rpm^[[0m$
                ^[[01;31mxyz-0.0.1-1555951745.rpm^[[0m$





                share|improve this answer













                In the sorted colored output ls|sort, we can see that the last line xyz-0.0.1-1554323568.rpm is the first line
                of the non-colored output. The other lines are sorted equally.



                If we have at a look at the colored escape codes (non-sorted), we can see that the first
                line starts with a different escape code ^[[0m. This is causing the wrong order when sorted (^[[01 before ^[[0m).



                $ /bin/ls --color xyz* | cat -A
                ^[[0m^[[01;31mxyz-0.0.1-1554323568.rpm^[[0m$
                ^[[01;31mxyz-0.0.1-1554490900.rpm^[[0m$
                ^[[01;31mxyz-0.0.1-1554745305.rpm^[[0m$
                ^[[01;31mxyz-0.0.1-1554751021.rpm^[[0m$
                ^[[01;31mxyz-0.0.1-1555513460.rpm^[[0m$
                ^[[01;31mxyz-0.0.1-1555951745.rpm^[[0m$






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 5 hours ago









                FreddyFreddy

                3535




                3535






























                    draft saved

                    draft discarded




















































                    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%2f1428322%2fbin-ls-sorts-differently-than-just-ls%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

                    數位音樂下載

                    When can things happen in Etherscan, such as the picture below?

                    格利澤436b