Understanding the 'loc' Command for Counting Lines of Code (with Examples)
The ’loc’ command is a tool written in Rust that is designed to count the number of lines of code in either the current directory or a specified directory structure. This command provides a convenient way to assess code size or project complexity, which can be useful during code audits, refactoring, or simply to gain insight into the codebase’s structure. More information about the tool can be found on its GitHub repository .
Use case 1: Print lines of code in the current directory
Code:
loc
Motivation:
Often, developers want to quickly determine the total number of lines of code in their current working directory. This is particularly useful during the early stages of a project or when conducting a quick audit of one’s own progress. The ’loc’ command, when run without any additional arguments, will automatically calculate and report this information for the immediate working directory.
Explanation:
- Running
loc
without specifying a directory assumes the command should consider the current directory. It totals up the lines of code in all files it deems relevant, making no distinction between file types initially, except for automatically ignoring files listed in typical.gitignore
files.
Example Output:
----------------------------------------------------
Language Files Lines Code
----------------------------------------------------
Rust 7 1534 1020
Markdown 3 304 300
----------------------------------------------------
Total 10 1838 1320
Use case 2: Print lines of code in the target directory
Code:
loc path/to/directory
Motivation:
This command is useful when you need to analyze lines of code in a directory other than the current one. For instance, if you’re comparing different projects or working on a networked drive with separate project folders, specifying the directory allows direct analysis without changing the current working directory in the terminal.
Explanation:
path/to/directory
: This specifies the directory you wish to analyze. The ’loc’ command will navigate into this directory path and calculate the line statistics for all relevant files found therein.
Example Output:
----------------------------------------------------
Language Files Lines Code
----------------------------------------------------
HTML 5 1234 1200
CSS 8 432 430
JavaScript 3 600 550
----------------------------------------------------
Total 16 2266 2180
Use case 3: Print lines of code with stats for individual files
Code:
loc --files
Motivation:
Developers and project managers often need to delve deeper into specific files to understand how code is distributed across the project. This use case provides a per-file breakdown, which can be invaluable when identifying areas of the codebase that have high complexity or are in need of refactoring.
Explanation:
--files
: This flag triggers the command to include individual file statistics in its output. Each file’s line count will be reported separately, alongside a comprehensive project summary at the end.
Example Output:
----------------------------------------------------
Language Files Lines Code
----------------------------------------------------
example.js 1 200 180
main.py 1 450 420
README.md 1 123 120
----------------------------------------------------
Total 3 773 720
Use case 4: Print lines of code without .gitignore (etc.) files
Code:
loc -u
Motivation:
By default, the ’loc’ command respects .gitignore
and other common ignore files, excluding these from the line count analysis. However, in some cases, you might want to include these files in your count—perhaps to audit files intentionally excluded from version control but significant to the local setup or testing.
Explanation:
-u
: The-u
flag overrides the default behavior of excluding ignored files. It ensures that all files, even those normally ignored by.gitignore
(and similar mechanisms), are analyzed. Using two-u
flags (not shown here) will further include hidden files and directories.
Example Output:
----------------------------------------------------
Language Files Lines Code
----------------------------------------------------
Configuration 2 100 100
Script 1 300 290
----------------------------------------------------
Total 3 400 390
Conclusion:
The ’loc’ command provides a powerful, efficient way to analyze codebases quantitatively. Whether assessing the total lines of code in a project, examining individual file contributions, or including files usually ignored, ’loc’ offers flexibility and depth for developers aiming to understand more about their code or share insightful metrics with their team. By leveraging the diverse functionalities of ’loc’, you can effectively track and manage project growth and complexity over time.