Counting lines including subdirectories

Peter Gordon peter at thecodergeek.com
Mon Dec 18 07:09:41 UTC 2006


One way to do it using find (the first "$" is your shell prompt):

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

Replace the /path/to/sources directory and '*.c' extension as you need.
You can also put the whole thing on one line if you'd like (just remove
the backslashes between the lines if you do); but I like to keep the
piped commands on separate lines for clarity. Granted, it's probably not
the most efficient way, but it works... :]


That will give you all file names in that directory (recursive) as well
as the lines count in each of them, such as:


-- 
Peter Gordon (codergeek42)
GnuPG Public Key ID: 0xFFC19479 / Fingerprint:
  DD68 A414 56BD 6368 D957 9666 4268 CB7A FFC1 9479
My Blog: http://thecodergeek.com/blog/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://listman.redhat.com/archives/fedora-list/attachments/20061217/e9ebe427/attachment-0001.sig>


More information about the fedora-list mailing list