How to use the command 'loc' (with examples)
The ’loc’ command is a tool written in Rust that counts lines of code in a directory. It can be used to provide line count information about files in a given directory, including statistics for individual files. It also has options to exclude certain files, such as .gitignore files.
Use case 1: Print lines of code in the current directory
Code:
loc
Motivation: The motivation for using this example is to quickly get the line count of all files in the current directory.
Explanation: Without providing any path argument, the ’loc’ command will count the lines of code in the current directory.
Example output:
-------------------------------------------------------------------------------
Language Files Lines Code Comments Blanks
-------------------------------------------------------------------------------
Markdown 1 8 8 0 0
Rust 1 6 6 0 0
-------------------------------------------------------------------------------
Total 2 14 14 0 0
-------------------------------------------------------------------------------
Use case 2: Print lines of code in the target directory
Code:
loc path/to/directory
Motivation: This example is useful when you want to count the lines of code in a specific directory and its subdirectories.
Explanation: By providing the path to the target directory as an argument, the ’loc’ command will count lines of code in that directory and all its subdirectories.
Example output:
-------------------------------------------------------------------------------
Language Files Lines Code Comments Blanks
-------------------------------------------------------------------------------
JavaScript 3 150 118 22 10
CSS 2 50 38 10 2
HTML 1 20 18 0 2
-------------------------------------------------------------------------------
Total 6 220 174 32 14
-------------------------------------------------------------------------------
Use case 3: Print lines of code with stats for individual files
Code:
loc --files
Motivation: This example is helpful when you want to get line count statistics for individual files.
Explanation: By using the ‘–files’ flag, the ’loc’ command provides line count statistics for each file individually, including the number of lines, code lines, comment lines, and blank lines.
Example output:
-------------------------------------------------------------------------------
Language Files Lines Code Comments Blanks
-------------------------------------------------------------------------------
JavaScript 1 100 80 15 5
CSS 1 40 30 8 2
HTML 1 10 9 0 1
-------------------------------------------------------------------------------
Total 3 150 119 23 8
-------------------------------------------------------------------------------
Use case 4: Print lines of code without .gitignore files
Code:
loc -u
Motivation: This example is useful when you want to count lines of code but exclude files specified in .gitignore or similar ignore files.
Explanation: The ‘-u’ flag, when used once, excludes .gitignore files from the line count. If used twice, it will additionally exclude hidden files and directories.
Example output:
-------------------------------------------------------------------------------
Language Files Lines Code Comments Blanks
-------------------------------------------------------------------------------
JavaScript 3 150 118 22 10
CSS 2 50 38 10 2
HTML 1 20 18 0 2
-------------------------------------------------------------------------------
Total 6 220 174 32 14
-------------------------------------------------------------------------------
Conclusion:
The ’loc’ command is a versatile tool for counting lines of code in directories. By using different options and arguments, you can get line count information for the current directory or a specific directory, with statistics for individual files. You can also exclude certain files, such as .gitignore files, from the line count. This command is valuable for software development projects to analyze codebases and track code size.