loc scans the current directory tree and reports lines of code by language, separating test code from non-test code and emitting pretty JSON via prettyx.
go build -o bin/loc -trimpath -ldflags="-s -w" .
sudo install bin/loc /usr/local/bin/
# or...
go install pkt.systems/loc@latest
loc [flags] [extensions...]
By default, loc scans the current working directory and counts all supported
languages. Providing extensions limits the scan to those languages.
Use -d multiple times to scan and aggregate multiple directories.
Repository-local .gitignore files are honored while scanning.
Examples:
loc
loc .go
loc .go .c .cpp
loc -d proj1 -d proj2
When you pass extensions, loc expands them to the full set of extensions for
the matching languages. This avoids partial counts for languages that use
multiple extensions.
Examples:
loc .hcounts C and Objective-C headers and also includes their related extensions (e.g..c,.m,.mm).loc .mincludes MATLAB and Objective-C sources (disambiguated by content).
Ambiguous extensions like .h and .m are resolved by inspecting file content.
The JSON includes:
loc,test_loc,code_locpercent_test_loc,percent_code_loc(100 == 100%)languagesmap with per-languageloc,test_loc,code_loc- Optional test counters (e.g., Go
test_count,example_count,benchmark_count,fuzz_count)
- Go (
.go) - Kotlin (
.kt,.kts) - Java (
.java) - JavaScript (
.js,.jsx,.mjs,.cjs) - TypeScript (
.ts,.tsx,.mts,.cts) - Python (
.py) - PHP (
.php) - Ruby (
.rb) - Rust (
.rs) - Swift (
.swift) - C (
.c,.h) - C++ (
.cpp,.cc,.cxx,.hpp,.hh,.hxx) - Objective-C (
.m,.mm,.h) - C# (
.cs) - Scala (
.scala) - Groovy (
.groovy) - Perl (
.pl,.pm,.t) - Dart (
.dart) - Lua (
.lua) - R (
.r,.R) - MATLAB (
.m) - Shell (
.sh,.bash,.zsh)
Tests are detected using common, deterministic conventions:
- Go:
_test.gofiles; countsTest,Example,Benchmark,Fuzzfunctions. - Java/Kotlin:
src/testor*Test.*; counts@Test. - JS/TS:
__tests__,test/,tests/,*.test.*,*.spec.*; countsdescribe/it/test. - Python:
test_*.py/*_test.pyortests/; countsdef test_. - PHP:
tests/or*Test.php; counts@testandfunction test*. - Ruby:
test/orspec/; countsdef test_,describe,it. - Rust:
tests/or*_test.rsfor test LOC; counts#[test]anywhere. - Swift:
Tests/or*Tests.swift; countsfunc test*. - C/C++/Shell: test LOC via
test(s)/or filename patterns; no test counts. - Objective-C:
Tests/or*Test(s).m/.mm; counts- (void)test*. - C#: test LOC via
test(s)/or*Test(s).cs; counts[Test],[Fact],[Theory]. - Scala:
src/testor*Test.scala; countstest(/it(/should(. - Groovy:
src/testor*Test.groovy; counts@Testordef "...". - Perl:
t/or*.t; countsok(andsubtest(. - Dart:
test/or*_test.dart; countstest(andgroup(. - Lua:
spec/or*_spec.lua; countsdescribe/it. - R:
tests/ortestthat/; countstest_that(. - MATLAB:
tests/or*_test.m; no test counts.