Advertisement

Recursively counting file lines through directory heirarchy

Started by September 18, 2004 02:01 PM
8 comments, last by Q-Parser 19 years, 11 months ago
I'm somewhat new to unix and I'm trying to figure out how to count the lines in all the files throughout a directory heirarchy. I was trying:
ls -R | wc -l
but that won't work because I have to supply wc with the file list on the command line, not through stdin. As it stands, its just counting the number of files. I was thinking there may be some way to use the "env" program to execute "wc -l ..." from stdin, but I can't figure it out. [help]
find . -exec wc {} \;

This will find all files below the current directory (the "."), exec the wc command on each file (the "{}" represents the filename returned by find).

Find is a wonderful command, but can be hopelessly cryptic to the uninitiated.

Advertisement
Thanks, that works very well except that it doesn't return a total number of lines since each wc is executed separately. I've been using
wc -l * */* */*/* */*/*/* */*/*/*/*
which works for the moment, although its really limited and hackish.
You could try this,

find . -type f | xargs wc -l

-M
Wow magic! I'm really going to have to learn how this find command works.
If you only want .cpp files, try
wc -l `find | grep .cpp$`

Those are backticks (`), not apostrophes ('). They're on the tilde (~) key (next to the 1). What they do is take the output of whatever's in the backticks and insert it into the shell.

find & grep are easily the two most convoluted commands in unix. The man pages have a wealth of information, but you probably already knew that[grin].
Advertisement
Quote:
I'm somewhat new to unix and I'm trying to figure out how to count the lines in all the files throughout a directory heirarchy. I was trying:

ls -R | wc -l

If I understand it correctly, this is what you need:

ls -R | cat |wc -l
Quote: Original post by j0seph
Quote:
I'm somewhat new to unix and I'm trying to figure out how to count the lines in all the files throughout a directory heirarchy. I was trying:

ls -R | wc -l

If I understand it correctly, this is what you need:

ls -R | cat |wc -l


The cat only serves takes the raw input and spits it back out unchanged, that command is the same as:

ls -R | wc -l

Unless I'm missing something. I think something like:

find . \( -type f \) | grep \.html | xargs wc -l

The "-type f" for find means that it only returns real files, not directories, symlinks or other things that might screw up wc.

The \.html makes grep return files who's names contain that exact string (If you don't escape the . it gets counted as part of a regular expression).

This outputs a nice form:

....
1685 ./webalizer/games/usage_200408.html
1356 ./webalizer/games/usage_200409.html
36879 total

Cheers,
Aaron
Unless your wc is different to mine, then wc can take input from stdin

ls -R | wc -l

works here
It does the job here, too!

This topic is closed to new replies.

Advertisement