Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 11-17-2006, 03:27 PM   #1
LQ Newbie
Registered: Jun 2006
Posts: 3

Rep: Reputation: 0
counting number of lines inside a directory structure.

I have a set of c,c++, text, and a couple of other files inside a directory structure. Some files have a tilde (~) at the end, such as main.cpp~, which are created by the editor to make backups of the original files.

How can I count the number of code lines inside my directoy structure, excluding the tilde files and a few others.

Old 11-17-2006, 03:49 PM   #2
Registered: Sep 2004
Location: NH
Distribution: FC6, FC1-4, RH9, Gentoo 2006.0/1, Slackware 10.1/2,11, Vector SOHO 5.0.1
Posts: 237

Rep: Reputation: 30
First change directory to the directory you'd like to run this from. Then perform the following command:
ls | grep -vc [~]
That will give you a number and that is how many lines are in that directory that don't have a ~ anywhere in it. To filter out more simply add more symbols in between the brackets. However, if any of the symbols are anywhere in the name of the file, that file will be excluded from the count.

For example if you ran:
 ls | grep -vc [~a]
All of the file names that contained the "~" character would be omitted from the count, and all the files that contained the letter "a" in their name would also be omitted.

Hope that helps
Old 11-17-2006, 04:08 PM   #3
Senior Member
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian
Posts: 2,210

Rep: Reputation: 341Reputation: 341Reputation: 341Reputation: 341
find /path/to/the/directory -type f -regex ".*[c|cpp|h|txt]" -print | xargs wc -l
This will do all the files that end in "c" or "cpp" or "h" or "txt". Adjust the regular expressions as necessary. You'll get a line count for each file, and a grand total at the end of the listing.

Oops. Fixed a very bad typo. I had left out the "xargs" part! That is definitely required if you want a count of the lines in these files. Without xargs, you'll get a count of the files, not the lines they contain. Sorry about that typo.

Last edited by haertig; 11-17-2006 at 04:13 PM.
Old 11-17-2006, 04:32 PM   #4
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Just a note on something I found out yesterday which I didn't previously know, ls has an option to hide backup files (those ending in ~):
ls -B
Having said that, find is probably better in this case because it's easier to exclude object files, library files etc, as haertig described.

It may be that there are so many files found that xargs invoked multiple instances of wc, and then your total will be split. Also, some files may contain spaces in which case wc will think one file is two filenames and fail to count them. Therefore, this might be a little more robust:
find /path/to/code/dir -type f -regex ".*[c|cpp|h]" -print0 |xargs -0 cat |wc -l
Old 11-20-2006, 01:50 PM   #5
LQ Newbie
Registered: Jun 2006
Posts: 3

Original Poster
Rep: Reputation: 0
That worked great, thanks guys.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting number of occurance in awk program sarajevo Programming 2 11-01-2006 11:31 AM
counting number of sheets printed from a printe avirup dasgupta Linux - General 2 07-25-2005 08:36 PM
Counting Lines ej25 Programming 20 12-06-2004 03:08 PM
counting number of files akin81 Linux - Newbie 6 03-25-2004 02:53 PM
counting the commented lines using awk [ /* */] itsjvivek Linux - General 8 01-17-2003 09:30 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:22 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration