List the number of lines in files

Written by:

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


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).

Stéphane Chazelas

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.

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).


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).


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.

List the number of lines in files
0 votes, 0.00 avg. rating (0% score)

Leave a Reply