LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 11-17-2006, 02:27 PM   #1
vl@d
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.

thanks
 
Old 11-17-2006, 02:49 PM   #2
caps_phisto
Member
 
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:
Code:
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:
Code:
 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, 03:08 PM   #3
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,331

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
Code:
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.

[edit]
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.
[/edit]

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

Rep: Reputation: 65
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 ~):
Code:
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:
Code:
find /path/to/code/dir -type f -regex ".*[c|cpp|h]" -print0 |xargs -0 cat |wc -l
 
Old 11-20-2006, 12:50 PM   #5
vl@d
LQ Newbie
 
Registered: Jun 2006
Posts: 3

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


Reply



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 10:31 AM
counting number of sheets printed from a printe avirup dasgupta Linux - General 2 07-25-2005 07:36 PM
Counting Lines ej25 Programming 20 12-06-2004 02:08 PM
counting number of files akin81 Linux - Newbie 6 03-25-2004 01:53 PM
counting the commented lines using awk [ /* */] itsjvivek Linux - General 8 01-17-2003 08:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:06 AM.

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