Showing posts with label csv. Show all posts
Showing posts with label csv. Show all posts

Thursday, 12 July 2012

Getting number of lines in all files in a directory - Unix

The bash command
 wc -l <file>
will print out the number of lines in <file>.

If you have a collection of files (.csv in the example below), and want to know how many rows are in each file, the command:
 for f in *.csv; do wc -l $f; done  
will print out a list of all the files ending in .csv, along with the number of lines in them.

To print the number of lines, without the filename, the cut command can be used, with the following parameters:
cut -d' ' -f1  

Combining them, we can use:
 for f in *.csv; do wc -l $f |cut -d' ' -f1; done  
to print out just the number of lines in each file ending in .csv in the directory.