how to sort files by their permissions using ls?












3















I have a large number of files and directories in one directory.



I need to sort them in terms of the permissions.



For example



drwx------
drwxr-xr-x
drwxr-x---


I am just wondering if we can sort the files and dirs using ls?










share|improve this question




















  • 1





    ls -l|sort will sort by permissions.

    – geedoubleya
    Oct 31 '14 at 12:22
















3















I have a large number of files and directories in one directory.



I need to sort them in terms of the permissions.



For example



drwx------
drwxr-xr-x
drwxr-x---


I am just wondering if we can sort the files and dirs using ls?










share|improve this question




















  • 1





    ls -l|sort will sort by permissions.

    – geedoubleya
    Oct 31 '14 at 12:22














3












3








3


1






I have a large number of files and directories in one directory.



I need to sort them in terms of the permissions.



For example



drwx------
drwxr-xr-x
drwxr-x---


I am just wondering if we can sort the files and dirs using ls?










share|improve this question
















I have a large number of files and directories in one directory.



I need to sort them in terms of the permissions.



For example



drwx------
drwxr-xr-x
drwxr-x---


I am just wondering if we can sort the files and dirs using ls?







ls sort






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 31 '14 at 23:16









Gilles

539k12810891606




539k12810891606










asked Oct 31 '14 at 12:18









Chenming ZhangChenming Zhang

5181711




5181711








  • 1





    ls -l|sort will sort by permissions.

    – geedoubleya
    Oct 31 '14 at 12:22














  • 1





    ls -l|sort will sort by permissions.

    – geedoubleya
    Oct 31 '14 at 12:22








1




1





ls -l|sort will sort by permissions.

– geedoubleya
Oct 31 '14 at 12:22





ls -l|sort will sort by permissions.

– geedoubleya
Oct 31 '14 at 12:22










3 Answers
3






active

oldest

votes


















8














ls does not directly support sorting by permissions, but you can combine it with the sort command:



ls -l | sort


You can use the -k option to sort to start matching from a specific character, the format is -k FIELD.CHAR, the permissions are the first field in the ls output. So e.g. -k 1.2 will start from the second character of the permission string, which will ignore any directory / device / link etc. flag, or -k 1.5 for sorting by group permissions.



If you don't want the additional output of ls -l, you can remove it with awk:



 ls -l | sort | awk '{ print $1, $NF}'


This will print only the first field (the permissions) and the last one (the filename).






share|improve this answer



















  • 1





    If you don't want additional output it much easy to use stat command stat -c"%A %n" * | sort

    – Costas
    Oct 31 '14 at 15:28



















2














You can also sort by octal value.



for i in *; do stat --format="%a %n" "$i"; done | sort -n





share|improve this answer

































    0














    Which option to the ls will sort the output by file size?






    share|improve this answer








    New contributor




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





















    • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review

      – G-Man
      1 hour ago











    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    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: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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%2funix.stackexchange.com%2fquestions%2f165225%2fhow-to-sort-files-by-their-permissions-using-ls%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    8














    ls does not directly support sorting by permissions, but you can combine it with the sort command:



    ls -l | sort


    You can use the -k option to sort to start matching from a specific character, the format is -k FIELD.CHAR, the permissions are the first field in the ls output. So e.g. -k 1.2 will start from the second character of the permission string, which will ignore any directory / device / link etc. flag, or -k 1.5 for sorting by group permissions.



    If you don't want the additional output of ls -l, you can remove it with awk:



     ls -l | sort | awk '{ print $1, $NF}'


    This will print only the first field (the permissions) and the last one (the filename).






    share|improve this answer



















    • 1





      If you don't want additional output it much easy to use stat command stat -c"%A %n" * | sort

      – Costas
      Oct 31 '14 at 15:28
















    8














    ls does not directly support sorting by permissions, but you can combine it with the sort command:



    ls -l | sort


    You can use the -k option to sort to start matching from a specific character, the format is -k FIELD.CHAR, the permissions are the first field in the ls output. So e.g. -k 1.2 will start from the second character of the permission string, which will ignore any directory / device / link etc. flag, or -k 1.5 for sorting by group permissions.



    If you don't want the additional output of ls -l, you can remove it with awk:



     ls -l | sort | awk '{ print $1, $NF}'


    This will print only the first field (the permissions) and the last one (the filename).






    share|improve this answer



















    • 1





      If you don't want additional output it much easy to use stat command stat -c"%A %n" * | sort

      – Costas
      Oct 31 '14 at 15:28














    8












    8








    8







    ls does not directly support sorting by permissions, but you can combine it with the sort command:



    ls -l | sort


    You can use the -k option to sort to start matching from a specific character, the format is -k FIELD.CHAR, the permissions are the first field in the ls output. So e.g. -k 1.2 will start from the second character of the permission string, which will ignore any directory / device / link etc. flag, or -k 1.5 for sorting by group permissions.



    If you don't want the additional output of ls -l, you can remove it with awk:



     ls -l | sort | awk '{ print $1, $NF}'


    This will print only the first field (the permissions) and the last one (the filename).






    share|improve this answer













    ls does not directly support sorting by permissions, but you can combine it with the sort command:



    ls -l | sort


    You can use the -k option to sort to start matching from a specific character, the format is -k FIELD.CHAR, the permissions are the first field in the ls output. So e.g. -k 1.2 will start from the second character of the permission string, which will ignore any directory / device / link etc. flag, or -k 1.5 for sorting by group permissions.



    If you don't want the additional output of ls -l, you can remove it with awk:



     ls -l | sort | awk '{ print $1, $NF}'


    This will print only the first field (the permissions) and the last one (the filename).







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 31 '14 at 12:37









    crater2150crater2150

    2,61821523




    2,61821523








    • 1





      If you don't want additional output it much easy to use stat command stat -c"%A %n" * | sort

      – Costas
      Oct 31 '14 at 15:28














    • 1





      If you don't want additional output it much easy to use stat command stat -c"%A %n" * | sort

      – Costas
      Oct 31 '14 at 15:28








    1




    1





    If you don't want additional output it much easy to use stat command stat -c"%A %n" * | sort

    – Costas
    Oct 31 '14 at 15:28





    If you don't want additional output it much easy to use stat command stat -c"%A %n" * | sort

    – Costas
    Oct 31 '14 at 15:28













    2














    You can also sort by octal value.



    for i in *; do stat --format="%a %n" "$i"; done | sort -n





    share|improve this answer






























      2














      You can also sort by octal value.



      for i in *; do stat --format="%a %n" "$i"; done | sort -n





      share|improve this answer




























        2












        2








        2







        You can also sort by octal value.



        for i in *; do stat --format="%a %n" "$i"; done | sort -n





        share|improve this answer















        You can also sort by octal value.



        for i in *; do stat --format="%a %n" "$i"; done | sort -n






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Oct 31 '14 at 15:50









        Ramesh

        23.7k34104185




        23.7k34104185










        answered Oct 31 '14 at 15:30









        LinuxGuruLinuxGuru

        30912




        30912























            0














            Which option to the ls will sort the output by file size?






            share|improve this answer








            New contributor




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





















            • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review

              – G-Man
              1 hour ago
















            0














            Which option to the ls will sort the output by file size?






            share|improve this answer








            New contributor




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





















            • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review

              – G-Man
              1 hour ago














            0












            0








            0







            Which option to the ls will sort the output by file size?






            share|improve this answer








            New contributor




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










            Which option to the ls will sort the output by file size?







            share|improve this answer








            New contributor




            Edmond Assadi 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 answer



            share|improve this answer






            New contributor




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









            answered 2 hours ago









            Edmond AssadiEdmond Assadi

            1




            1




            New contributor




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





            New contributor





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






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













            • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review

              – G-Man
              1 hour ago



















            • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review

              – G-Man
              1 hour ago

















            If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review

            – G-Man
            1 hour ago





            If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review

            – G-Man
            1 hour ago


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • 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%2funix.stackexchange.com%2fquestions%2f165225%2fhow-to-sort-files-by-their-permissions-using-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

            Accessing regular linux commands in Huawei's Dopra Linux

            Can't connect RFCOMM socket: Host is down

            Kernel panic - not syncing: Fatal Exception in Interrupt