How to Use the Command 'tokei' (with Examples)

How to Use the Command 'tokei' (with Examples)

Tokei is a powerful open-source tool that analyzes your codebase to provide statistics about your code. Whether you’re looking to measure lines of code, comments, or blanks as a developer or project manager, Tokei offers insightful metrics you need. Particularly useful in large and multi-language projects, Tokei reveals vital insights into code distribution, helping you manage, maintain, and improve your codebase effectively.

Use case 1: Display a report for the code in a directory and all subdirectories

Code:

tokei path/to/directory

Motivation:

When managing a large project, you might want to get a holistic view of all the code written. This includes understanding not only the main files but everything within the entire directory structure, capturing every file and folder. Using Tokei for this approach gives you insights into the overall scale and structure of your project. This is particularly useful when embarking on new projects or at stages where comprehensive maintenance is required.

Explanation:

  • tokei: Invokes the Tokei program.
  • path/to/directory: Specifies the path to the directory you wish to analyze. Tokei will recursively explore this directory, generating statistics for all files and subdirectories within it.

Example Output:

-------------------------------------------------------------------------------
 Language            Files        Lines         Code     Comments       Blanks
-------------------------------------------------------------------------------
 Rust                 10          1500         1300          100          100
 Markdown             5            800          500          200          100
-------------------------------------------------------------------------------
 Total                15          2300         1800          300          200
-------------------------------------------------------------------------------

Use case 2: Display a report for a directory excluding .min.js files

Code:

tokei path/to/directory -e *.min.js

Motivation:

Minified JavaScript files (*.min.js) are often byproducts of build processes and not manually maintained by developers. They add unnecessary bulk to your code statistics, and excluding them helps in focusing on source files that actually need review. This subset exclusion is beneficial during development audits or when estimating the amount of actual, maintainable code in a project.

Explanation:

  • tokei: Again, the command program.
  • path/to/directory: The directory to analyze.
  • -e *.min.js: This flag denotes exclusion, asking Tokei to ignore all files ending in .min.js while aggregating statistics.

Example Output:

-------------------------------------------------------------------------------
 Language            Files        Lines         Code     Comments       Blanks
-------------------------------------------------------------------------------
 Rust                  5          1000          900           50           50
 JavaScript            3            700          600          50            50
-------------------------------------------------------------------------------
 Total                 8          1700         1500          100          100
-------------------------------------------------------------------------------

Use case 3: Display statistics for individual files in a directory

Code:

tokei path/to/directory --files

Motivation:

Identifying the individual contribution of each file in a directory is essential for fine-tuned analysis. Perhaps you’re interested in singling out particularly large or complex files that might need refactoring. This granular analysis provides visibility into each file’s size and complexity, offering a keen sense of intricacy and distribution within your codebase.

Explanation:

  • tokei: The command initiating the analysis.
  • path/to/directory: The directory under scrutiny.
  • --files: This flag tells Tokei to produce output for each file separately, rather than compiling it into a collective report.

Example Output:

file1.js
-------------------------------------------------------------------------------
 Language           Lines         Code     Comments       Blanks
-------------------------------------------------------------------------------
 JavaScript          200           150           30           20
-------------------------------------------------------------------------------

file2.rs
-------------------------------------------------------------------------------
 Language            Lines         Code     Comments       Blanks
-------------------------------------------------------------------------------
 Rust                300           250           30           20
-------------------------------------------------------------------------------
Total

Use case 4: Display a report for all files of type Rust and Markdown

Code:

tokei path/to/directory -t=Rust,Markdown

Motivation:

If you’re working with multiple languages but interested in only specific ones, you need to filter your analysis to focus on them. This use case effectively narrows down statistics to languages or file types that matter for your current context, which is especially useful in multi-language codebases when preparing language-specific reports or insights.

Explanation:

  • tokei: Initiates the program.
  • path/to/directory: Denotes the directory to check.
  • -t=Rust,Markdown: Specifies the types of files or programming languages to include in the report, in this case, Rust and Markdown. This tells Tokei to ignore any languages or file types not specified in the argument.

Example Output:

-------------------------------------------------------------------------------
 Language            Files        Lines         Code     Comments       Blanks
-------------------------------------------------------------------------------
 Rust                  6            1200         1000           100         100
 Markdown              4            600           400            150         50
-------------------------------------------------------------------------------
 Total                10           1800         1400            250        150
-------------------------------------------------------------------------------

Conclusion:

Tokei is an invaluable tool for understanding and managing your codebase through its analysis and statistics features. Whether you’re looking at comprehensive project-wide statistics or focused language-specific metrics, Tokei provides a wide scope of customizable options to suit your needs. By employing these examples, developers and project managers alike can gain critical insights into their projects, enhancing productivity and guiding successful code maintenance strategies.

Related Posts

Harnessing the Power of 'az repos' Command in Azure DevOps (with examples)

Harnessing the Power of 'az repos' Command in Azure DevOps (with examples)

The az repos command is an integral part of the Azure CLI designed to manage Azure DevOps repositories effectively.

Read More
How to use the command 'ruff format' (with examples)

How to use the command 'ruff format' (with examples)

The ruff format command is a specialized tool designed for Python developers who seek to maintain clean, well-structured code.

Read More
How to use the command 'msfconsole' (with examples)

How to use the command 'msfconsole' (with examples)

The Metasploit Framework is a powerful open-source tool utilized widely for developing, testing, and executing exploits to check systems for vulnerabilities.

Read More