How to use the command 'tokei' (with examples)
The ’tokei’ command is a program that prints out statistics about code in a directory and its subdirectories. It is a useful tool for developers to analyze their codebase and gain insights into lines of code, code comments, and code size. In this article, we will illustrate different use cases of the ’tokei’ command.
Use case 1: Get a report on the code in a directory and all subdirectories
Code:
tokei path/to/directory
Motivation:
The motivation for using this example is to get an overall report of the code statistics in a specific directory and all its subdirectories. This can be useful for analyzing the codebase of a project and identifying trends or areas that need improvement.
Explanation:
- ’tokei’: The command itself.
- ‘path/to/directory’: The path to the directory for which you want to get code statistics.
Example output:
-----------------------------------------------------------------------
Language Files Lines Code Comments Blanks
-----------------------------------------------------------------------
Rust 10 350 243 52 55
JavaScript 5 250 180 45 25
Markdown 2 100 95 0 5
-----------------------------------------------------------------------
Total 17 700 518 97 85
-----------------------------------------------------------------------
This output shows the code statistics for Rust, JavaScript, and Markdown files in the specified directory and its subdirectories. It includes the number of files, lines of code, lines of comments, and blank lines.
Use case 2: Get a report for a directory excluding “.min.js” files
Code:
tokei path/to/directory -e *.min.js
Motivation:
In some cases, we might want to exclude specific files from the code statistics. This example shows how to exclude “.min.js” files, which are commonly minified versions of JavaScript files, from the report.
Explanation:
- ’tokei’: The command itself.
- ‘path/to/directory’: The path to the directory for which you want to get code statistics.
- ‘-e .min.js’: The “-e” flag followed by “.min.js” excludes any files with the “.min.js” extension from the code statistics.
Example output:
-----------------------------------------------------------------------
Language Files Lines Code Comments Blanks
-----------------------------------------------------------------------
Rust 8 250 180 45 25
Markdown 1 50 45 0 5
-----------------------------------------------------------------------
Total 9 300 225 45 30
-----------------------------------------------------------------------
The output above shows the code statistics for Rust and Markdown files in the specified directory and its subdirectories, excluding the “.min.js” files.
Use case 3: Print out statistics for individual files in a directory
Code:
tokei path/to/directory --files
Motivation:
Sometimes, we may need detailed statistics for individual files rather than just an aggregated report. This example demonstrates how to print out the code statistics for each file in the specified directory.
Explanation:
- ’tokei’: The command itself.
- ‘path/to/directory’: The path to the directory for which you want to get code statistics.
- ‘–files’: This flag instructs the command to print out the code statistics for each individual file in the directory.
Example output:
--------------------------------
Filename Lines Code
--------------------------------
main.rs 100 80
lib.rs 50 40
--------------------------------
Total 150 120
--------------------------------
The output above shows the code statistics for individual files in the specified directory. It includes the filename, lines of code, and lines of comments for each file.
Use case 4: Get a report for all files of type Rust and Markdown
Code:
tokei path/to/directory -t=Rust,Markdown
Motivation:
If we only want to analyze specific types of files, we can use filtering by file extension. This example demonstrates how to get a report for all files of type Rust and Markdown.
Explanation:
- ’tokei’: The command itself.
- ‘path/to/directory’: The path to the directory for which you want to get code statistics.
- ‘-t=Rust,Markdown’: The “-t” flag followed by “=Rust,Markdown” filters the code statistics to only include files with the specified extensions.
Example output:
-----------------------------------------------------------------------
Language Files Lines Code Comments Blanks
-----------------------------------------------------------------------
Rust 10 350 243 52 55
Markdown 2 100 95 0 5
-----------------------------------------------------------------------
Total 12 450 338 52 60
-----------------------------------------------------------------------
The output above shows the code statistics for Rust and Markdown files in the specified directory and its subdirectories, excluding other file types.
Conclusion:
The ’tokei’ command is a powerful tool for analyzing code statistics, providing insights into the size and composition of a codebase. It offers various options to customize the type of information returned, making it a valuable asset for developers. By understanding and utilizing the different use cases of the ’tokei’ command, developers can gain better insights into their code and improve the quality and efficiency of their projects.