Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line counting - how to exclude a folder?

Tags:

bash

I'm using bash to count my lines of source in a project. Here's the code:

#!/bin/bash
find . -name '*.java' | xargs wc -l
find . -name '*.xml' | xargs wc -l

I want to exclude files from the ./gen folder. I think it's something to do with 'prune', but I can't figure out how to do it.

EDIT Thankyou to ruakh, but his code wasn't my final solution, I had to make it count the lines again. Here's the finished thing if anybody wants it:

#!/bin/bash
find . -path ./gen -prune -o -name '*.java' | xargs wc -l
find . -name '*.xml' | xargs wc -l
like image 548
Todd Davies Avatar asked Jul 18 '12 15:07

Todd Davies


People also ask

How do I count lines of code in a folder?

Cloc can be used to count lines in particular file or in multiple files within directory. To use cloc simply type cloc followed by the file or directory which you wish to examine. Now lets run cloc on it. As you can see it counted the number of files, blank lines, comments and lines of code.

How do you count all lines in all files in a directory?

The easiest way to count files in a directory on Linux is to use the “ls” command and pipe it with the “wc -l” command. The “wc” command is used on Linux in order to print the bytes, characters or newlines count.

How do I exclude a file from a scan?

(For example, if you select File, you simply need to browse and select the file you want to exclude. Or if you select the File type option, you only need to enter the extension of the file format to skip during scans.)

Is it possible to exclude certain sections of JS code?

Some branches in JS code are typically hard, if not impossible to test. Examples are a hasOwnProperty check, UMD wrappers and so on. Istanbul now has a facility by which coverage can be excluded for certain sections of code. What’s more, 100% coverage isn’t necessary or even reasonable in most cases.

Should I ignore some files/functions for coverage purposes?

Another class of files/functions you might want to ignore for coverage purposes are test-specific helpers. It doesn’t matter that some of them aren’t run as part of tests, as they’re not the code under test. As with a lot of things in software, it’s about tradeoffs.

How to list recursively Ignored files in directories in Linux?

So now if you want to list recursively ignored files in directories, we will be using an additional parameter –untracked-files=all, and the command would be: So now we can see that it is listing all the recursively ignored files in directories also.


1 Answers

I couldn't make ruakh's solution work and ended up with the following instead:

find . -not -path '*/gen*' -a \( -name '*.java' -o -name '*.xml' \)

Also found an interesting egrep option based on GNU find:

find a/ -regextype egrep -not -path '*/gen*' -a -regex '.*(java|xml)$'

In order to count lines in the matched files you can stick xargs cat | wc -l on the end:

find . -not -path '*/gen*' -a \( -name '*.java' -o -name '*.xml' \) | xargs cat | wc -l
like image 55
Thor Avatar answered Oct 07 '22 12:10

Thor