Table of Contents
Back to Blog

Article

How to Count Lines in a File (Linux/Mac/Windows)

Compare the fastest ways to count lines in a file on Linux, macOS, and Windows using built-in tools and scripts.

Published: March 20, 2026Updated: March 20, 20262 min readAuthor: Line Counter Editorial Team

When someone asks how to count lines in a file, the first question should be: on which operating system, and how large is the file? The fastest solution for a tiny config file is not always the best choice for a multi-gigabyte log. This guide covers the practical options on Linux, macOS, and Windows.

Linux and macOS: use wc -l

The standard Unix answer is:

wc -l myfile.txt

This is fast, widely available, and simple to remember. It counts line breaks, so if your file does not end with a trailing newline the visible line count can differ from what some editors display. That behavior is usually correct for shell workflows but it is worth understanding.

If you want just the number without the filename:

wc -l < myfile.txt

This is useful when the output will be piped into another command or stored in a variable.

Count lines across multiple files

For a directory of source files, combine find and wc:

find . -type f -name "*.py" -print0 | xargs -0 wc -l

That prints counts for each matching file plus a total. On macOS and Linux, null-separated arguments help when filenames contain spaces.

Exclude generated folders

Project-wide line counts get distorted quickly if you include build output, dependencies, and caches. A more realistic command looks like this:

find . \
  -type d \( -name node_modules -o -name dist -o -name .git \) -prune \
  -o -type f -name "*.ts" -print0 | xargs -0 wc -l

This matters because SEO tools, SDK clients, and generated assets can otherwise dwarf the code your team actually maintains.

Windows PowerShell

On Windows, PowerShell is the default answer:

(Get-Content .\myfile.txt).Count

This is fine for smaller files, but Get-Content reads the file into memory. For very large files, a .NET call is usually more efficient:

[System.IO.File]::ReadLines("C:\logs\app.log").Count

That streams the file instead of materializing the entire contents first.

Count non-empty lines

Sometimes you need the number of meaningful lines rather than the number of raw line breaks. On Unix-like systems:

grep -cve '^[[:space:]]*$' myfile.txt

And in PowerShell:

(Get-Content .\myfile.txt | Where-Object { $_.Trim() -ne "" }).Count

These patterns ignore empty and whitespace-only lines, which is usually what people mean when they count content lines in prose or configuration files.

Count lines in code projects

Once you move from a single file to a whole repository, raw line counts are rarely enough. You often want:

  1. Total lines across selected extensions.
  2. Non-empty lines.
  3. Comment-aware line counts.
  4. Folder exclusions.

At that point, a dedicated line counter or a small reusable script is often easier to maintain than a pile of shell one-liners. The command line is excellent for quick local checks, but a browser-based tool or a shared script is better for repeatable reports that teammates can review together.

Common pitfalls

Three problems show up repeatedly:

  1. Counting dependency folders by accident.
  2. Confusing visible editor lines with newline-delimited file lines.
  3. Loading very large files into memory when a streaming approach would be safer.

If you avoid those mistakes, line counting becomes a straightforward operational task rather than a source of off-by-one surprises.

Frequently Asked Questions

What command counts lines on Linux and macOS?

The classic answer is wc -l filename, which prints the number of newline-terminated lines in the file.

How do I count lines on Windows?

Use PowerShell with Get-Content or [System.IO.File] helpers, depending on the file size and performance you need.

Related Guides