counting number of lines inside a directory structure.
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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]
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.