List text files sorted by line count

Alec Jacobson

January 05, 2014

weblog/

Here's a bash snippet to list the text files (only text files, no binaries) in the current directory sorted by line count:

find . -depth 1 -type f -exec grep -Il "" {} \; | xargs wc -l | sort

This outputs something like:

    6 ./alexa.h
   35 ./Astrid.h
   70 ./Henrick.sh
   74 ./dagmar.h
  133 ./Adrea.cpp
  168 ./max.cxx
  216 ./Sabina.cpp
 2339 ./Thorsten.cpp
39991 ./albrecht.off

Update: For the particular case of wc (and maybe) others its faster to do this:

wc -l `grep -Il "" *` | sort