Issue
I use this shell script to count the lines of code in a Django project,
find . -name "*.py" -type f -exec grep . {} \; | wc -l
How can I modify this to not count the migration scripts? Essentially that means not count anything inside any subfolder by the name migrations
.
Solution
Given you want to exclude all files in a directory named migrations
, you can add a condition to the find
command:
find . -name "*.py" -type f ! -path '*/migrations/*' -exec grep . {} \; | wc -l
Note that counting the number of lines is probably easier with cloc
[GitHub]:
cloc --not-match-d=migrations .
This will then generate a summary like:
$ cloc --not-match-d='migrations' .
102 text files.
86 unique files.
38 files ignored.
github.com/AlDanial/cloc v 1.74 T=0.88 s (97.3 files/s, 42411.8 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
XML 2 11 32 33299
Python 63 619 273 2673
HTML 18 35 49 304
JavaScript 2 8 5 100
CSS 1 5 6 81
-------------------------------------------------------------------------------
SUM: 86 678 365 36457
-------------------------------------------------------------------------------
Answered By - Willem Van Onsem Answer Checked By - David Goodson (WPSolving Volunteer)