List files sorted by the number of lines they containCount number of lines in files then compare which has more (BASH)How to list files sorted by modification date recursively (no stat command available!)List the files accessed by a programCompressing all files that contain a particular stringFinding all files containing a word and then counting the number of linesList files with line count and dateCount and list down + advanced featuresscript that lists all files in a directory and subdirectories sorted by size, listing only file names not complete pathsSearch for files that contain a string and list their names sorted by the modified dateRecursively list all directories that contain one or more jpg image filesList files sorted according to specific line of contents

Do I need a multiple entry visa for a trip UK -> Sweden -> UK?

Modify casing of marked letters

How to be diplomatic in refusing to write code that breaches the privacy of our users

Can somebody explain Brexit in a few child-proof sentences?

Personal Teleportation as a Weapon

Can a monster with multiattack use this ability if they are missing a limb?

Is expanding the research of a group into machine learning as a PhD student risky?

How does a character multiclassing into warlock get a focus?

Applicability of Single Responsibility Principle

Irreducibility of a simple polynomial

What defines a dissertation?

Can criminal fraud exist without damages?

Teaching indefinite integrals that require special-casing

What will be the benefits of Brexit?

What is difference between behavior and behaviour

Is it correct to write "is not focus on"?

Lay out the Carpet

Is HostGator storing my password in plaintext?

Why does John Bercow say “unlock” after reading out the results of a vote?

Print name if parameter passed to function

Why did Kant, Hegel, and Adorno leave some words and phrases in the Greek alphabet?

Using parameter substitution on a Bash array

What's the purpose of "true" in bash "if sudo true; then"

Should my PhD thesis be submitted under my legal name?



List files sorted by the number of lines they contain


Count number of lines in files then compare which has more (BASH)How to list files sorted by modification date recursively (no stat command available!)List the files accessed by a programCompressing all files that contain a particular stringFinding all files containing a word and then counting the number of linesList files with line count and dateCount and list down + advanced featuresscript that lists all files in a directory and subdirectories sorted by size, listing only file names not complete pathsSearch for files that contain a string and list their names sorted by the modified dateRecursively list all directories that contain one or more jpg image filesList files sorted according to specific line of contents













26















How can I list the number of lines in the files in /group/book/four/word, sorted by the number of lines they contain?



ls -l command lists them down but does not sort them










share|improve this question



















  • 1





    Do you want the files listed by number of lines, or list the number of lines in the files or both? ls -l doesn't give the number of lines. ls -lS sorts file by size with some ls implementations (size being number of bytes in the content).

    – Stéphane Chazelas
    Nov 27 '14 at 12:59















26















How can I list the number of lines in the files in /group/book/four/word, sorted by the number of lines they contain?



ls -l command lists them down but does not sort them










share|improve this question



















  • 1





    Do you want the files listed by number of lines, or list the number of lines in the files or both? ls -l doesn't give the number of lines. ls -lS sorts file by size with some ls implementations (size being number of bytes in the content).

    – Stéphane Chazelas
    Nov 27 '14 at 12:59













26












26








26


6






How can I list the number of lines in the files in /group/book/four/word, sorted by the number of lines they contain?



ls -l command lists them down but does not sort them










share|improve this question
















How can I list the number of lines in the files in /group/book/four/word, sorted by the number of lines they contain?



ls -l command lists them down but does not sort them







bash shell files wc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 28 '16 at 20:40









don_crissti

51.7k15141168




51.7k15141168










asked Nov 27 '14 at 12:31









Ken RKen R

149126




149126







  • 1





    Do you want the files listed by number of lines, or list the number of lines in the files or both? ls -l doesn't give the number of lines. ls -lS sorts file by size with some ls implementations (size being number of bytes in the content).

    – Stéphane Chazelas
    Nov 27 '14 at 12:59












  • 1





    Do you want the files listed by number of lines, or list the number of lines in the files or both? ls -l doesn't give the number of lines. ls -lS sorts file by size with some ls implementations (size being number of bytes in the content).

    – Stéphane Chazelas
    Nov 27 '14 at 12:59







1




1





Do you want the files listed by number of lines, or list the number of lines in the files or both? ls -l doesn't give the number of lines. ls -lS sorts file by size with some ls implementations (size being number of bytes in the content).

– Stéphane Chazelas
Nov 27 '14 at 12:59





Do you want the files listed by number of lines, or list the number of lines in the files or both? ls -l doesn't give the number of lines. ls -lS sorts file by size with some ls implementations (size being number of bytes in the content).

– Stéphane Chazelas
Nov 27 '14 at 12:59










5 Answers
5






active

oldest

votes


















26














You should use a command like this:



find /group/book/four/word/ -type f -exec wc -l + | sort -rn



  • find : search for files on the path you want. If you don't want it recursive, and your find implementation supports it, you should add -maxdepth 1 just before the -exec option.


  • exec : tells the command to execute wc -l on every file.


  • sort -rn : sort the results numerically in reverse order. From greater to lower.

(that assumes file names don't contain newline characters).






share|improve this answer

























  • Note that when passed more than one file (or with some implementations, more than one file that it can read), wc will also print a total line, so here you'll also get one or more "total" lines unless there is only one file. You can pipe to grep / to remove them.

    – Stéphane Chazelas
    Feb 28 '16 at 20:47












  • upvote because of sort command

    – Francisco
    Sep 14 '18 at 23:43


















9














Non-recursive



Probably the simplest version if you don't need recursivity :



wc -l /group/book/four/word/*|sort -n


wc counts lines (option -l) in every (but hidden) (*) files under /group/book/four/word/, and sort sorts the result (through the pipe |) numerically (option -n).



Recursive



Someone made a comment to this answer mentioning grep -rlc, before to suppress it. Indeed grep is a great alternative, especially if you need recursivity :



grep -rc '^' /group/book/four/word/|tr ':' ' '|sort -n -k2


will count (option -c) recursively (option -r) lines matching (grep) '^' (that is, beginning of lines) in the directory /group/book/four/word/. Then you have to replace the colon by a space, e.g. using tr, to help sort, which you want to sort numerically (option -n) on the second column (option -k2).



Update : See Stephane's comment about possible limitations and how you can actually get rid of tr.






share|improve this answer




















  • 1





    grep -c . counts the lines that contain at least one valid character. Use grep -c '^' to count all the lines (will also count trailing characters after the last newline with some grep implementations). Note that not all grep implementations support a -r and behaviour varies among those that do. You don't need to translate :s (colon, not semicolon) to spaces for sort. Just use -t:. Note that that assumes file names don't contain : or blank or newline characters.

    – Stéphane Chazelas
    Nov 28 '14 at 18:07












  • Thanks for posting your non-recursive solution; I didn't know wc gave such a handy total all if you pass multiple paths. Coupling that functionality with the wild card and the pipe to sort is really clean.

    – Qcom
    Sep 25 '15 at 21:21


















7














With zsh:



lines() REPLY=$(wc -l < $REPLY)
printf '%sn' /group/book/four/word/*(.no+lines)


We define a new sorting function lines that replies with the number of lines in the file. And we use the o+lines glob qualifier which together with n (for numeric sort), defines how the results of the glob are ordered. (. also added to only check regular files).



That makes no assumption on what character the file names may contain other than hidden files (those starting with .) are omitted. Add the D glob qualifier if you want them as well.






share|improve this answer




















  • 2





    OP is tagged with bash only...

    – l0b0
    Nov 27 '14 at 13:15






  • 7





    @l0b0 that doesn't mean that the next person who needs this will also be running bash.

    – terdon
    Nov 27 '14 at 13:40


















4














You don't specify whether you also want the files in any subdirectories of /group/book/four/word. The find solution in jherran's answer will descend into subdirectories. If that is not wanted, use the shell instead:



for file in ./*; do [ -f "$file" ] && wc -l "$file"; done | sort -n


If your file names can contain newlines, you can use something like:



for file in ./*; do 
[ -f "$file" ] &&
printf "%lu %s" "$(wc -l < "$file")" "$file"
done | sort -zn | tr '' 'n'


Finally, if you do want to descend into subdirectories, you can use this in bash 4 or above:



shopt -s globstar
for file in ./**/*; do [ -f "$file" ] && wc -l "$file"; done | sort -n


Note that versions of bash prior to 4.3 were following symlinks when recursively descending the directory tree (like zsh's or tcsh's ***/*).



Also, all solutions above will ignore hidden files (those whose name starts with a ., use shopt -s dotglob to include them) and will also include the line count of symbolic links (which the find approach will not).






share|improve this answer

























  • Note that other differences from jherran's solution is that yours will also consider symlink to regular files (-xtype f in GNU find or *(-.) in zsh) and will omit hidden files.

    – Stéphane Chazelas
    Nov 27 '14 at 13:55











  • @StéphaneChazelas thanks, clarified. Why the %lu in printf? As I recall, that means long unsigned decimal, is it really necessary? Why not treat the number as a string? Does it make a difference?

    – terdon
    Nov 27 '14 at 13:59







  • 2





    If the wc output is empty (for instance because the file is not readable), then that will expand to 0 instead of the empty string, which is slightly better. Some sort implementations work with unsigned integers some with signed. %lu sounds like the safest bet, but it doesn't probably matter as if you have 2^31 lines, that will take ages anyway.

    – Stéphane Chazelas
    Nov 27 '14 at 14:01



















1














If you want to install fd a really fast file finder written in Rust (you should install it, it's great to have anyway)



fd --type=file . | xargs wc -l | sort -n


Basically fd lists the files, xargs will pass the list of files to wc (stands for word count but passing -l will make it count lines) then finally it's sorted from least number of lines to greatest using sort -n.






share|improve this answer








New contributor




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



















    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%2f170275%2flist-files-sorted-by-the-number-of-lines-they-contain%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    26














    You should use a command like this:



    find /group/book/four/word/ -type f -exec wc -l + | sort -rn



    • find : search for files on the path you want. If you don't want it recursive, and your find implementation supports it, you should add -maxdepth 1 just before the -exec option.


    • exec : tells the command to execute wc -l on every file.


    • sort -rn : sort the results numerically in reverse order. From greater to lower.

    (that assumes file names don't contain newline characters).






    share|improve this answer

























    • Note that when passed more than one file (or with some implementations, more than one file that it can read), wc will also print a total line, so here you'll also get one or more "total" lines unless there is only one file. You can pipe to grep / to remove them.

      – Stéphane Chazelas
      Feb 28 '16 at 20:47












    • upvote because of sort command

      – Francisco
      Sep 14 '18 at 23:43















    26














    You should use a command like this:



    find /group/book/four/word/ -type f -exec wc -l + | sort -rn



    • find : search for files on the path you want. If you don't want it recursive, and your find implementation supports it, you should add -maxdepth 1 just before the -exec option.


    • exec : tells the command to execute wc -l on every file.


    • sort -rn : sort the results numerically in reverse order. From greater to lower.

    (that assumes file names don't contain newline characters).






    share|improve this answer

























    • Note that when passed more than one file (or with some implementations, more than one file that it can read), wc will also print a total line, so here you'll also get one or more "total" lines unless there is only one file. You can pipe to grep / to remove them.

      – Stéphane Chazelas
      Feb 28 '16 at 20:47












    • upvote because of sort command

      – Francisco
      Sep 14 '18 at 23:43













    26












    26








    26







    You should use a command like this:



    find /group/book/four/word/ -type f -exec wc -l + | sort -rn



    • find : search for files on the path you want. If you don't want it recursive, and your find implementation supports it, you should add -maxdepth 1 just before the -exec option.


    • exec : tells the command to execute wc -l on every file.


    • sort -rn : sort the results numerically in reverse order. From greater to lower.

    (that assumes file names don't contain newline characters).






    share|improve this answer















    You should use a command like this:



    find /group/book/four/word/ -type f -exec wc -l + | sort -rn



    • find : search for files on the path you want. If you don't want it recursive, and your find implementation supports it, you should add -maxdepth 1 just before the -exec option.


    • exec : tells the command to execute wc -l on every file.


    • sort -rn : sort the results numerically in reverse order. From greater to lower.

    (that assumes file names don't contain newline characters).







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 27 '14 at 12:41









    Stéphane Chazelas

    311k57587945




    311k57587945










    answered Nov 27 '14 at 12:34









    jherranjherran

    2,29931328




    2,29931328












    • Note that when passed more than one file (or with some implementations, more than one file that it can read), wc will also print a total line, so here you'll also get one or more "total" lines unless there is only one file. You can pipe to grep / to remove them.

      – Stéphane Chazelas
      Feb 28 '16 at 20:47












    • upvote because of sort command

      – Francisco
      Sep 14 '18 at 23:43

















    • Note that when passed more than one file (or with some implementations, more than one file that it can read), wc will also print a total line, so here you'll also get one or more "total" lines unless there is only one file. You can pipe to grep / to remove them.

      – Stéphane Chazelas
      Feb 28 '16 at 20:47












    • upvote because of sort command

      – Francisco
      Sep 14 '18 at 23:43
















    Note that when passed more than one file (or with some implementations, more than one file that it can read), wc will also print a total line, so here you'll also get one or more "total" lines unless there is only one file. You can pipe to grep / to remove them.

    – Stéphane Chazelas
    Feb 28 '16 at 20:47






    Note that when passed more than one file (or with some implementations, more than one file that it can read), wc will also print a total line, so here you'll also get one or more "total" lines unless there is only one file. You can pipe to grep / to remove them.

    – Stéphane Chazelas
    Feb 28 '16 at 20:47














    upvote because of sort command

    – Francisco
    Sep 14 '18 at 23:43





    upvote because of sort command

    – Francisco
    Sep 14 '18 at 23:43













    9














    Non-recursive



    Probably the simplest version if you don't need recursivity :



    wc -l /group/book/four/word/*|sort -n


    wc counts lines (option -l) in every (but hidden) (*) files under /group/book/four/word/, and sort sorts the result (through the pipe |) numerically (option -n).



    Recursive



    Someone made a comment to this answer mentioning grep -rlc, before to suppress it. Indeed grep is a great alternative, especially if you need recursivity :



    grep -rc '^' /group/book/four/word/|tr ':' ' '|sort -n -k2


    will count (option -c) recursively (option -r) lines matching (grep) '^' (that is, beginning of lines) in the directory /group/book/four/word/. Then you have to replace the colon by a space, e.g. using tr, to help sort, which you want to sort numerically (option -n) on the second column (option -k2).



    Update : See Stephane's comment about possible limitations and how you can actually get rid of tr.






    share|improve this answer




















    • 1





      grep -c . counts the lines that contain at least one valid character. Use grep -c '^' to count all the lines (will also count trailing characters after the last newline with some grep implementations). Note that not all grep implementations support a -r and behaviour varies among those that do. You don't need to translate :s (colon, not semicolon) to spaces for sort. Just use -t:. Note that that assumes file names don't contain : or blank or newline characters.

      – Stéphane Chazelas
      Nov 28 '14 at 18:07












    • Thanks for posting your non-recursive solution; I didn't know wc gave such a handy total all if you pass multiple paths. Coupling that functionality with the wild card and the pipe to sort is really clean.

      – Qcom
      Sep 25 '15 at 21:21















    9














    Non-recursive



    Probably the simplest version if you don't need recursivity :



    wc -l /group/book/four/word/*|sort -n


    wc counts lines (option -l) in every (but hidden) (*) files under /group/book/four/word/, and sort sorts the result (through the pipe |) numerically (option -n).



    Recursive



    Someone made a comment to this answer mentioning grep -rlc, before to suppress it. Indeed grep is a great alternative, especially if you need recursivity :



    grep -rc '^' /group/book/four/word/|tr ':' ' '|sort -n -k2


    will count (option -c) recursively (option -r) lines matching (grep) '^' (that is, beginning of lines) in the directory /group/book/four/word/. Then you have to replace the colon by a space, e.g. using tr, to help sort, which you want to sort numerically (option -n) on the second column (option -k2).



    Update : See Stephane's comment about possible limitations and how you can actually get rid of tr.






    share|improve this answer




















    • 1





      grep -c . counts the lines that contain at least one valid character. Use grep -c '^' to count all the lines (will also count trailing characters after the last newline with some grep implementations). Note that not all grep implementations support a -r and behaviour varies among those that do. You don't need to translate :s (colon, not semicolon) to spaces for sort. Just use -t:. Note that that assumes file names don't contain : or blank or newline characters.

      – Stéphane Chazelas
      Nov 28 '14 at 18:07












    • Thanks for posting your non-recursive solution; I didn't know wc gave such a handy total all if you pass multiple paths. Coupling that functionality with the wild card and the pipe to sort is really clean.

      – Qcom
      Sep 25 '15 at 21:21













    9












    9








    9







    Non-recursive



    Probably the simplest version if you don't need recursivity :



    wc -l /group/book/four/word/*|sort -n


    wc counts lines (option -l) in every (but hidden) (*) files under /group/book/four/word/, and sort sorts the result (through the pipe |) numerically (option -n).



    Recursive



    Someone made a comment to this answer mentioning grep -rlc, before to suppress it. Indeed grep is a great alternative, especially if you need recursivity :



    grep -rc '^' /group/book/four/word/|tr ':' ' '|sort -n -k2


    will count (option -c) recursively (option -r) lines matching (grep) '^' (that is, beginning of lines) in the directory /group/book/four/word/. Then you have to replace the colon by a space, e.g. using tr, to help sort, which you want to sort numerically (option -n) on the second column (option -k2).



    Update : See Stephane's comment about possible limitations and how you can actually get rid of tr.






    share|improve this answer















    Non-recursive



    Probably the simplest version if you don't need recursivity :



    wc -l /group/book/four/word/*|sort -n


    wc counts lines (option -l) in every (but hidden) (*) files under /group/book/four/word/, and sort sorts the result (through the pipe |) numerically (option -n).



    Recursive



    Someone made a comment to this answer mentioning grep -rlc, before to suppress it. Indeed grep is a great alternative, especially if you need recursivity :



    grep -rc '^' /group/book/four/word/|tr ':' ' '|sort -n -k2


    will count (option -c) recursively (option -r) lines matching (grep) '^' (that is, beginning of lines) in the directory /group/book/four/word/. Then you have to replace the colon by a space, e.g. using tr, to help sort, which you want to sort numerically (option -n) on the second column (option -k2).



    Update : See Stephane's comment about possible limitations and how you can actually get rid of tr.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 28 '14 at 20:26

























    answered Nov 27 '14 at 20:17









    Skippy le Grand GourouSkippy le Grand Gourou

    1,1351122




    1,1351122







    • 1





      grep -c . counts the lines that contain at least one valid character. Use grep -c '^' to count all the lines (will also count trailing characters after the last newline with some grep implementations). Note that not all grep implementations support a -r and behaviour varies among those that do. You don't need to translate :s (colon, not semicolon) to spaces for sort. Just use -t:. Note that that assumes file names don't contain : or blank or newline characters.

      – Stéphane Chazelas
      Nov 28 '14 at 18:07












    • Thanks for posting your non-recursive solution; I didn't know wc gave such a handy total all if you pass multiple paths. Coupling that functionality with the wild card and the pipe to sort is really clean.

      – Qcom
      Sep 25 '15 at 21:21












    • 1





      grep -c . counts the lines that contain at least one valid character. Use grep -c '^' to count all the lines (will also count trailing characters after the last newline with some grep implementations). Note that not all grep implementations support a -r and behaviour varies among those that do. You don't need to translate :s (colon, not semicolon) to spaces for sort. Just use -t:. Note that that assumes file names don't contain : or blank or newline characters.

      – Stéphane Chazelas
      Nov 28 '14 at 18:07












    • Thanks for posting your non-recursive solution; I didn't know wc gave such a handy total all if you pass multiple paths. Coupling that functionality with the wild card and the pipe to sort is really clean.

      – Qcom
      Sep 25 '15 at 21:21







    1




    1





    grep -c . counts the lines that contain at least one valid character. Use grep -c '^' to count all the lines (will also count trailing characters after the last newline with some grep implementations). Note that not all grep implementations support a -r and behaviour varies among those that do. You don't need to translate :s (colon, not semicolon) to spaces for sort. Just use -t:. Note that that assumes file names don't contain : or blank or newline characters.

    – Stéphane Chazelas
    Nov 28 '14 at 18:07






    grep -c . counts the lines that contain at least one valid character. Use grep -c '^' to count all the lines (will also count trailing characters after the last newline with some grep implementations). Note that not all grep implementations support a -r and behaviour varies among those that do. You don't need to translate :s (colon, not semicolon) to spaces for sort. Just use -t:. Note that that assumes file names don't contain : or blank or newline characters.

    – Stéphane Chazelas
    Nov 28 '14 at 18:07














    Thanks for posting your non-recursive solution; I didn't know wc gave such a handy total all if you pass multiple paths. Coupling that functionality with the wild card and the pipe to sort is really clean.

    – Qcom
    Sep 25 '15 at 21:21





    Thanks for posting your non-recursive solution; I didn't know wc gave such a handy total all if you pass multiple paths. Coupling that functionality with the wild card and the pipe to sort is really clean.

    – Qcom
    Sep 25 '15 at 21:21











    7














    With zsh:



    lines() REPLY=$(wc -l < $REPLY)
    printf '%sn' /group/book/four/word/*(.no+lines)


    We define a new sorting function lines that replies with the number of lines in the file. And we use the o+lines glob qualifier which together with n (for numeric sort), defines how the results of the glob are ordered. (. also added to only check regular files).



    That makes no assumption on what character the file names may contain other than hidden files (those starting with .) are omitted. Add the D glob qualifier if you want them as well.






    share|improve this answer




















    • 2





      OP is tagged with bash only...

      – l0b0
      Nov 27 '14 at 13:15






    • 7





      @l0b0 that doesn't mean that the next person who needs this will also be running bash.

      – terdon
      Nov 27 '14 at 13:40















    7














    With zsh:



    lines() REPLY=$(wc -l < $REPLY)
    printf '%sn' /group/book/four/word/*(.no+lines)


    We define a new sorting function lines that replies with the number of lines in the file. And we use the o+lines glob qualifier which together with n (for numeric sort), defines how the results of the glob are ordered. (. also added to only check regular files).



    That makes no assumption on what character the file names may contain other than hidden files (those starting with .) are omitted. Add the D glob qualifier if you want them as well.






    share|improve this answer




















    • 2





      OP is tagged with bash only...

      – l0b0
      Nov 27 '14 at 13:15






    • 7





      @l0b0 that doesn't mean that the next person who needs this will also be running bash.

      – terdon
      Nov 27 '14 at 13:40













    7












    7








    7







    With zsh:



    lines() REPLY=$(wc -l < $REPLY)
    printf '%sn' /group/book/four/word/*(.no+lines)


    We define a new sorting function lines that replies with the number of lines in the file. And we use the o+lines glob qualifier which together with n (for numeric sort), defines how the results of the glob are ordered. (. also added to only check regular files).



    That makes no assumption on what character the file names may contain other than hidden files (those starting with .) are omitted. Add the D glob qualifier if you want them as well.






    share|improve this answer















    With zsh:



    lines() REPLY=$(wc -l < $REPLY)
    printf '%sn' /group/book/four/word/*(.no+lines)


    We define a new sorting function lines that replies with the number of lines in the file. And we use the o+lines glob qualifier which together with n (for numeric sort), defines how the results of the glob are ordered. (. also added to only check regular files).



    That makes no assumption on what character the file names may contain other than hidden files (those starting with .) are omitted. Add the D glob qualifier if you want them as well.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 27 '14 at 12:50

























    answered Nov 27 '14 at 12:38









    Stéphane ChazelasStéphane Chazelas

    311k57587945




    311k57587945







    • 2





      OP is tagged with bash only...

      – l0b0
      Nov 27 '14 at 13:15






    • 7





      @l0b0 that doesn't mean that the next person who needs this will also be running bash.

      – terdon
      Nov 27 '14 at 13:40












    • 2





      OP is tagged with bash only...

      – l0b0
      Nov 27 '14 at 13:15






    • 7





      @l0b0 that doesn't mean that the next person who needs this will also be running bash.

      – terdon
      Nov 27 '14 at 13:40







    2




    2





    OP is tagged with bash only...

    – l0b0
    Nov 27 '14 at 13:15





    OP is tagged with bash only...

    – l0b0
    Nov 27 '14 at 13:15




    7




    7





    @l0b0 that doesn't mean that the next person who needs this will also be running bash.

    – terdon
    Nov 27 '14 at 13:40





    @l0b0 that doesn't mean that the next person who needs this will also be running bash.

    – terdon
    Nov 27 '14 at 13:40











    4














    You don't specify whether you also want the files in any subdirectories of /group/book/four/word. The find solution in jherran's answer will descend into subdirectories. If that is not wanted, use the shell instead:



    for file in ./*; do [ -f "$file" ] && wc -l "$file"; done | sort -n


    If your file names can contain newlines, you can use something like:



    for file in ./*; do 
    [ -f "$file" ] &&
    printf "%lu %s" "$(wc -l < "$file")" "$file"
    done | sort -zn | tr '' 'n'


    Finally, if you do want to descend into subdirectories, you can use this in bash 4 or above:



    shopt -s globstar
    for file in ./**/*; do [ -f "$file" ] && wc -l "$file"; done | sort -n


    Note that versions of bash prior to 4.3 were following symlinks when recursively descending the directory tree (like zsh's or tcsh's ***/*).



    Also, all solutions above will ignore hidden files (those whose name starts with a ., use shopt -s dotglob to include them) and will also include the line count of symbolic links (which the find approach will not).






    share|improve this answer

























    • Note that other differences from jherran's solution is that yours will also consider symlink to regular files (-xtype f in GNU find or *(-.) in zsh) and will omit hidden files.

      – Stéphane Chazelas
      Nov 27 '14 at 13:55











    • @StéphaneChazelas thanks, clarified. Why the %lu in printf? As I recall, that means long unsigned decimal, is it really necessary? Why not treat the number as a string? Does it make a difference?

      – terdon
      Nov 27 '14 at 13:59







    • 2





      If the wc output is empty (for instance because the file is not readable), then that will expand to 0 instead of the empty string, which is slightly better. Some sort implementations work with unsigned integers some with signed. %lu sounds like the safest bet, but it doesn't probably matter as if you have 2^31 lines, that will take ages anyway.

      – Stéphane Chazelas
      Nov 27 '14 at 14:01
















    4














    You don't specify whether you also want the files in any subdirectories of /group/book/four/word. The find solution in jherran's answer will descend into subdirectories. If that is not wanted, use the shell instead:



    for file in ./*; do [ -f "$file" ] && wc -l "$file"; done | sort -n


    If your file names can contain newlines, you can use something like:



    for file in ./*; do 
    [ -f "$file" ] &&
    printf "%lu %s" "$(wc -l < "$file")" "$file"
    done | sort -zn | tr '' 'n'


    Finally, if you do want to descend into subdirectories, you can use this in bash 4 or above:



    shopt -s globstar
    for file in ./**/*; do [ -f "$file" ] && wc -l "$file"; done | sort -n


    Note that versions of bash prior to 4.3 were following symlinks when recursively descending the directory tree (like zsh's or tcsh's ***/*).



    Also, all solutions above will ignore hidden files (those whose name starts with a ., use shopt -s dotglob to include them) and will also include the line count of symbolic links (which the find approach will not).






    share|improve this answer

























    • Note that other differences from jherran's solution is that yours will also consider symlink to regular files (-xtype f in GNU find or *(-.) in zsh) and will omit hidden files.

      – Stéphane Chazelas
      Nov 27 '14 at 13:55











    • @StéphaneChazelas thanks, clarified. Why the %lu in printf? As I recall, that means long unsigned decimal, is it really necessary? Why not treat the number as a string? Does it make a difference?

      – terdon
      Nov 27 '14 at 13:59







    • 2





      If the wc output is empty (for instance because the file is not readable), then that will expand to 0 instead of the empty string, which is slightly better. Some sort implementations work with unsigned integers some with signed. %lu sounds like the safest bet, but it doesn't probably matter as if you have 2^31 lines, that will take ages anyway.

      – Stéphane Chazelas
      Nov 27 '14 at 14:01














    4












    4








    4







    You don't specify whether you also want the files in any subdirectories of /group/book/four/word. The find solution in jherran's answer will descend into subdirectories. If that is not wanted, use the shell instead:



    for file in ./*; do [ -f "$file" ] && wc -l "$file"; done | sort -n


    If your file names can contain newlines, you can use something like:



    for file in ./*; do 
    [ -f "$file" ] &&
    printf "%lu %s" "$(wc -l < "$file")" "$file"
    done | sort -zn | tr '' 'n'


    Finally, if you do want to descend into subdirectories, you can use this in bash 4 or above:



    shopt -s globstar
    for file in ./**/*; do [ -f "$file" ] && wc -l "$file"; done | sort -n


    Note that versions of bash prior to 4.3 were following symlinks when recursively descending the directory tree (like zsh's or tcsh's ***/*).



    Also, all solutions above will ignore hidden files (those whose name starts with a ., use shopt -s dotglob to include them) and will also include the line count of symbolic links (which the find approach will not).






    share|improve this answer















    You don't specify whether you also want the files in any subdirectories of /group/book/four/word. The find solution in jherran's answer will descend into subdirectories. If that is not wanted, use the shell instead:



    for file in ./*; do [ -f "$file" ] && wc -l "$file"; done | sort -n


    If your file names can contain newlines, you can use something like:



    for file in ./*; do 
    [ -f "$file" ] &&
    printf "%lu %s" "$(wc -l < "$file")" "$file"
    done | sort -zn | tr '' 'n'


    Finally, if you do want to descend into subdirectories, you can use this in bash 4 or above:



    shopt -s globstar
    for file in ./**/*; do [ -f "$file" ] && wc -l "$file"; done | sort -n


    Note that versions of bash prior to 4.3 were following symlinks when recursively descending the directory tree (like zsh's or tcsh's ***/*).



    Also, all solutions above will ignore hidden files (those whose name starts with a ., use shopt -s dotglob to include them) and will also include the line count of symbolic links (which the find approach will not).







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 3 '15 at 17:04

























    answered Nov 27 '14 at 13:39









    terdonterdon

    133k32264444




    133k32264444












    • Note that other differences from jherran's solution is that yours will also consider symlink to regular files (-xtype f in GNU find or *(-.) in zsh) and will omit hidden files.

      – Stéphane Chazelas
      Nov 27 '14 at 13:55











    • @StéphaneChazelas thanks, clarified. Why the %lu in printf? As I recall, that means long unsigned decimal, is it really necessary? Why not treat the number as a string? Does it make a difference?

      – terdon
      Nov 27 '14 at 13:59







    • 2





      If the wc output is empty (for instance because the file is not readable), then that will expand to 0 instead of the empty string, which is slightly better. Some sort implementations work with unsigned integers some with signed. %lu sounds like the safest bet, but it doesn't probably matter as if you have 2^31 lines, that will take ages anyway.

      – Stéphane Chazelas
      Nov 27 '14 at 14:01


















    • Note that other differences from jherran's solution is that yours will also consider symlink to regular files (-xtype f in GNU find or *(-.) in zsh) and will omit hidden files.

      – Stéphane Chazelas
      Nov 27 '14 at 13:55











    • @StéphaneChazelas thanks, clarified. Why the %lu in printf? As I recall, that means long unsigned decimal, is it really necessary? Why not treat the number as a string? Does it make a difference?

      – terdon
      Nov 27 '14 at 13:59







    • 2





      If the wc output is empty (for instance because the file is not readable), then that will expand to 0 instead of the empty string, which is slightly better. Some sort implementations work with unsigned integers some with signed. %lu sounds like the safest bet, but it doesn't probably matter as if you have 2^31 lines, that will take ages anyway.

      – Stéphane Chazelas
      Nov 27 '14 at 14:01

















    Note that other differences from jherran's solution is that yours will also consider symlink to regular files (-xtype f in GNU find or *(-.) in zsh) and will omit hidden files.

    – Stéphane Chazelas
    Nov 27 '14 at 13:55





    Note that other differences from jherran's solution is that yours will also consider symlink to regular files (-xtype f in GNU find or *(-.) in zsh) and will omit hidden files.

    – Stéphane Chazelas
    Nov 27 '14 at 13:55













    @StéphaneChazelas thanks, clarified. Why the %lu in printf? As I recall, that means long unsigned decimal, is it really necessary? Why not treat the number as a string? Does it make a difference?

    – terdon
    Nov 27 '14 at 13:59






    @StéphaneChazelas thanks, clarified. Why the %lu in printf? As I recall, that means long unsigned decimal, is it really necessary? Why not treat the number as a string? Does it make a difference?

    – terdon
    Nov 27 '14 at 13:59





    2




    2





    If the wc output is empty (for instance because the file is not readable), then that will expand to 0 instead of the empty string, which is slightly better. Some sort implementations work with unsigned integers some with signed. %lu sounds like the safest bet, but it doesn't probably matter as if you have 2^31 lines, that will take ages anyway.

    – Stéphane Chazelas
    Nov 27 '14 at 14:01






    If the wc output is empty (for instance because the file is not readable), then that will expand to 0 instead of the empty string, which is slightly better. Some sort implementations work with unsigned integers some with signed. %lu sounds like the safest bet, but it doesn't probably matter as if you have 2^31 lines, that will take ages anyway.

    – Stéphane Chazelas
    Nov 27 '14 at 14:01












    1














    If you want to install fd a really fast file finder written in Rust (you should install it, it's great to have anyway)



    fd --type=file . | xargs wc -l | sort -n


    Basically fd lists the files, xargs will pass the list of files to wc (stands for word count but passing -l will make it count lines) then finally it's sorted from least number of lines to greatest using sort -n.






    share|improve this answer








    New contributor




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
























      1














      If you want to install fd a really fast file finder written in Rust (you should install it, it's great to have anyway)



      fd --type=file . | xargs wc -l | sort -n


      Basically fd lists the files, xargs will pass the list of files to wc (stands for word count but passing -l will make it count lines) then finally it's sorted from least number of lines to greatest using sort -n.






      share|improve this answer








      New contributor




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






















        1












        1








        1







        If you want to install fd a really fast file finder written in Rust (you should install it, it's great to have anyway)



        fd --type=file . | xargs wc -l | sort -n


        Basically fd lists the files, xargs will pass the list of files to wc (stands for word count but passing -l will make it count lines) then finally it's sorted from least number of lines to greatest using sort -n.






        share|improve this answer








        New contributor




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










        If you want to install fd a really fast file finder written in Rust (you should install it, it's great to have anyway)



        fd --type=file . | xargs wc -l | sort -n


        Basically fd lists the files, xargs will pass the list of files to wc (stands for word count but passing -l will make it count lines) then finally it's sorted from least number of lines to greatest using sort -n.







        share|improve this answer








        New contributor




        JustGage 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




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









        answered yesterday









        JustGageJustGage

        1113




        1113




        New contributor




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





        New contributor





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






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



























            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%2f170275%2flist-files-sorted-by-the-number-of-lines-they-contain%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







            -bash, files, shell, wc

            Popular posts from this blog

            Frič See also Navigation menuinternal link

            Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

            fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox