Saturday 2 February 2008

Unix tips: count total number of lines in a directory tree of files

to count the number of lines in a subdirectory of files.

find monthly_reports/ -iname '*.csv' \
| xargs -n 1 wc -l \
| cut -d' ' -f1 \
| (SUM=0; while read NUM; do SUM=$(($SUM+$NUM)); done; echo $SUM)

PROBLEM: this has trouble if file names have spaces in them.


To find out how many lines in total are contained in a subdirectory of
files, there is the "Wc" command, which is a recursive version of "wc"

Wc -l monthly_reports/*/*.csv

-l specifies count the lines

No comments: