How to Count Lines of Code on Mac

Photo of Simon Simon • • Guide • 6 min read

Sometimes you just want to know how large a project has become. Maybe you are curious about the mix of languages, want to compare two projects, or are checking how much code is hiding inside an old repository.

Counting lines sounds simple, but the result depends on what you count. A basic command can count every line in every file, including comments, blank lines, generated code, and dependencies. A more useful result separates those categories and focuses on the source files you actually wrote.

This guide covers three ways to count lines of code on a Mac: a built-in Terminal command, the cloc command-line tool, and DevKnife’s visual Code Statistics tool.

What Counts as a Line of Code?

There is no single universal line count for a project. Source-aware counting tools often divide files into a few categories:

  • Code: Lines containing source code or data.
  • Comments: Lines containing only comments.
  • Blank: Empty or whitespace-only lines.
  • Total: All of those categories combined.

A physical line count is useful for getting a rough idea of project size, but it is not a measure of code quality or developer productivity. Ten clear lines can be better than one clever line, and the same feature may take very different amounts of code in Swift, Python, or C++.

The scope matters too. If the scan includes node_modules, build output, generated files, or vendored libraries, the total can say more about your dependencies than your own project.

Option 1: Count Lines with wc

macOS includes the wc command, which can count lines, words, and characters in a file.

To count the lines in one Swift file, open Terminal and run:

Terminal window
wc -l ContentView.swift

The number on the left is the raw line count for the file. It includes code, comments, and blank lines. Technically, wc -l counts newline characters, so a final line without a newline may not be included.

You can combine find and wc to count several file types in a project. For example, this command counts Swift and Objective-C files in the current folder and its subfolders:

Terminal window
find . -type f \( -name '*.swift' -o -name '*.m' -o -name '*.mm' \) -print0 \
| xargs -0 wc -l

The final total shows the combined line count. Change the filename patterns when you need to count another language.

This method is already available on every Mac and works well for a quick check. Its limitation is that it does not understand the source code. It cannot separate code from comments or blank lines, group the result by language, or automatically avoid dependency and build folders.

Option 2: Count Code with cloc

cloc is a command-line tool made specifically for counting source code. It recognizes many programming languages and reports files, blank lines, comments, and code separately.

If you use Homebrew, install it with:

Terminal window
brew install cloc

Move to your project folder and scan it:

Terminal window
cd /path/to/project
cloc .

The result is grouped by language, followed by totals for the whole project.

For more control, you can exclude folders that should not be part of the count:

Terminal window
cloc --exclude-dir=node_modules,Pods,DerivedData,build,dist,vendor .

cloc is a good choice when you work in Terminal, want a detailed report, or need to repeat the count in a script. It is also flexible, but that means you should check the scan scope and exclusion options before comparing results between projects.

Option 3: Use DevKnife’s Code Statistics

DevKnife includes a Code Statistics tool for scanning a project without installing a command-line utility or building a long shell command.

To scan a project:

  1. Open Code Statistics in DevKnife.
  2. Drag a project folder into the window or click Choose Folder.
  3. Wait for the scan to finish.

The summary shows the number of files and the totals for code, comments, blank lines, and all lines. A table below groups those values by language, making it easier to see what the project is made of.

DevKnife Code Statistics showing file and line counts for the DevKnife project

DevKnife’s Code Statistics showing project totals and a breakdown by language.

DevKnife automatically skips common dependency and build folders such as .git, node_modules, Pods, DerivedData, build, dist, and vendor. It also ignores hidden files, symbolic links, binary files, common lock files, source maps, minified JavaScript and CSS, and files larger than 20 MB.

Recent project folders stay available in the Open Folder menu, and you can use Refresh after the code changes. The scan runs locally on your Mac. DevKnife does not upload your source files or the resulting statistics.

Read more about Code Statistics in the DevKnife documentation.

Why Different Tools Show Different Results

Do not be surprised if wc, cloc, and DevKnife return different totals for the same project.

The tools may:

  • recognize different file extensions and languages
  • include or exclude different folders
  • handle generated, minified, or binary files differently
  • classify comments and mixed code-and-comment lines differently
  • count the final newline in a file differently

This does not necessarily mean that one result is wrong. It usually means that the tools are measuring slightly different things. When comparing projects or tracking growth over time, use the same tool and the same exclusions each time.

Which Method Should You Use?

Use wc -l when you need a quick total for one file or a small set of known files. Use cloc when you want a scriptable command-line report with detailed options.

Use DevKnife when you want a readable overview without configuring a command, especially when you want to see the language breakdown and return to the same project later.

Conclusion

Counting lines of code can give you a useful overview of a project, as long as you understand what the number includes. A raw line count is quick, while a source-aware tool gives you a clearer split between code, comments, blank lines, and languages.

The built-in wc command is enough for simple checks, and cloc is a capable choice for Terminal workflows. For a visual result with common dependencies and build output filtered automatically, DevKnife’s Code Statistics brings the same information into a native Mac interface.

Tweet Share

Further Reading

·
Dev Log

DevKnife Update 1.3.0

DevKnife 1.3.0 adds Dark Mode, new Text Inspector and IndexNow tools, improved editor performance and other fixes.

Read More
·
Guide

How to Count Tokens for ChatGPT, Claude, and Gemini

Learn how token counting works for ChatGPT, Claude, and Gemini, why each model counts differently, and how to check prompt length with DevKnife's Text Inspector.

Read More
DevKnige logo

Ready to try DevKnife?

Fast, private, and built for macOS.

Made for Apple Silicon · macOS 14 · Just 10 MB