Count Lines of Source Code on Linux Using Tokei

What is Tokei and how is it useful to developers? Tokei is a very powerful program that is used to display code statistics. This means it can count the total number of files, total lines within the files or code, can show comments, blanks etc and groups them according to language used.

Features of Tokei

Tokei has a number of intuitive features that makes it a program to go for. These include:

  • Very Fast – Tokei can count millions of lines of codes within seconds.
  • Accuracy – Tokei is accurate in that it correctly handles both multi-line comments and nested comments and doesn’t comments that are in strings.
  • Multiple formats – It can give outputs in various formats such as JSON, YAML and CBOR, hence the output can be easily stored and reused.
  • Huge language range – It supports more that 150 languages with their extensions.
  • Support for Operating systems – Tokei is available on Mac, Windows and Linux Operating systems.
  • Tokei is a Library making it easy to be integrated with other projects.
  • Can be installed with and without color. The variable No_COLOR st to 1 makes it black and white.

Installing Tokei utility on Linux / macOS

To manually install Tokei in Linux, use the steps below:

First download the latest version of Tokei using wget as shown. Check the pre-built binaries for you to download in gitub.

sudo wget -qO tokei.tar.gz https://github.com/XAMPPRocky/tokei/releases/latest/download/tokei-x86_64-unknown-linux-gnu.tar.gz

Extract the file to /usr/local/bin directory.

sudo tar xf tokei.tar.gz -C /usr/local/bin

You can check the version installed.

$ tokei --version
tokei 13.0.0 compiled with serialization support: json, cbor, yaml

You can also install with the corresponding package managers in other Linux distributions.

# Arch Linux / Manjaro
sudo pacman -S tokei

# Fedora
sudo dnf install tokei

# OpenSUSE
sudo zypper install tokei

# FreeBSD
pkg install tokei

# Cargo
cargo install tokei

# Alpine Linux (Since 3.13)
apk add tokei

You can also build and install Tokei from source. This requires the latest stable Rust compiler. Check the installation example on Ubuntu 24.04 below.

First, update your system packages:

sudo apt -y update
sudo apt -y upgrade
sudo reboot

Install Rust on Linux:

sudo apt -y install rustc

Install Tokei:

$ cargo install --git https://github.com/XAMPPRocky/tokei.git tokei

-------------
-------------
Compiling tokei v12.1.2 (/home/lorna/.cargo/git/checkouts/tokei-d1ad1f1a744b4594/0152709)
    Finished release [optimized] target(s) in 7m 38s
  Installing /home/lorna/.cargo/bin/tokei
   Installed package `tokei v12.1.2 (https://github.com/XAMPPRocky/tokei.git#0152709e)` (executable `tokei`)
warning: be sure to add `/home/lorna/.cargo/bin` to your PATH to be able to run the installed binaries

$ PATH=$PATH:~/.cargo/bin

Install Tokei on Windows and MacOS as shown below:

On MacOS:

# With Homebrew
brew install tokei

# With MacPorts
sudo port selfupdate
sudo port install tokei

On Windows:

scoop install tokei

Configuring Tokei on Linux

Create a configuration file that enables you to change the default behavior. The file can be named tokei.toml or .tokeirc and can be placed in the current directory, home directory or configuration directory. Example configuration as below:

# The width of the terminal output in columns.
columns = 80

# Sort languages based on the specified column.
sort = "lines"

# If set, tokei will only show the languages in `types`.
types = ["Python"]

# Any doc strings (e.g. `"""hello"""` in python) will be counted as comments.
treat_doc_strings_as_comments = true

How to Use Tokei to count lines of code

Once you install tokei, you can simply run tokeito display code statistics for the current directory

$ tokei 
===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Markdown                8          194            0          124           70
 Python                  4           27           18            2            7
 Plain Text              1            1            0            1            0
 YAML                    3           53           53            0            0
-------------------------------------------------------------------------------
 Jupyter Notebooks       1            0            0            0            0
 |- Python               1           16           13            0            3
 (Total)                             16           13            0            3
===============================================================================
 Total                  17          275           71          127           77
===============================================================================

To list the supported languages and extensions, run the below command. The list is a bit long.

$ tokei -l
ABNF
ABAP
ActionScript
Ada
Agda
Alex
Alloy
Arduino C++
AsciiDoc
ASN.1
ASP
ASP.NET
Assembly
GNU Style Assembly
AutoHotKey
Autoconf
Automake
BASH
Batch
Bean
BrightScript
C
.....
.....

If you have a specific file/code in a specific directory that you wish to display its statistics, pass the directory name as shown. This will report on the specified folder and its subdolders.

$ tokei /home/lorna 
===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Markdown                8          194            0          124           70
 Python                  4           27           18            2            7
 Plain Text              1            1            0            1            0
 YAML                    3           53           53            0            0
-------------------------------------------------------------------------------
 Jupyter Notebooks       1            0            0            0            0
 |- Python               1           16           13            0            3
 (Total)                             16           13            0            3
===============================================================================
 Total                  17          275           71          127           77
===============================================================================

Use a comma or space to separate folders if you wish to report on multiple folders.

$ tokei ./foo ./bar ./baz
# or
$ tokei ./foo, ./bar, ./baz

To exclude folders, tokei inspects .gitignore and .ignore files where you can list paths to exclude but you can also use --excludefor excuding additional files. Excluded paths can also be listed in .tokeignore file using the same syntax as .gitignore files. Simply create a file with one of the names and add the paths to be ignored.

tokei /<path-to-inspect> --exclude *.rs

You can also sort what you wish to display. By default tokei sorts alphabetically by language but with --sorttokei can sort the columns by blanks, comments, lines and code

$ tokei /home/lorna --sort code
===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 YAML                    3           53           53            0            0
 Python                  4           27           18            2            7
 Markdown                8          194            0          124           70
 Plain Text              1            1            0            1            0
-------------------------------------------------------------------------------
 Jupyter Notebooks       1            0            0            0            0
 |- Python               1           16           13            0            3
 (Total)                             16           13            0            3
===============================================================================
 Total                  17          275           71          127           77
===============================================================================

To output individual file statistics with Tokei instead of the default total of the languages, use --files flag

$ tokei /home/lorna/ --files
===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Markdown                8          194            0          124           70
-------------------------------------------------------------------------------
 |orna/Nextcloud/Readme.md            1            0            0            1
 |loud/Templates/Readme.md           12            0            9            3
 |emplates/Product plan.md           58            0           32           26
 |mplates/Meeting notes.md           25            0           16            9
 |loud/Documents/Readme.md            3            0            2            1
 |oud/Documents/Example.md           19            0           14            5
 |xtcloud/Photos/Readme.md            3            0            2            1
 |na/linux-amd64/README.md           73            0           49           24
-------------------------------------------------------------------------------
 Python                  4           27           18            2            7
-------------------------------------------------------------------------------
 /home/lorna/test.py                  8            5            0            3
 /home/lorna/testpsutil.py            9            8            1            0
 /home/lorna/pytest1.py               7            3            1            3
 /home/lorna/teststdin.py             3            2            0            1
-------------------------------------------------------------------------------
 Plain Text              1            1            0            1            0
-------------------------------------------------------------------------------
 /home/lorna/file1.txt                1            0            1            0
-------------------------------------------------------------------------------
 YAML                    3           53           53            0            0
-------------------------------------------------------------------------------
 /home/lorna/k0s.yaml                23           23            0            0
 |common/config/config.yml           10           10            0            0
 /home/lorna/k0sctl.yaml             20           20            0            0
-------------------------------------------------------------------------------
 Jupyter Notebooks       1            0            0            0            0
 |- Python               1           16           13            0            3
 (Total)                             16           13            0            3
-------------------------------------------------------------------------------
 /home/lorna/test1.ipynb
 |- Jupyter Notebooks                 0            0            0            0
 |- Python                           16           13            0            3
 /home/lorna/test1.ipynb              0            0            0            0
===============================================================================
 Total                  17          275           71          127           77
===============================================================================

You can also define the kind of output you want. This could be json, yaml, cbor or all. See below example:

$ tokei /home/lorna/ --output json
{"Jupyter Notebooks":{"blanks":0,"children":{"Python":[{"name":"/home/lorna/test1.ipynb","stats":{"blanks":3,"blobs":{},"code":13,"comments":0}}]},"code":0,"comments":0,"inaccurate":false,"reports":[{"name":"/home/lorna/test1.ipynb","stats":{"blanks":0,"blobs":{"Python":{"blanks":3,"blobs":{},"code":13,"comments":0}},"code":0,"comments":0}}]},"Markdown":{"blanks":70,"children":{},"code":0,"comments":124,"inaccurate":false,"reports":[{"name":"/home/lorna/Nextcloud/Readme.md","stats":{"blanks":1,"blobs":{},"code":0,"comments":0}},{"name":"/home/lorna/Nextcloud/Templates/Readme.md","stats":{"blanks":3,"blobs":{},"code":0,"comments":9}},{"name":"/home/lorna/Nextcloud/Templates/Meeting notes.md","stats":{"blanks":9,"blobs":{},"code":0,"comments":16}},{"name":"/home/lorna/Nextcloud/Documents/Readme.md","stats":{"blanks":1,"blobs":{},"code"

$ tokei /home/lorna/ --output yaml
---
Jupyter Notebooks:
  blanks: 0
  children:
    Python:
      - name: /home/lorna/test1.ipynb
        stats:
          blanks: 3
          blobs: {}
          code: 13
          comments: 0
  code: 0
  comments: 0
  inaccurate: false
  reports:
    - name: /home/lorna/test1.ipynb
      stats:
        blanks: 0
        blobs:
          Python:
            blanks: 3
            blobs: {}
            code: 13
            comments: 0
        code: 0
        comments: 0

$ tokei /home/lorna/ --output cbor
a6714a757079746572204e6f7465626f6f6b73a666626c616e6b7300686368696c6472656ea166507974686f6e81a2646e616d65772f686f6d652f6c6f726e612f74657374312e6970796e62657374617473a466626c616e6b730365626c6f6273a064636f64650d68636f6d6d656e74730064636f64650068636f6d6d656e7473006a696e6163637572617465f4677265706f72747381a2646e616d65772f686f6d652f6c6f726e612f74657374312e6970796e62657374617473a466626c616e6b730065626c6f6273a166507974686f6ea466626c616e6b730365626c6f6273a064636f64650d68636f6d6d656e74730064636f64650068636f6d6d656e747300684d61726b646f776ea666626c616e6b731846686368696c6472656ea064636f64650068636f6d6d656e7473187c6a696e6163637572617465f4677265706f72747388a2646e616d65781f2f686f6d652f6c6f

Check the options to be used with tokei using -h flag:

$ tokei -h
tokei 12.1.2 compiled with serialization support: json, cbor, yaml
Erin P. <[email protected]> + Contributors
Count your code, quickly.
Support this project on GitHub Sponsors: https://github.com/sponsors/XAMPPRocky

USAGE:
    tokei [FLAGS] [OPTIONS] [--] [input]...

FLAGS:
    -C, --compact             Do not print statistics about embedded languages.
    -f, --files               Will print out statistics on individual files.
    -h, --help                Prints help information
        --hidden              Count hidden files.
    -l, --languages           Prints out supported languages and their extensions.
        --no-ignore           Don't respect ignore files (.gitignore, .ignore, etc.). This implies --no-ignore-parent,
                              --no-ignore-dot, and --no-ignore-vcs.
        --no-ignore-dot       Don't respect .ignore and .tokeignore files, including those in parent directories.
        --no-ignore-parent    Don't respect ignore files (.gitignore, .ignore, etc.) in parent directories.
        --no-ignore-vcs       Don't respect VCS ignore files (.gitignore, .hgignore, etc.), including those in parent
                              directories.
    -V, --version             Prints version information
    -v, --verbose             Set log output level:
                                          1: to show unknown file extensions,
                                          2: reserved for future debugging,
                                          3: enable file level trace. Not recommended on multiple files

OPTIONS:
    -c, --columns <columns>                Sets a strict column width of the output, only available for terminal output.
    -e, --exclude <exclude>...             Ignore all files & directories matching the pattern.
    -i, --input <file_input>               Gives statistics from a previous tokei run. Can be given a file path, or
                                           "stdin" to read from stdin.
    -n, --num-format <num_format_style>    Format of printed numbers, i.e. plain (1234, default), commas (1,234), dots
                                           (1.234), or underscores (1_234). Cannot be used with --output. [possible
                                           values: commas, dots, plain, underscores]
    -o, --output <output>                  Outputs Tokei in a specific format. Compile with additional features for more
                                           format support. [possible values: cbor, json, yaml]
    -s, --sort <sort>                      Sort languages based on column [possible values: files, lines, blanks, code,
                                           comments]
    -t, --type <types>                     Filters output by language type, seperated by a comma. i.e. -t=Rust,Markdown

ARGS:
    <input>...    The path(s) to the file or directory to be counted.

That’s it! you have successfully installed tokei on various operating systems and learnt how to you can use it. I trust the guide has been useful. Have fun with tokei!

More guides to check out:

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Post

Nextcloud is a free and open-source syncing and file sharing server. It is self-hosted and allows companies to have a […]

The aaPanel is a free and open-source control panel used to manage hosting services such as websites, databases, Docker containers, […]

Welcome to this guide on how to use SSH & SSH Keys on Debian 12/11/10. SSH stands for secure shell, […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.