How to make ls sort by file extension and then name?












38














By default the ls command sorts just by filename, but I want directories to appear before other file types. I might even want files to be sorted by extension, like the way Windows explorer sorts by the type column. Is there a way to do something similar with ls?










share|improve this question















migrated from stackoverflow.com Aug 7 '09 at 22:34


This question came from our site for professional and enthusiast programmers.




















    38














    By default the ls command sorts just by filename, but I want directories to appear before other file types. I might even want files to be sorted by extension, like the way Windows explorer sorts by the type column. Is there a way to do something similar with ls?










    share|improve this question















    migrated from stackoverflow.com Aug 7 '09 at 22:34


    This question came from our site for professional and enthusiast programmers.


















      38












      38








      38


      7





      By default the ls command sorts just by filename, but I want directories to appear before other file types. I might even want files to be sorted by extension, like the way Windows explorer sorts by the type column. Is there a way to do something similar with ls?










      share|improve this question















      By default the ls command sorts just by filename, but I want directories to appear before other file types. I might even want files to be sorted by extension, like the way Windows explorer sorts by the type column. Is there a way to do something similar with ls?







      linux shell






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 3 '17 at 11:05









      Chris Stryczynski

      1175




      1175










      asked Aug 7 '09 at 18:23









      allyourcode

      298136




      298136




      migrated from stackoverflow.com Aug 7 '09 at 22:34


      This question came from our site for professional and enthusiast programmers.






      migrated from stackoverflow.com Aug 7 '09 at 22:34


      This question came from our site for professional and enthusiast programmers.
























          8 Answers
          8






          active

          oldest

          votes


















          37














          I think the complete answer is more of a combination of the above.



          -X (later --sort=extension) has been supported in Linux since at least FC3 and will sort based on extension. --group-directories-first was added more recently (maybe around FC8?). However, combining the two doesn't seem to work (at least on FC8).



          The primary issue seems to be with the use of singular primary sort keys. See this mailing list discussion for some insight into it.






          share|improve this answer



















          • 4




            I know this post is rather old, but to others coming here from e.g. Google (like I did): The combination of --sort=extension and --group-directories-first (or only --group-directories) works fine for me on Ubuntu 12.10. Worth a shot on your machine too! =)
            – Tomas Aschan
            Mar 29 '13 at 11:27





















          20














          On Linux,



          $ ls --group-directories-first


          (man ls is your friend!)






          share|improve this answer



















          • 1




            upvoted IN SPITE of the rtfm comment.
            – Sridhar-Sarnobat
            May 14 '16 at 22:14










          • gls --group-directories-first on Mac if you install GNU Core utils over homebrew
            – To Kra
            Mar 1 '17 at 9:32



















          13














          -X is the option you're looking for:



          ls -lX





          share|improve this answer































            11














            On bash, this will sort of work:



            $ ls | rev | sort | rev


            From man rev:



             The rev utility copies the specified files to the standard output,
            reversing the order of characters in every line. If no files are speci-
            fied, the standard input is read.


            So
            1. ls gives its output, with any flags you want
            2. each line is reversed
            3. then they're sorted
            4. and reversed again
            5. like this:




            1. like this:

            2. each line is reversed

            3. then they're sorted

            4. and reversed again
              So

            5. ls gives its output, with any flags you want


            Or, more to the point, as below. They're sorted by last character, then next-to-last, etc. All the .rtf files, for example, are listed together, after a .save file and another file with no extension whose name ends in 'e'. Then come .png files, and so on. This will also work with ls -l, because the extension is normally the last thing on the line (exceptions if you have lines like "tmp@ -> /home/jones/tmp", where links are followed by their targets).



             $ ls | rev|sort|rev
            cslu1
            ls.mp2
            ls.mp3
            ls.mp4
            trees_110214-15
            PAT
            CSLU
            Proxy Form.doc
            finannbyid
            toannbyid
            101209ssi.txt.save
            to_annotate_size
            Matas-time-by-week-integration2.rtf
            cyp3.rtf
            data-dir-scan.perl.doc.rtf
            whence-r21-numid.rtf
            platypus.rtf
            Screen shot 2011-01-21 at 2.17.50 PM.png
            emacs print help.png
            log
            new_month_log
            special
            Google-ngram-critique.html
            perl_path.html
            nl
            DWE_BEN_89808.2.ann
            foo
            d.o.foo
            100811_from_iMac_Documents_in_dock.zip
            to-palikir.zip
            tmp
            file-cleanup
            bar
            data-scan-docs
            cmp-mg-ann-numids
            finished_numids
            to_annotate_numids
            manls.ps
            Mike_address_ticket
            cyp2.out
            cyp3.out
            locate-cyp.out
            manls.out
            DWE_BEN_89808.2.text
            tag2.txt
            l2.txt
            du-h-d3.txt
            finished_ann_numids_110407_1714.txt
            finished_all_numids_110407_1718.txt
            data-dir-scan.perl.doc.txt
            whence-r21-numid.txt
            finannid.txt
            toannid.txt
            b9-workspace-anndiff.txt
            tag.txt
            duh.txt
            d.o-mail.txt
            safextn.txt
            mg3longhdr.txt
            finished_numids.txt
            41692-langnames.txt
            TimeAnnotationGuidelines.txt
            41langs.txt
            thing4-homedir-links.txt
            bnlinks.txt
            grants.txt
            mata-file-reports.txt
            logx.txt
            logx
            b9-workspace-anndiff.txt~
            bnlinks.txt~





            share|improve this answer



















            • 1




              Wouldn't list directories first, though.
              – CarlF
              Jun 3 '11 at 21:29










            • it will if you ls -F
              – glenn jackman
              Jun 4 '11 at 14:55










            • +1 for the effort...
              – varun
              Apr 3 at 9:12










            • @glennjackman that's better. should be in the answer.
              – tjt263
              Sep 9 at 2:47










            • It would be nice (especially with -F) if piping it through rev & sort didn't strip the colour (-G).
              – tjt263
              Sep 9 at 2:48



















            3














            If you're not on linux,



            ls -l |sort -d -k 1.1,1.1r -k 9 |awk '{print $9}'


            should sort directories first (let me know if I'm wrong). Doesn't sort by extension, though: you have to make the awk statement a lot busier if you want to do that ...





            To also make it work with names containing spaces, I would probably replace the awk with something like sed -E -e 's/([^ ]+[ ]+){8}//' to strip out the first 8 fields instead of printing the 9th






            share|improve this answer























            • actually, I'm pretty sure this will work on linux, too, but as others have stated you have some built-in support there.
              – Zac Thompson
              Oct 27 '09 at 5:06






            • 1




              Works on a Mac. Note that if you have CLICOLOR turned on this will remove the coloring.
              – eykanal
              Mar 30 '11 at 14:36






            • 1




              will give incorrect results for filenames with spaces.
              – glenn jackman
              Jun 4 '11 at 14:53










            • @glenn, you are right ... if this was a concern I would probably replace the awk with something like sed -E -e 's/([^ ]+[ ]+){8}//' to strip out the first 8 fields instead of printing the 9th
              – Zac Thompson
              Jun 4 '11 at 18:29










            • ls -l |sort -d -k 1.1,1.1r -k 9 | cut -c 50-
              – nohillside
              Feb 10 '12 at 20:51



















            2














            If you're running on Linux, GNU ls supports the --sort option:-



            ls --sort=extension





            share|improve this answer





















            • Not what was asked for.
              – Sridhar-Sarnobat
              May 14 '16 at 21:05



















            0














            A good approach is using two commands at once, separating folders at first, then the sorted files by extensions, like so:



            ls -p | grep /;ls -p | grep -v / | rev | sort | rev





            share|improve this answer































              -1














              I added to my .bashrc (linux) the line



              alias lx = "ls -X"


              that way I just type lx and get it sorted by extension.






              share|improve this answer





















              • So the answer to the question is use ls -X :)
                – Andrew
                Jan 23 '13 at 20:23










              • Not what was asked for
                – Sridhar-Sarnobat
                May 14 '16 at 21:05











              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%2f19781%2fhow-to-make-ls-sort-by-file-extension-and-then-name%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              8 Answers
              8






              active

              oldest

              votes








              8 Answers
              8






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              37














              I think the complete answer is more of a combination of the above.



              -X (later --sort=extension) has been supported in Linux since at least FC3 and will sort based on extension. --group-directories-first was added more recently (maybe around FC8?). However, combining the two doesn't seem to work (at least on FC8).



              The primary issue seems to be with the use of singular primary sort keys. See this mailing list discussion for some insight into it.






              share|improve this answer



















              • 4




                I know this post is rather old, but to others coming here from e.g. Google (like I did): The combination of --sort=extension and --group-directories-first (or only --group-directories) works fine for me on Ubuntu 12.10. Worth a shot on your machine too! =)
                – Tomas Aschan
                Mar 29 '13 at 11:27


















              37














              I think the complete answer is more of a combination of the above.



              -X (later --sort=extension) has been supported in Linux since at least FC3 and will sort based on extension. --group-directories-first was added more recently (maybe around FC8?). However, combining the two doesn't seem to work (at least on FC8).



              The primary issue seems to be with the use of singular primary sort keys. See this mailing list discussion for some insight into it.






              share|improve this answer



















              • 4




                I know this post is rather old, but to others coming here from e.g. Google (like I did): The combination of --sort=extension and --group-directories-first (or only --group-directories) works fine for me on Ubuntu 12.10. Worth a shot on your machine too! =)
                – Tomas Aschan
                Mar 29 '13 at 11:27
















              37












              37








              37






              I think the complete answer is more of a combination of the above.



              -X (later --sort=extension) has been supported in Linux since at least FC3 and will sort based on extension. --group-directories-first was added more recently (maybe around FC8?). However, combining the two doesn't seem to work (at least on FC8).



              The primary issue seems to be with the use of singular primary sort keys. See this mailing list discussion for some insight into it.






              share|improve this answer














              I think the complete answer is more of a combination of the above.



              -X (later --sort=extension) has been supported in Linux since at least FC3 and will sort based on extension. --group-directories-first was added more recently (maybe around FC8?). However, combining the two doesn't seem to work (at least on FC8).



              The primary issue seems to be with the use of singular primary sort keys. See this mailing list discussion for some insight into it.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Aug 31 '15 at 22:55









              JakeGould

              30.9k1093137




              30.9k1093137










              answered Aug 7 '09 at 20:24







              CS















              • 4




                I know this post is rather old, but to others coming here from e.g. Google (like I did): The combination of --sort=extension and --group-directories-first (or only --group-directories) works fine for me on Ubuntu 12.10. Worth a shot on your machine too! =)
                – Tomas Aschan
                Mar 29 '13 at 11:27
















              • 4




                I know this post is rather old, but to others coming here from e.g. Google (like I did): The combination of --sort=extension and --group-directories-first (or only --group-directories) works fine for me on Ubuntu 12.10. Worth a shot on your machine too! =)
                – Tomas Aschan
                Mar 29 '13 at 11:27










              4




              4




              I know this post is rather old, but to others coming here from e.g. Google (like I did): The combination of --sort=extension and --group-directories-first (or only --group-directories) works fine for me on Ubuntu 12.10. Worth a shot on your machine too! =)
              – Tomas Aschan
              Mar 29 '13 at 11:27






              I know this post is rather old, but to others coming here from e.g. Google (like I did): The combination of --sort=extension and --group-directories-first (or only --group-directories) works fine for me on Ubuntu 12.10. Worth a shot on your machine too! =)
              – Tomas Aschan
              Mar 29 '13 at 11:27















              20














              On Linux,



              $ ls --group-directories-first


              (man ls is your friend!)






              share|improve this answer



















              • 1




                upvoted IN SPITE of the rtfm comment.
                – Sridhar-Sarnobat
                May 14 '16 at 22:14










              • gls --group-directories-first on Mac if you install GNU Core utils over homebrew
                – To Kra
                Mar 1 '17 at 9:32
















              20














              On Linux,



              $ ls --group-directories-first


              (man ls is your friend!)






              share|improve this answer



















              • 1




                upvoted IN SPITE of the rtfm comment.
                – Sridhar-Sarnobat
                May 14 '16 at 22:14










              • gls --group-directories-first on Mac if you install GNU Core utils over homebrew
                – To Kra
                Mar 1 '17 at 9:32














              20












              20








              20






              On Linux,



              $ ls --group-directories-first


              (man ls is your friend!)






              share|improve this answer














              On Linux,



              $ ls --group-directories-first


              (man ls is your friend!)







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Aug 31 '15 at 22:51









              JakeGould

              30.9k1093137




              30.9k1093137










              answered Aug 7 '09 at 18:29







              Ned Deily















              • 1




                upvoted IN SPITE of the rtfm comment.
                – Sridhar-Sarnobat
                May 14 '16 at 22:14










              • gls --group-directories-first on Mac if you install GNU Core utils over homebrew
                – To Kra
                Mar 1 '17 at 9:32














              • 1




                upvoted IN SPITE of the rtfm comment.
                – Sridhar-Sarnobat
                May 14 '16 at 22:14










              • gls --group-directories-first on Mac if you install GNU Core utils over homebrew
                – To Kra
                Mar 1 '17 at 9:32








              1




              1




              upvoted IN SPITE of the rtfm comment.
              – Sridhar-Sarnobat
              May 14 '16 at 22:14




              upvoted IN SPITE of the rtfm comment.
              – Sridhar-Sarnobat
              May 14 '16 at 22:14












              gls --group-directories-first on Mac if you install GNU Core utils over homebrew
              – To Kra
              Mar 1 '17 at 9:32




              gls --group-directories-first on Mac if you install GNU Core utils over homebrew
              – To Kra
              Mar 1 '17 at 9:32











              13














              -X is the option you're looking for:



              ls -lX





              share|improve this answer




























                13














                -X is the option you're looking for:



                ls -lX





                share|improve this answer


























                  13












                  13








                  13






                  -X is the option you're looking for:



                  ls -lX





                  share|improve this answer














                  -X is the option you're looking for:



                  ls -lX






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jun 29 '15 at 14:42









                  sixtyfootersdude

                  3,160124065




                  3,160124065










                  answered Aug 7 '09 at 18:26









                  Carl Manaster

                  28126




                  28126























                      11














                      On bash, this will sort of work:



                      $ ls | rev | sort | rev


                      From man rev:



                       The rev utility copies the specified files to the standard output,
                      reversing the order of characters in every line. If no files are speci-
                      fied, the standard input is read.


                      So
                      1. ls gives its output, with any flags you want
                      2. each line is reversed
                      3. then they're sorted
                      4. and reversed again
                      5. like this:




                      1. like this:

                      2. each line is reversed

                      3. then they're sorted

                      4. and reversed again
                        So

                      5. ls gives its output, with any flags you want


                      Or, more to the point, as below. They're sorted by last character, then next-to-last, etc. All the .rtf files, for example, are listed together, after a .save file and another file with no extension whose name ends in 'e'. Then come .png files, and so on. This will also work with ls -l, because the extension is normally the last thing on the line (exceptions if you have lines like "tmp@ -> /home/jones/tmp", where links are followed by their targets).



                       $ ls | rev|sort|rev
                      cslu1
                      ls.mp2
                      ls.mp3
                      ls.mp4
                      trees_110214-15
                      PAT
                      CSLU
                      Proxy Form.doc
                      finannbyid
                      toannbyid
                      101209ssi.txt.save
                      to_annotate_size
                      Matas-time-by-week-integration2.rtf
                      cyp3.rtf
                      data-dir-scan.perl.doc.rtf
                      whence-r21-numid.rtf
                      platypus.rtf
                      Screen shot 2011-01-21 at 2.17.50 PM.png
                      emacs print help.png
                      log
                      new_month_log
                      special
                      Google-ngram-critique.html
                      perl_path.html
                      nl
                      DWE_BEN_89808.2.ann
                      foo
                      d.o.foo
                      100811_from_iMac_Documents_in_dock.zip
                      to-palikir.zip
                      tmp
                      file-cleanup
                      bar
                      data-scan-docs
                      cmp-mg-ann-numids
                      finished_numids
                      to_annotate_numids
                      manls.ps
                      Mike_address_ticket
                      cyp2.out
                      cyp3.out
                      locate-cyp.out
                      manls.out
                      DWE_BEN_89808.2.text
                      tag2.txt
                      l2.txt
                      du-h-d3.txt
                      finished_ann_numids_110407_1714.txt
                      finished_all_numids_110407_1718.txt
                      data-dir-scan.perl.doc.txt
                      whence-r21-numid.txt
                      finannid.txt
                      toannid.txt
                      b9-workspace-anndiff.txt
                      tag.txt
                      duh.txt
                      d.o-mail.txt
                      safextn.txt
                      mg3longhdr.txt
                      finished_numids.txt
                      41692-langnames.txt
                      TimeAnnotationGuidelines.txt
                      41langs.txt
                      thing4-homedir-links.txt
                      bnlinks.txt
                      grants.txt
                      mata-file-reports.txt
                      logx.txt
                      logx
                      b9-workspace-anndiff.txt~
                      bnlinks.txt~





                      share|improve this answer



















                      • 1




                        Wouldn't list directories first, though.
                        – CarlF
                        Jun 3 '11 at 21:29










                      • it will if you ls -F
                        – glenn jackman
                        Jun 4 '11 at 14:55










                      • +1 for the effort...
                        – varun
                        Apr 3 at 9:12










                      • @glennjackman that's better. should be in the answer.
                        – tjt263
                        Sep 9 at 2:47










                      • It would be nice (especially with -F) if piping it through rev & sort didn't strip the colour (-G).
                        – tjt263
                        Sep 9 at 2:48
















                      11














                      On bash, this will sort of work:



                      $ ls | rev | sort | rev


                      From man rev:



                       The rev utility copies the specified files to the standard output,
                      reversing the order of characters in every line. If no files are speci-
                      fied, the standard input is read.


                      So
                      1. ls gives its output, with any flags you want
                      2. each line is reversed
                      3. then they're sorted
                      4. and reversed again
                      5. like this:




                      1. like this:

                      2. each line is reversed

                      3. then they're sorted

                      4. and reversed again
                        So

                      5. ls gives its output, with any flags you want


                      Or, more to the point, as below. They're sorted by last character, then next-to-last, etc. All the .rtf files, for example, are listed together, after a .save file and another file with no extension whose name ends in 'e'. Then come .png files, and so on. This will also work with ls -l, because the extension is normally the last thing on the line (exceptions if you have lines like "tmp@ -> /home/jones/tmp", where links are followed by their targets).



                       $ ls | rev|sort|rev
                      cslu1
                      ls.mp2
                      ls.mp3
                      ls.mp4
                      trees_110214-15
                      PAT
                      CSLU
                      Proxy Form.doc
                      finannbyid
                      toannbyid
                      101209ssi.txt.save
                      to_annotate_size
                      Matas-time-by-week-integration2.rtf
                      cyp3.rtf
                      data-dir-scan.perl.doc.rtf
                      whence-r21-numid.rtf
                      platypus.rtf
                      Screen shot 2011-01-21 at 2.17.50 PM.png
                      emacs print help.png
                      log
                      new_month_log
                      special
                      Google-ngram-critique.html
                      perl_path.html
                      nl
                      DWE_BEN_89808.2.ann
                      foo
                      d.o.foo
                      100811_from_iMac_Documents_in_dock.zip
                      to-palikir.zip
                      tmp
                      file-cleanup
                      bar
                      data-scan-docs
                      cmp-mg-ann-numids
                      finished_numids
                      to_annotate_numids
                      manls.ps
                      Mike_address_ticket
                      cyp2.out
                      cyp3.out
                      locate-cyp.out
                      manls.out
                      DWE_BEN_89808.2.text
                      tag2.txt
                      l2.txt
                      du-h-d3.txt
                      finished_ann_numids_110407_1714.txt
                      finished_all_numids_110407_1718.txt
                      data-dir-scan.perl.doc.txt
                      whence-r21-numid.txt
                      finannid.txt
                      toannid.txt
                      b9-workspace-anndiff.txt
                      tag.txt
                      duh.txt
                      d.o-mail.txt
                      safextn.txt
                      mg3longhdr.txt
                      finished_numids.txt
                      41692-langnames.txt
                      TimeAnnotationGuidelines.txt
                      41langs.txt
                      thing4-homedir-links.txt
                      bnlinks.txt
                      grants.txt
                      mata-file-reports.txt
                      logx.txt
                      logx
                      b9-workspace-anndiff.txt~
                      bnlinks.txt~





                      share|improve this answer



















                      • 1




                        Wouldn't list directories first, though.
                        – CarlF
                        Jun 3 '11 at 21:29










                      • it will if you ls -F
                        – glenn jackman
                        Jun 4 '11 at 14:55










                      • +1 for the effort...
                        – varun
                        Apr 3 at 9:12










                      • @glennjackman that's better. should be in the answer.
                        – tjt263
                        Sep 9 at 2:47










                      • It would be nice (especially with -F) if piping it through rev & sort didn't strip the colour (-G).
                        – tjt263
                        Sep 9 at 2:48














                      11












                      11








                      11






                      On bash, this will sort of work:



                      $ ls | rev | sort | rev


                      From man rev:



                       The rev utility copies the specified files to the standard output,
                      reversing the order of characters in every line. If no files are speci-
                      fied, the standard input is read.


                      So
                      1. ls gives its output, with any flags you want
                      2. each line is reversed
                      3. then they're sorted
                      4. and reversed again
                      5. like this:




                      1. like this:

                      2. each line is reversed

                      3. then they're sorted

                      4. and reversed again
                        So

                      5. ls gives its output, with any flags you want


                      Or, more to the point, as below. They're sorted by last character, then next-to-last, etc. All the .rtf files, for example, are listed together, after a .save file and another file with no extension whose name ends in 'e'. Then come .png files, and so on. This will also work with ls -l, because the extension is normally the last thing on the line (exceptions if you have lines like "tmp@ -> /home/jones/tmp", where links are followed by their targets).



                       $ ls | rev|sort|rev
                      cslu1
                      ls.mp2
                      ls.mp3
                      ls.mp4
                      trees_110214-15
                      PAT
                      CSLU
                      Proxy Form.doc
                      finannbyid
                      toannbyid
                      101209ssi.txt.save
                      to_annotate_size
                      Matas-time-by-week-integration2.rtf
                      cyp3.rtf
                      data-dir-scan.perl.doc.rtf
                      whence-r21-numid.rtf
                      platypus.rtf
                      Screen shot 2011-01-21 at 2.17.50 PM.png
                      emacs print help.png
                      log
                      new_month_log
                      special
                      Google-ngram-critique.html
                      perl_path.html
                      nl
                      DWE_BEN_89808.2.ann
                      foo
                      d.o.foo
                      100811_from_iMac_Documents_in_dock.zip
                      to-palikir.zip
                      tmp
                      file-cleanup
                      bar
                      data-scan-docs
                      cmp-mg-ann-numids
                      finished_numids
                      to_annotate_numids
                      manls.ps
                      Mike_address_ticket
                      cyp2.out
                      cyp3.out
                      locate-cyp.out
                      manls.out
                      DWE_BEN_89808.2.text
                      tag2.txt
                      l2.txt
                      du-h-d3.txt
                      finished_ann_numids_110407_1714.txt
                      finished_all_numids_110407_1718.txt
                      data-dir-scan.perl.doc.txt
                      whence-r21-numid.txt
                      finannid.txt
                      toannid.txt
                      b9-workspace-anndiff.txt
                      tag.txt
                      duh.txt
                      d.o-mail.txt
                      safextn.txt
                      mg3longhdr.txt
                      finished_numids.txt
                      41692-langnames.txt
                      TimeAnnotationGuidelines.txt
                      41langs.txt
                      thing4-homedir-links.txt
                      bnlinks.txt
                      grants.txt
                      mata-file-reports.txt
                      logx.txt
                      logx
                      b9-workspace-anndiff.txt~
                      bnlinks.txt~





                      share|improve this answer














                      On bash, this will sort of work:



                      $ ls | rev | sort | rev


                      From man rev:



                       The rev utility copies the specified files to the standard output,
                      reversing the order of characters in every line. If no files are speci-
                      fied, the standard input is read.


                      So
                      1. ls gives its output, with any flags you want
                      2. each line is reversed
                      3. then they're sorted
                      4. and reversed again
                      5. like this:




                      1. like this:

                      2. each line is reversed

                      3. then they're sorted

                      4. and reversed again
                        So

                      5. ls gives its output, with any flags you want


                      Or, more to the point, as below. They're sorted by last character, then next-to-last, etc. All the .rtf files, for example, are listed together, after a .save file and another file with no extension whose name ends in 'e'. Then come .png files, and so on. This will also work with ls -l, because the extension is normally the last thing on the line (exceptions if you have lines like "tmp@ -> /home/jones/tmp", where links are followed by their targets).



                       $ ls | rev|sort|rev
                      cslu1
                      ls.mp2
                      ls.mp3
                      ls.mp4
                      trees_110214-15
                      PAT
                      CSLU
                      Proxy Form.doc
                      finannbyid
                      toannbyid
                      101209ssi.txt.save
                      to_annotate_size
                      Matas-time-by-week-integration2.rtf
                      cyp3.rtf
                      data-dir-scan.perl.doc.rtf
                      whence-r21-numid.rtf
                      platypus.rtf
                      Screen shot 2011-01-21 at 2.17.50 PM.png
                      emacs print help.png
                      log
                      new_month_log
                      special
                      Google-ngram-critique.html
                      perl_path.html
                      nl
                      DWE_BEN_89808.2.ann
                      foo
                      d.o.foo
                      100811_from_iMac_Documents_in_dock.zip
                      to-palikir.zip
                      tmp
                      file-cleanup
                      bar
                      data-scan-docs
                      cmp-mg-ann-numids
                      finished_numids
                      to_annotate_numids
                      manls.ps
                      Mike_address_ticket
                      cyp2.out
                      cyp3.out
                      locate-cyp.out
                      manls.out
                      DWE_BEN_89808.2.text
                      tag2.txt
                      l2.txt
                      du-h-d3.txt
                      finished_ann_numids_110407_1714.txt
                      finished_all_numids_110407_1718.txt
                      data-dir-scan.perl.doc.txt
                      whence-r21-numid.txt
                      finannid.txt
                      toannid.txt
                      b9-workspace-anndiff.txt
                      tag.txt
                      duh.txt
                      d.o-mail.txt
                      safextn.txt
                      mg3longhdr.txt
                      finished_numids.txt
                      41692-langnames.txt
                      TimeAnnotationGuidelines.txt
                      41langs.txt
                      thing4-homedir-links.txt
                      bnlinks.txt
                      grants.txt
                      mata-file-reports.txt
                      logx.txt
                      logx
                      b9-workspace-anndiff.txt~
                      bnlinks.txt~






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Oct 1 '15 at 11:15









                      bertieb

                      5,537112342




                      5,537112342










                      answered Jun 3 '11 at 21:22









                      M. Mandel

                      11112




                      11112








                      • 1




                        Wouldn't list directories first, though.
                        – CarlF
                        Jun 3 '11 at 21:29










                      • it will if you ls -F
                        – glenn jackman
                        Jun 4 '11 at 14:55










                      • +1 for the effort...
                        – varun
                        Apr 3 at 9:12










                      • @glennjackman that's better. should be in the answer.
                        – tjt263
                        Sep 9 at 2:47










                      • It would be nice (especially with -F) if piping it through rev & sort didn't strip the colour (-G).
                        – tjt263
                        Sep 9 at 2:48














                      • 1




                        Wouldn't list directories first, though.
                        – CarlF
                        Jun 3 '11 at 21:29










                      • it will if you ls -F
                        – glenn jackman
                        Jun 4 '11 at 14:55










                      • +1 for the effort...
                        – varun
                        Apr 3 at 9:12










                      • @glennjackman that's better. should be in the answer.
                        – tjt263
                        Sep 9 at 2:47










                      • It would be nice (especially with -F) if piping it through rev & sort didn't strip the colour (-G).
                        – tjt263
                        Sep 9 at 2:48








                      1




                      1




                      Wouldn't list directories first, though.
                      – CarlF
                      Jun 3 '11 at 21:29




                      Wouldn't list directories first, though.
                      – CarlF
                      Jun 3 '11 at 21:29












                      it will if you ls -F
                      – glenn jackman
                      Jun 4 '11 at 14:55




                      it will if you ls -F
                      – glenn jackman
                      Jun 4 '11 at 14:55












                      +1 for the effort...
                      – varun
                      Apr 3 at 9:12




                      +1 for the effort...
                      – varun
                      Apr 3 at 9:12












                      @glennjackman that's better. should be in the answer.
                      – tjt263
                      Sep 9 at 2:47




                      @glennjackman that's better. should be in the answer.
                      – tjt263
                      Sep 9 at 2:47












                      It would be nice (especially with -F) if piping it through rev & sort didn't strip the colour (-G).
                      – tjt263
                      Sep 9 at 2:48




                      It would be nice (especially with -F) if piping it through rev & sort didn't strip the colour (-G).
                      – tjt263
                      Sep 9 at 2:48











                      3














                      If you're not on linux,



                      ls -l |sort -d -k 1.1,1.1r -k 9 |awk '{print $9}'


                      should sort directories first (let me know if I'm wrong). Doesn't sort by extension, though: you have to make the awk statement a lot busier if you want to do that ...





                      To also make it work with names containing spaces, I would probably replace the awk with something like sed -E -e 's/([^ ]+[ ]+){8}//' to strip out the first 8 fields instead of printing the 9th






                      share|improve this answer























                      • actually, I'm pretty sure this will work on linux, too, but as others have stated you have some built-in support there.
                        – Zac Thompson
                        Oct 27 '09 at 5:06






                      • 1




                        Works on a Mac. Note that if you have CLICOLOR turned on this will remove the coloring.
                        – eykanal
                        Mar 30 '11 at 14:36






                      • 1




                        will give incorrect results for filenames with spaces.
                        – glenn jackman
                        Jun 4 '11 at 14:53










                      • @glenn, you are right ... if this was a concern I would probably replace the awk with something like sed -E -e 's/([^ ]+[ ]+){8}//' to strip out the first 8 fields instead of printing the 9th
                        – Zac Thompson
                        Jun 4 '11 at 18:29










                      • ls -l |sort -d -k 1.1,1.1r -k 9 | cut -c 50-
                        – nohillside
                        Feb 10 '12 at 20:51
















                      3














                      If you're not on linux,



                      ls -l |sort -d -k 1.1,1.1r -k 9 |awk '{print $9}'


                      should sort directories first (let me know if I'm wrong). Doesn't sort by extension, though: you have to make the awk statement a lot busier if you want to do that ...





                      To also make it work with names containing spaces, I would probably replace the awk with something like sed -E -e 's/([^ ]+[ ]+){8}//' to strip out the first 8 fields instead of printing the 9th






                      share|improve this answer























                      • actually, I'm pretty sure this will work on linux, too, but as others have stated you have some built-in support there.
                        – Zac Thompson
                        Oct 27 '09 at 5:06






                      • 1




                        Works on a Mac. Note that if you have CLICOLOR turned on this will remove the coloring.
                        – eykanal
                        Mar 30 '11 at 14:36






                      • 1




                        will give incorrect results for filenames with spaces.
                        – glenn jackman
                        Jun 4 '11 at 14:53










                      • @glenn, you are right ... if this was a concern I would probably replace the awk with something like sed -E -e 's/([^ ]+[ ]+){8}//' to strip out the first 8 fields instead of printing the 9th
                        – Zac Thompson
                        Jun 4 '11 at 18:29










                      • ls -l |sort -d -k 1.1,1.1r -k 9 | cut -c 50-
                        – nohillside
                        Feb 10 '12 at 20:51














                      3












                      3








                      3






                      If you're not on linux,



                      ls -l |sort -d -k 1.1,1.1r -k 9 |awk '{print $9}'


                      should sort directories first (let me know if I'm wrong). Doesn't sort by extension, though: you have to make the awk statement a lot busier if you want to do that ...





                      To also make it work with names containing spaces, I would probably replace the awk with something like sed -E -e 's/([^ ]+[ ]+){8}//' to strip out the first 8 fields instead of printing the 9th






                      share|improve this answer














                      If you're not on linux,



                      ls -l |sort -d -k 1.1,1.1r -k 9 |awk '{print $9}'


                      should sort directories first (let me know if I'm wrong). Doesn't sort by extension, though: you have to make the awk statement a lot busier if you want to do that ...





                      To also make it work with names containing spaces, I would probably replace the awk with something like sed -E -e 's/([^ ]+[ ]+){8}//' to strip out the first 8 fields instead of printing the 9th







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Feb 11 '12 at 20:07

























                      answered Oct 27 '09 at 5:04









                      Zac Thompson

                      822913




                      822913












                      • actually, I'm pretty sure this will work on linux, too, but as others have stated you have some built-in support there.
                        – Zac Thompson
                        Oct 27 '09 at 5:06






                      • 1




                        Works on a Mac. Note that if you have CLICOLOR turned on this will remove the coloring.
                        – eykanal
                        Mar 30 '11 at 14:36






                      • 1




                        will give incorrect results for filenames with spaces.
                        – glenn jackman
                        Jun 4 '11 at 14:53










                      • @glenn, you are right ... if this was a concern I would probably replace the awk with something like sed -E -e 's/([^ ]+[ ]+){8}//' to strip out the first 8 fields instead of printing the 9th
                        – Zac Thompson
                        Jun 4 '11 at 18:29










                      • ls -l |sort -d -k 1.1,1.1r -k 9 | cut -c 50-
                        – nohillside
                        Feb 10 '12 at 20:51


















                      • actually, I'm pretty sure this will work on linux, too, but as others have stated you have some built-in support there.
                        – Zac Thompson
                        Oct 27 '09 at 5:06






                      • 1




                        Works on a Mac. Note that if you have CLICOLOR turned on this will remove the coloring.
                        – eykanal
                        Mar 30 '11 at 14:36






                      • 1




                        will give incorrect results for filenames with spaces.
                        – glenn jackman
                        Jun 4 '11 at 14:53










                      • @glenn, you are right ... if this was a concern I would probably replace the awk with something like sed -E -e 's/([^ ]+[ ]+){8}//' to strip out the first 8 fields instead of printing the 9th
                        – Zac Thompson
                        Jun 4 '11 at 18:29










                      • ls -l |sort -d -k 1.1,1.1r -k 9 | cut -c 50-
                        – nohillside
                        Feb 10 '12 at 20:51
















                      actually, I'm pretty sure this will work on linux, too, but as others have stated you have some built-in support there.
                      – Zac Thompson
                      Oct 27 '09 at 5:06




                      actually, I'm pretty sure this will work on linux, too, but as others have stated you have some built-in support there.
                      – Zac Thompson
                      Oct 27 '09 at 5:06




                      1




                      1




                      Works on a Mac. Note that if you have CLICOLOR turned on this will remove the coloring.
                      – eykanal
                      Mar 30 '11 at 14:36




                      Works on a Mac. Note that if you have CLICOLOR turned on this will remove the coloring.
                      – eykanal
                      Mar 30 '11 at 14:36




                      1




                      1




                      will give incorrect results for filenames with spaces.
                      – glenn jackman
                      Jun 4 '11 at 14:53




                      will give incorrect results for filenames with spaces.
                      – glenn jackman
                      Jun 4 '11 at 14:53












                      @glenn, you are right ... if this was a concern I would probably replace the awk with something like sed -E -e 's/([^ ]+[ ]+){8}//' to strip out the first 8 fields instead of printing the 9th
                      – Zac Thompson
                      Jun 4 '11 at 18:29




                      @glenn, you are right ... if this was a concern I would probably replace the awk with something like sed -E -e 's/([^ ]+[ ]+){8}//' to strip out the first 8 fields instead of printing the 9th
                      – Zac Thompson
                      Jun 4 '11 at 18:29












                      ls -l |sort -d -k 1.1,1.1r -k 9 | cut -c 50-
                      – nohillside
                      Feb 10 '12 at 20:51




                      ls -l |sort -d -k 1.1,1.1r -k 9 | cut -c 50-
                      – nohillside
                      Feb 10 '12 at 20:51











                      2














                      If you're running on Linux, GNU ls supports the --sort option:-



                      ls --sort=extension





                      share|improve this answer





















                      • Not what was asked for.
                        – Sridhar-Sarnobat
                        May 14 '16 at 21:05
















                      2














                      If you're running on Linux, GNU ls supports the --sort option:-



                      ls --sort=extension





                      share|improve this answer





















                      • Not what was asked for.
                        – Sridhar-Sarnobat
                        May 14 '16 at 21:05














                      2












                      2








                      2






                      If you're running on Linux, GNU ls supports the --sort option:-



                      ls --sort=extension





                      share|improve this answer












                      If you're running on Linux, GNU ls supports the --sort option:-



                      ls --sort=extension






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 7 '09 at 18:29









                      DaveR

                      1292




                      1292












                      • Not what was asked for.
                        – Sridhar-Sarnobat
                        May 14 '16 at 21:05


















                      • Not what was asked for.
                        – Sridhar-Sarnobat
                        May 14 '16 at 21:05
















                      Not what was asked for.
                      – Sridhar-Sarnobat
                      May 14 '16 at 21:05




                      Not what was asked for.
                      – Sridhar-Sarnobat
                      May 14 '16 at 21:05











                      0














                      A good approach is using two commands at once, separating folders at first, then the sorted files by extensions, like so:



                      ls -p | grep /;ls -p | grep -v / | rev | sort | rev





                      share|improve this answer




























                        0














                        A good approach is using two commands at once, separating folders at first, then the sorted files by extensions, like so:



                        ls -p | grep /;ls -p | grep -v / | rev | sort | rev





                        share|improve this answer


























                          0












                          0








                          0






                          A good approach is using two commands at once, separating folders at first, then the sorted files by extensions, like so:



                          ls -p | grep /;ls -p | grep -v / | rev | sort | rev





                          share|improve this answer














                          A good approach is using two commands at once, separating folders at first, then the sorted files by extensions, like so:



                          ls -p | grep /;ls -p | grep -v / | rev | sort | rev






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Dec 10 at 21:37

























                          answered Dec 10 at 18:17









                          PYK

                          1012




                          1012























                              -1














                              I added to my .bashrc (linux) the line



                              alias lx = "ls -X"


                              that way I just type lx and get it sorted by extension.






                              share|improve this answer





















                              • So the answer to the question is use ls -X :)
                                – Andrew
                                Jan 23 '13 at 20:23










                              • Not what was asked for
                                – Sridhar-Sarnobat
                                May 14 '16 at 21:05
















                              -1














                              I added to my .bashrc (linux) the line



                              alias lx = "ls -X"


                              that way I just type lx and get it sorted by extension.






                              share|improve this answer





















                              • So the answer to the question is use ls -X :)
                                – Andrew
                                Jan 23 '13 at 20:23










                              • Not what was asked for
                                – Sridhar-Sarnobat
                                May 14 '16 at 21:05














                              -1












                              -1








                              -1






                              I added to my .bashrc (linux) the line



                              alias lx = "ls -X"


                              that way I just type lx and get it sorted by extension.






                              share|improve this answer












                              I added to my .bashrc (linux) the line



                              alias lx = "ls -X"


                              that way I just type lx and get it sorted by extension.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jan 23 '13 at 19:34









                              user192273

                              1




                              1












                              • So the answer to the question is use ls -X :)
                                – Andrew
                                Jan 23 '13 at 20:23










                              • Not what was asked for
                                – Sridhar-Sarnobat
                                May 14 '16 at 21:05


















                              • So the answer to the question is use ls -X :)
                                – Andrew
                                Jan 23 '13 at 20:23










                              • Not what was asked for
                                – Sridhar-Sarnobat
                                May 14 '16 at 21:05
















                              So the answer to the question is use ls -X :)
                              – Andrew
                              Jan 23 '13 at 20:23




                              So the answer to the question is use ls -X :)
                              – Andrew
                              Jan 23 '13 at 20:23












                              Not what was asked for
                              – Sridhar-Sarnobat
                              May 14 '16 at 21:05




                              Not what was asked for
                              – Sridhar-Sarnobat
                              May 14 '16 at 21:05


















                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f19781%2fhow-to-make-ls-sort-by-file-extension-and-then-name%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

                              How do I know what Microsoft account the skydrive app is syncing to?

                              When does type information flow backwards in C++?

                              Grease: Live!