Count lines of multiple similar files in unix

In continuation to my earlier post regarding concatenation similar files into one. I am writing this post to demonstrate counting lines of similar kind of files in a given directory and sub-directory.

Below command will find all the logs with given name pattern inside the directory including sub directories and then WC will count it’s lines and display on screen in total.

wc -l `find . -name usage*.log`

Sample output of command

     4 ./2011-08-08/usage.log
     5 ./2011-08-09/usage.log
     5 ./2011-08-10/usage.log
     4 ./2011-08-11/usage.log
     2 ./2011-08-12/usage.log
     4 ./2011-08-13/usage.log
   24 total

Below command will not count the blank lines.

grep -cv ‘^\s*$’ `find . -name “usag*.log”`

Sample Output, see below blank lines are not counted.

./2011-08-08/usage.log:3
./2011-08-09/usage.log:4
./2011-08-10/usage.log:4
./2011-08-11/usage.log:3
./2011-08-12/usage.log:1
./2011-08-13/usage.log:3

About nitingautam

I am Tech Lead (Java/J2EE/ExtJs) with a MNC located @ Gurgaon.
This entry was posted in HowTo, unix and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *