What is the way to count total number of lines in an Xcode Project? I can see number of lines in an individual file but I need a sum up of all the lines in a project.
5 Answers
A lightweight solution if you're using Homebrew (and a fan of the terminal) is the command-line program 'Cloc' (count lines of code). It breaks down the output for languages used in your project and gives you other useful information.
$ brew install cloc
$ cd path/to/project/
$ cloc .

- 46,496
- 21
- 150
- 195

- 8,932
- 4
- 43
- 64
-
1`$ brew install cloc` `$ cd path/to/project/` `$ cloc .` – blwinters Sep 20 '16 at 14:38
-
11Make sure to exclude Pods if you use Cocoapods! `$ cloc . --exclude-dir=Pods` – Mike Sprague Mar 14 '18 at 10:15
-
what about swift packages? How do you skip those? – zumzum Jan 07 '22 at 03:23
-
2Swift Packages are cloned into a separate location - by default, ~/Library/Caches/org.swift.spm. – Tim Jan 07 '22 at 13:21
If you don't want to pay $4.99 for a one time use, and you don't want to bother with HomeBrew. While it does count the empty lines between your code, you can do this:
- Open Terminal
- cd to your Xcode project
- Execute the following when inside your target project:
find . -name "*.swift" -print0 | xargs -0 wc -l
If you want to exclude pods:
find . -path ./Pods -prune -o -name "*.swift" -print0 ! -name "/Pods" | xargs -0 wc -l
If your project has objective c and swift:
find . -type d \( -path ./Pods -o -path ./Vendor \) -prune -o \( -iname \*.m -o -iname \*.mm -o -iname \*.h -o -iname \*.swift \) -print0 | xargs -0 wc -l

- 12,189
- 5
- 77
- 85
-
-
@BartłomiejSemańczyk, not sure what you mean, can you paste your terminal output? Or any other output? – ScottyBlades Mar 23 '21 at 02:50
Check out: CLOC
ClOC counts blank lines, comment lines, and physical lines of source code.
To use CLOC (Count Lines Of Code) for count number of lines in a project. Download the the CLOC .pl file and write following line in terminal:
perl ./DirectoryWhereClockFileIS/cloc-1.56.pl ./YourDirectoryWhereYourSourcesAre
It will show you results like:

- 4,176
- 4
- 39
- 46
There is an app on the App Store called Xcode Statistics. (Or something like that). It does what you want.
A word of warning though. The number of lines in a project has little to no relation to the quality or complexity of that project.

- 7,502
- 6
- 51
- 82

- 76,236
- 42
- 207
- 306
-
2Thanks for that. And on your second point, I know I know. It is just for the fun of it ;) – Feb 27 '15 at 11:56
-
1
-
2Somebody should invent a way to point to another resource without having to relate on the correct search term. /snark https://itunes.apple.com/us/app/project-statistics-for-xcode/id650764572?l=en&mt=12 – Matthias Bauch Feb 27 '15 at 12:17
-
@MatthiasBauch lol! I was on my phone at the time so wasn't able to find the app on the Mac App Store. Lol – Fogmeister Feb 27 '15 at 12:58
-
Comments on App Store indicate no Swift support. Last version update was 5 years ago. – Darrell Root Nov 27 '19 at 23:34
-
@DarrellRoot you’re commenting on a question and answer that is 5 years old... ♂️ – Fogmeister Nov 27 '19 at 23:54
-
2Yes. Just had this problem and upvoted the answer in 4th place as most helpful. Did not downvote anyone. Just commented. We are building a library of questions and answers, but the rapid recent development of Swift hurts answer relevance. Might be a topic for meta. – Darrell Root Nov 28 '19 at 00:02
-
1
You can use this script to do that :)
#!/usr/bin/python
"""
Count number of lines of code in a projects
directory.
Credits to https://github.com/0RaMsY0
"""
import os
import argparse
def count_lines(path: str, code_extension: str) -> int:
FOLDERS = []
FILES = []
PATHS = [path]
TOTAL_LINES = 0
STOP_LOOP = False
while STOP_LOOP != True:
for i in PATHS:
for _ in os.listdir(i):
if _.endswith(f".{code_extension}"):
FILES.append(f"{i}/{_}")
else:
if os.path.isdir(f"{i}/{_}"):
PATHS.append(f"{i}/{_}")
STOP_LOOP = True
for FILE in FILES:
TOTAL_LINES += len(open(FILE, "r").readlines())
return TOTAL_LINES
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--path", help="Path to count code numbers")
parser.add_argument("-ex", "--extension", help="Code extenstion e.g py js ...")
args = parser.parse_args()
PATH = args.path
CODE_EXTENSION = args.extension
TOTAL_LINES = count_lines(PATH, code_extension=CODE_EXTENSION)
print(TOTAL_LINES)
Usage:
you need to pass in two flags one for your project's path and the other is the extension of the source code for example it can be py for python, js for javascript etc...
python countlines.py -p "/path/to/project" -ex py
Warning: Do not add a '.' to the start of the extension or it will not work

- 21
- 3