Order all files by size using find

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












As practice I need to complete a script that orders all filles by size with a given extension(.txt for example) including those in subdirectories too.



For example; 
./ex1.sh einstein txt
einstein/copyright.txt
einstein/do-how.txt
einstein/etext9/bil11.txt
einstein/etext9/2ws271.txt
einstein/etext9/liber11.txt
einstein/etext0/bib0010h/Readme.txt
einstein/etext0/kknta10.txt


I can't use du or other advanced commands. At some point I need to use find, I tried something like this



find -depth - type f -name "*.$extension" | sort ....


but this hasn't work really well, as i don't really know how to sort them by size, only result i get is sorted by name.



I was looking for an output similar to ls -lhS but including subdirectories.







share|improve this question


















  • 1




    If you can only use find, why are you using sort? Please edit your question and explain your requirements. What is your example? Is it the output of a command? Or is it your ex1.sh script? If so, what are the parameters (einstein and txt) that you are giving?
    – terdon♦
    Mar 15 at 12:10















up vote
0
down vote

favorite












As practice I need to complete a script that orders all filles by size with a given extension(.txt for example) including those in subdirectories too.



For example; 
./ex1.sh einstein txt
einstein/copyright.txt
einstein/do-how.txt
einstein/etext9/bil11.txt
einstein/etext9/2ws271.txt
einstein/etext9/liber11.txt
einstein/etext0/bib0010h/Readme.txt
einstein/etext0/kknta10.txt


I can't use du or other advanced commands. At some point I need to use find, I tried something like this



find -depth - type f -name "*.$extension" | sort ....


but this hasn't work really well, as i don't really know how to sort them by size, only result i get is sorted by name.



I was looking for an output similar to ls -lhS but including subdirectories.







share|improve this question


















  • 1




    If you can only use find, why are you using sort? Please edit your question and explain your requirements. What is your example? Is it the output of a command? Or is it your ex1.sh script? If so, what are the parameters (einstein and txt) that you are giving?
    – terdon♦
    Mar 15 at 12:10













up vote
0
down vote

favorite









up vote
0
down vote

favorite











As practice I need to complete a script that orders all filles by size with a given extension(.txt for example) including those in subdirectories too.



For example; 
./ex1.sh einstein txt
einstein/copyright.txt
einstein/do-how.txt
einstein/etext9/bil11.txt
einstein/etext9/2ws271.txt
einstein/etext9/liber11.txt
einstein/etext0/bib0010h/Readme.txt
einstein/etext0/kknta10.txt


I can't use du or other advanced commands. At some point I need to use find, I tried something like this



find -depth - type f -name "*.$extension" | sort ....


but this hasn't work really well, as i don't really know how to sort them by size, only result i get is sorted by name.



I was looking for an output similar to ls -lhS but including subdirectories.







share|improve this question














As practice I need to complete a script that orders all filles by size with a given extension(.txt for example) including those in subdirectories too.



For example; 
./ex1.sh einstein txt
einstein/copyright.txt
einstein/do-how.txt
einstein/etext9/bil11.txt
einstein/etext9/2ws271.txt
einstein/etext9/liber11.txt
einstein/etext0/bib0010h/Readme.txt
einstein/etext0/kknta10.txt


I can't use du or other advanced commands. At some point I need to use find, I tried something like this



find -depth - type f -name "*.$extension" | sort ....


but this hasn't work really well, as i don't really know how to sort them by size, only result i get is sorted by name.



I was looking for an output similar to ls -lhS but including subdirectories.









share|improve this question













share|improve this question




share|improve this question








edited Mar 15 at 12:14

























asked Mar 15 at 12:02









Arnau Martínez

142




142







  • 1




    If you can only use find, why are you using sort? Please edit your question and explain your requirements. What is your example? Is it the output of a command? Or is it your ex1.sh script? If so, what are the parameters (einstein and txt) that you are giving?
    – terdon♦
    Mar 15 at 12:10













  • 1




    If you can only use find, why are you using sort? Please edit your question and explain your requirements. What is your example? Is it the output of a command? Or is it your ex1.sh script? If so, what are the parameters (einstein and txt) that you are giving?
    – terdon♦
    Mar 15 at 12:10








1




1




If you can only use find, why are you using sort? Please edit your question and explain your requirements. What is your example? Is it the output of a command? Or is it your ex1.sh script? If so, what are the parameters (einstein and txt) that you are giving?
– terdon♦
Mar 15 at 12:10





If you can only use find, why are you using sort? Please edit your question and explain your requirements. What is your example? Is it the output of a command? Or is it your ex1.sh script? If so, what are the parameters (einstein and txt) that you are giving?
– terdon♦
Mar 15 at 12:10











4 Answers
4






active

oldest

votes

















up vote
0
down vote



accepted










find includes an option -printf which allows you to format what to output about your results, and how to output them. One of possibilities listed in the documentation for that option (see the man page) is %s for a file's size in bytes. Thus, you can add to your find command something like `-printf "%s %pn".






share|improve this answer




















  • @terdon - yes. His answer had not been posted when I began writing my answer.
    – user1404316
    Mar 15 at 12:35










  • Ah, the classic race condition! :)
    – terdon♦
    Mar 15 at 12:51

















up vote
2
down vote













The only sorting that any of the finds can do AFAIK is to have contents appear before the containing directory (the -depth option). You will have use something else to sort on size.



If you have GNU find, try:



find . -type f -iname "*.$extension" -printf "%s %pn" | sort -n | sed 's/^[0-9]* //'


-printf "%s %pn" prints the size and file path, then we sort numerically, and then strip the size using sed.






share|improve this answer
















  • 1




    (assuming file paths don't contain newlines and bytes not forming valid characters and that $extension doesn't contain wildcards)
    – Stéphane Chazelas
    Mar 15 at 12:15






  • 1




    (in which case with -printf and the -z options as needed, I think)
    – muru
    Mar 15 at 12:16










  • yep, NUL terminated output is the solution - e.g. find . -type f -printf "%s %p" | sort -z -k1,1n | awk -v RS='' print $2' (non-portable - works with GNU awk, maybe some others). +1, was going to write an answer like this but you've already written it.
    – cas
    Mar 15 at 12:48

















up vote
1
down vote













With zsh:



printf '%sn' **/*.$extension(D.oL)


To get a GNU ls -lh-type output, with GNU ls:



ls -Ulhd -- **/*.$extension(D.oL)


Or if the list is too large:



autoload zargs # best in ~/.zshrc
zargs --eof= -- **/*.$extension(D.oL) '' ls -Ulhd --


Or



printf '%s' **/*.$extension(D.oL) | xargs -r0 ls -Ulhd --


If for some reason, you do need to use find, you can always do:



printf '%s' ./**/*.$extension(D.oL) | xargs -r0 sh -c '
exec find "$@" -prune ...' sh





share|improve this answer






















  • The OP needs to use find for some reason.
    – terdon♦
    Mar 15 at 12:17

















up vote
0
down vote













i use



 find . -type f -iname "*.$extension" -print0 | xargs -0 ls -lS


this works fine for me






share|improve this answer




















    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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f430381%2forder-all-files-by-size-using-find%23new-answer', 'question_page');

    );

    Post as a guest






























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    find includes an option -printf which allows you to format what to output about your results, and how to output them. One of possibilities listed in the documentation for that option (see the man page) is %s for a file's size in bytes. Thus, you can add to your find command something like `-printf "%s %pn".






    share|improve this answer




















    • @terdon - yes. His answer had not been posted when I began writing my answer.
      – user1404316
      Mar 15 at 12:35










    • Ah, the classic race condition! :)
      – terdon♦
      Mar 15 at 12:51














    up vote
    0
    down vote



    accepted










    find includes an option -printf which allows you to format what to output about your results, and how to output them. One of possibilities listed in the documentation for that option (see the man page) is %s for a file's size in bytes. Thus, you can add to your find command something like `-printf "%s %pn".






    share|improve this answer




















    • @terdon - yes. His answer had not been posted when I began writing my answer.
      – user1404316
      Mar 15 at 12:35










    • Ah, the classic race condition! :)
      – terdon♦
      Mar 15 at 12:51












    up vote
    0
    down vote



    accepted







    up vote
    0
    down vote



    accepted






    find includes an option -printf which allows you to format what to output about your results, and how to output them. One of possibilities listed in the documentation for that option (see the man page) is %s for a file's size in bytes. Thus, you can add to your find command something like `-printf "%s %pn".






    share|improve this answer












    find includes an option -printf which allows you to format what to output about your results, and how to output them. One of possibilities listed in the documentation for that option (see the man page) is %s for a file's size in bytes. Thus, you can add to your find command something like `-printf "%s %pn".







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 15 at 12:15









    user1404316

    2,314520




    2,314520











    • @terdon - yes. His answer had not been posted when I began writing my answer.
      – user1404316
      Mar 15 at 12:35










    • Ah, the classic race condition! :)
      – terdon♦
      Mar 15 at 12:51
















    • @terdon - yes. His answer had not been posted when I began writing my answer.
      – user1404316
      Mar 15 at 12:35










    • Ah, the classic race condition! :)
      – terdon♦
      Mar 15 at 12:51















    @terdon - yes. His answer had not been posted when I began writing my answer.
    – user1404316
    Mar 15 at 12:35




    @terdon - yes. His answer had not been posted when I began writing my answer.
    – user1404316
    Mar 15 at 12:35












    Ah, the classic race condition! :)
    – terdon♦
    Mar 15 at 12:51




    Ah, the classic race condition! :)
    – terdon♦
    Mar 15 at 12:51












    up vote
    2
    down vote













    The only sorting that any of the finds can do AFAIK is to have contents appear before the containing directory (the -depth option). You will have use something else to sort on size.



    If you have GNU find, try:



    find . -type f -iname "*.$extension" -printf "%s %pn" | sort -n | sed 's/^[0-9]* //'


    -printf "%s %pn" prints the size and file path, then we sort numerically, and then strip the size using sed.






    share|improve this answer
















    • 1




      (assuming file paths don't contain newlines and bytes not forming valid characters and that $extension doesn't contain wildcards)
      – Stéphane Chazelas
      Mar 15 at 12:15






    • 1




      (in which case with -printf and the -z options as needed, I think)
      – muru
      Mar 15 at 12:16










    • yep, NUL terminated output is the solution - e.g. find . -type f -printf "%s %p" | sort -z -k1,1n | awk -v RS='' print $2' (non-portable - works with GNU awk, maybe some others). +1, was going to write an answer like this but you've already written it.
      – cas
      Mar 15 at 12:48














    up vote
    2
    down vote













    The only sorting that any of the finds can do AFAIK is to have contents appear before the containing directory (the -depth option). You will have use something else to sort on size.



    If you have GNU find, try:



    find . -type f -iname "*.$extension" -printf "%s %pn" | sort -n | sed 's/^[0-9]* //'


    -printf "%s %pn" prints the size and file path, then we sort numerically, and then strip the size using sed.






    share|improve this answer
















    • 1




      (assuming file paths don't contain newlines and bytes not forming valid characters and that $extension doesn't contain wildcards)
      – Stéphane Chazelas
      Mar 15 at 12:15






    • 1




      (in which case with -printf and the -z options as needed, I think)
      – muru
      Mar 15 at 12:16










    • yep, NUL terminated output is the solution - e.g. find . -type f -printf "%s %p" | sort -z -k1,1n | awk -v RS='' print $2' (non-portable - works with GNU awk, maybe some others). +1, was going to write an answer like this but you've already written it.
      – cas
      Mar 15 at 12:48












    up vote
    2
    down vote










    up vote
    2
    down vote









    The only sorting that any of the finds can do AFAIK is to have contents appear before the containing directory (the -depth option). You will have use something else to sort on size.



    If you have GNU find, try:



    find . -type f -iname "*.$extension" -printf "%s %pn" | sort -n | sed 's/^[0-9]* //'


    -printf "%s %pn" prints the size and file path, then we sort numerically, and then strip the size using sed.






    share|improve this answer












    The only sorting that any of the finds can do AFAIK is to have contents appear before the containing directory (the -depth option). You will have use something else to sort on size.



    If you have GNU find, try:



    find . -type f -iname "*.$extension" -printf "%s %pn" | sort -n | sed 's/^[0-9]* //'


    -printf "%s %pn" prints the size and file path, then we sort numerically, and then strip the size using sed.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 15 at 12:11









    muru

    33.4k577141




    33.4k577141







    • 1




      (assuming file paths don't contain newlines and bytes not forming valid characters and that $extension doesn't contain wildcards)
      – Stéphane Chazelas
      Mar 15 at 12:15






    • 1




      (in which case with -printf and the -z options as needed, I think)
      – muru
      Mar 15 at 12:16










    • yep, NUL terminated output is the solution - e.g. find . -type f -printf "%s %p" | sort -z -k1,1n | awk -v RS='' print $2' (non-portable - works with GNU awk, maybe some others). +1, was going to write an answer like this but you've already written it.
      – cas
      Mar 15 at 12:48












    • 1




      (assuming file paths don't contain newlines and bytes not forming valid characters and that $extension doesn't contain wildcards)
      – Stéphane Chazelas
      Mar 15 at 12:15






    • 1




      (in which case with -printf and the -z options as needed, I think)
      – muru
      Mar 15 at 12:16










    • yep, NUL terminated output is the solution - e.g. find . -type f -printf "%s %p" | sort -z -k1,1n | awk -v RS='' print $2' (non-portable - works with GNU awk, maybe some others). +1, was going to write an answer like this but you've already written it.
      – cas
      Mar 15 at 12:48







    1




    1




    (assuming file paths don't contain newlines and bytes not forming valid characters and that $extension doesn't contain wildcards)
    – Stéphane Chazelas
    Mar 15 at 12:15




    (assuming file paths don't contain newlines and bytes not forming valid characters and that $extension doesn't contain wildcards)
    – Stéphane Chazelas
    Mar 15 at 12:15




    1




    1




    (in which case with -printf and the -z options as needed, I think)
    – muru
    Mar 15 at 12:16




    (in which case with -printf and the -z options as needed, I think)
    – muru
    Mar 15 at 12:16












    yep, NUL terminated output is the solution - e.g. find . -type f -printf "%s %p" | sort -z -k1,1n | awk -v RS='' print $2' (non-portable - works with GNU awk, maybe some others). +1, was going to write an answer like this but you've already written it.
    – cas
    Mar 15 at 12:48




    yep, NUL terminated output is the solution - e.g. find . -type f -printf "%s %p" | sort -z -k1,1n | awk -v RS='' print $2' (non-portable - works with GNU awk, maybe some others). +1, was going to write an answer like this but you've already written it.
    – cas
    Mar 15 at 12:48










    up vote
    1
    down vote













    With zsh:



    printf '%sn' **/*.$extension(D.oL)


    To get a GNU ls -lh-type output, with GNU ls:



    ls -Ulhd -- **/*.$extension(D.oL)


    Or if the list is too large:



    autoload zargs # best in ~/.zshrc
    zargs --eof= -- **/*.$extension(D.oL) '' ls -Ulhd --


    Or



    printf '%s' **/*.$extension(D.oL) | xargs -r0 ls -Ulhd --


    If for some reason, you do need to use find, you can always do:



    printf '%s' ./**/*.$extension(D.oL) | xargs -r0 sh -c '
    exec find "$@" -prune ...' sh





    share|improve this answer






















    • The OP needs to use find for some reason.
      – terdon♦
      Mar 15 at 12:17














    up vote
    1
    down vote













    With zsh:



    printf '%sn' **/*.$extension(D.oL)


    To get a GNU ls -lh-type output, with GNU ls:



    ls -Ulhd -- **/*.$extension(D.oL)


    Or if the list is too large:



    autoload zargs # best in ~/.zshrc
    zargs --eof= -- **/*.$extension(D.oL) '' ls -Ulhd --


    Or



    printf '%s' **/*.$extension(D.oL) | xargs -r0 ls -Ulhd --


    If for some reason, you do need to use find, you can always do:



    printf '%s' ./**/*.$extension(D.oL) | xargs -r0 sh -c '
    exec find "$@" -prune ...' sh





    share|improve this answer






















    • The OP needs to use find for some reason.
      – terdon♦
      Mar 15 at 12:17












    up vote
    1
    down vote










    up vote
    1
    down vote









    With zsh:



    printf '%sn' **/*.$extension(D.oL)


    To get a GNU ls -lh-type output, with GNU ls:



    ls -Ulhd -- **/*.$extension(D.oL)


    Or if the list is too large:



    autoload zargs # best in ~/.zshrc
    zargs --eof= -- **/*.$extension(D.oL) '' ls -Ulhd --


    Or



    printf '%s' **/*.$extension(D.oL) | xargs -r0 ls -Ulhd --


    If for some reason, you do need to use find, you can always do:



    printf '%s' ./**/*.$extension(D.oL) | xargs -r0 sh -c '
    exec find "$@" -prune ...' sh





    share|improve this answer














    With zsh:



    printf '%sn' **/*.$extension(D.oL)


    To get a GNU ls -lh-type output, with GNU ls:



    ls -Ulhd -- **/*.$extension(D.oL)


    Or if the list is too large:



    autoload zargs # best in ~/.zshrc
    zargs --eof= -- **/*.$extension(D.oL) '' ls -Ulhd --


    Or



    printf '%s' **/*.$extension(D.oL) | xargs -r0 ls -Ulhd --


    If for some reason, you do need to use find, you can always do:



    printf '%s' ./**/*.$extension(D.oL) | xargs -r0 sh -c '
    exec find "$@" -prune ...' sh






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 15 at 12:20

























    answered Mar 15 at 12:13









    Stéphane Chazelas

    280k53515847




    280k53515847











    • The OP needs to use find for some reason.
      – terdon♦
      Mar 15 at 12:17
















    • The OP needs to use find for some reason.
      – terdon♦
      Mar 15 at 12:17















    The OP needs to use find for some reason.
    – terdon♦
    Mar 15 at 12:17




    The OP needs to use find for some reason.
    – terdon♦
    Mar 15 at 12:17










    up vote
    0
    down vote













    i use



     find . -type f -iname "*.$extension" -print0 | xargs -0 ls -lS


    this works fine for me






    share|improve this answer
























      up vote
      0
      down vote













      i use



       find . -type f -iname "*.$extension" -print0 | xargs -0 ls -lS


      this works fine for me






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        i use



         find . -type f -iname "*.$extension" -print0 | xargs -0 ls -lS


        this works fine for me






        share|improve this answer












        i use



         find . -type f -iname "*.$extension" -print0 | xargs -0 ls -lS


        this works fine for me







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 15 at 12:59









        Ayyanar

        715




        715






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430381%2forder-all-files-by-size-using-find%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay