Sign in to follow this  

Recursively counting file lines through directory heirarchy

This topic is 4826 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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].

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this