195

Is there a way to determine how many lines of code an Xcode project contains? I promise not to use such information for managerial measurement or employee benchmarking purposes. ;)

Dave
  • 12,408
  • 12
  • 64
  • 67
  • If you want lines, then you can use this answer: http://stackoverflow.com/questions/5901758/xcode-4-see-the-number-of-the-line-of-code-i-am-on But it includes spaces – GeneCode Feb 27 '14 at 09:38
  • Does this answer your question? [Count total number of lines in an Xcode project](https://stackoverflow.com/questions/28764408/count-total-number-of-lines-in-an-xcode-project) – ScottyBlades Jun 27 '21 at 20:18

15 Answers15

270

I see this floating around and use it myself:

find . "(" -name "*.m" -or -name "*.mm" -or -name "*.cpp" -or -name "*.swift" ")" -print0 | xargs -0 wc -l
Eddy Borja
  • 1,638
  • 17
  • 21
Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • 2
    To handle subdirs with spaces in the names you need: find . "(" -name "*.m" -or -name "*.mm" -or -name "*.cpp" ")" -print0 | xargs -0 wc -l – Matt__C Apr 04 '12 at 18:26
  • 6
    it appears the .m and .mm tests are missing an * (edited: seems SO is not rendering those without a preceding slash) The above was not working for me until I added them as such: find . "(" -name "\*.m" -or -name "\*.mm" -or -name "\*.cpp" ")" -print0 | xargs -0 wc -l – bladnman Feb 18 '13 at 16:46
  • How can I get total number of lines excluding commented and blank lines ? – Vineesh TP Oct 29 '14 at 11:10
  • 2
    Note for Swift users - replace or add another 'or' with the file extension .swift – Byron Coetsee Dec 27 '14 at 13:27
149

Check out CLOC.

cloc counts blank lines, comment lines, and physical lines of source code in many programming languages.

(Legacy builds are archived on SourceForge.)

Community
  • 1
  • 1
Nathan Kinsinger
  • 23,641
  • 2
  • 30
  • 19
  • 14
    CLOC is available via Homebrew. The ease of use in the command line was refreshing. – avelis Apr 24 '13 at 00:26
  • 1
    I love the pun in the name there -- yes you can tell how productive you've been by checking out your CLOC. – bobobobo May 15 '13 at 02:41
  • Use above link to download './cloc-1.56.pl' perm version of cloc tool. Make sure that you enable execution permission on cloc-1.56.pl file using 'chmod u+x cloc-1.56.pl' command. If your source code is located in directory 'project_code' You just need to run following command. – JZ. Sep 26 '14 at 03:41
85

I have been using CLOC as mentioned by Nathan Kinsinger and it is fairly easy to use. It is a PERL script that you can add and run from your project directory.

PERL is already part of Mac OS and you can invoke the script this way to find out your number of lines you have written:

perl cloc-1.56.pl ./YourDirectoryWhereYourSourcesAre

This is an example of output i got from such command:

   176 text files.
   176 unique files.                                          
     4 files ignored.

http://cloc.sourceforge.net v 1.56  T=2.0 s (86.0 files/s, 10838.0 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Objective C                     80           3848           1876          11844
C/C++ Header                    92            980           1716           1412
-------------------------------------------------------------------------------
SUM:                           172           4828           3592          13256
-------------------------------------------------------------------------------
Stunner
  • 12,025
  • 12
  • 86
  • 145
tiguero
  • 11,477
  • 5
  • 43
  • 61
  • 4
    Nice! I like this better than my own answer. :-) – Joshua Nozzi Jan 27 '15 at 21:56
  • 1
    You may need to download the script first. Then navigate to the directory where that script is and run that command with whatever file name you downloaded. – prolfe Mar 27 '15 at 20:30
  • Should be cloc perl-1.56.pl ./YourDirectoryWhereYourSourcesAre and not as above, if you install perl from scratch make sure you enter the current perl version (mine was 5.30.1 so basically cloc perl-5.30.1.pl ./) – Golan Shay Jan 06 '20 at 16:57
  • 1
    One last thing, I had to run span Moo as per error that appeared when running it for the 1st time so checkout for that one as well – Golan Shay Jan 06 '20 at 16:58
73

Open up Terminal.app, go into your project's root directory, and run this command:

For Swift only:

find . \( -iname \*.swift \) -exec wc -l '{}' \+

For Obj-C only:

find . \( -iname \*.m -o -iname \*.mm -o -iname \*.h \) -exec wc -l '{}' \+

For Obj-C + Swift:

find . \( -iname \*.m -o -iname \*.mm -o -iname \*.h -o -iname \*.swift \) -exec wc -l '{}' \+

For Obj-C + Swift + C + C++:

find . \( -iname \*.m -o -iname \*.mm -o -iname \*.c -o -iname \*.cc -o -iname \*.h -o -iname \*.hh -o -iname \*.hpp -o -iname \*.cpp -o -iname \*.swift \) -exec wc -l '{}' \+

Terminal quick tips:
ls: list directory contents
cd: change directory
Press tab to autocomplete
Remember to put "\" backslash before spaces
I suggest going one folder down from the main project so you get rid of code count from the frameworks

Esqarrouth
  • 38,543
  • 21
  • 161
  • 168
45

In terminal, change into the project directory and run:

find . -type f -print0 | xargs -0 cat | wc -l

If you want only certain file types, try something like

find . -type f -name \*.[ch]* -print0 | xargs -0 cat | wc -l
Andrew McGregor
  • 31,730
  • 2
  • 29
  • 28
  • Nice, but includes comments and blank lines and thus is not exactly what was asked for. A LOC measure should be as independent of code formatting style as possible and thus a bunch of blank lines or comments between to logical parts in a file shouldn't count towards the sum. – bassim Aug 28 '14 at 12:41
39
  1. open terminal
  2. navigate to your project
  3. execute following command inside your project:

    find . -path ./Pods -prune -o -name "*.swift" -print0 ! -name "/Pods" | xargs -0 wc -l
    

    Or:

    find . -path ./Pods -prune -o -name "*[hm]" -print0 ! -name "/Pods" | xargs -0 wc -l
    

(*Excluding pod files count from total count)

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
Shan Shafiq
  • 1,018
  • 10
  • 10
34

Check out Xcode Statistician, it does exactly what you want. It also provides other interesting statistics so is worth a run for fun now and then.

Note that it will not look inside real folders, though it will look in groups. Odds are you aren't using real folders so it'll work great. If you are using folders then you just have to do the count in each folder and add them together.

Note: As of June, 2012, it seems this does not work properly with the latest versions of Xcode.

Maarten
  • 351
  • 1
  • 13
Matthew Frederick
  • 22,245
  • 10
  • 71
  • 97
  • Just a heads up for those who mix Objective-C and C/C++: It doesn't count *.c or *.cpp files. – Emile Cormier Feb 17 '12 at 00:30
  • 3
    Note that Xcode Statisician doesn't deal with subdirs in your project at this point. – Matt__C Apr 04 '12 at 18:27
  • @Matt__C Aye, hence "Note that it will not look inside real folders". Quite a bit more manual work, unfortunately (and recursive folder searching is easy to code), but it's workable. – Matthew Frederick Apr 05 '12 at 20:54
  • 1
    Seems it doesn't work at all with X-Code 4.2 projects on Lion (at least it didn't give any statistics for my project) – BadPirate May 31 '12 at 00:54
  • Ah, bummer, it may be outdated now. – Matthew Frederick May 31 '12 at 18:20
  • It still works as of xcode 4.5.1. It just has a bug where it doesn't count lines in subfolders. If you navigate into each folder, it will tell you the number of lines in that folder. A bit tedious to add them up, but you probably won't have more than a dozen or so folders anyway (hopefully) – Robert Wagstaff Oct 10 '12 at 05:23
  • Sorry, but this program fails to recurse down sub-directories, and is also not code-signed by any identified developer - so may be malware. So for most reasonably sized projects, it is unusable – Motti Shneor May 30 '18 at 17:52
  • @MottiShneor This answer was from 8 years ago. Things have changed a great deal since then. In fact, the edited answer (made 3 years ago) specifically notes that it no longer works. – Matthew Frederick May 31 '18 at 20:37
16

If you go to your project's directory in terminal and enter:

find . "(" -name "*.h" -or -name "*.m" -or -name "*.mm" -or -name "*.hpp" -or -name "*.cpp"  -or  -name "*.c" -or -name "*.cc" -or -name "*.swift" ")" -print0 | xargs -0 wc -l

That will give you a project breakdown, as well as the line total for each file and the project as a whole.

RileyE
  • 10,874
  • 13
  • 63
  • 106
  • For swift just removed everything and added .swift `find . "(" -name "*.swift" ")" -print0 | xargs -0 wc -l` – WCByrne Jan 21 '15 at 23:51
  • @WCByrne Alternatively, we can just add .swift to the original command – RileyE Jan 22 '15 at 17:59
  • Indeed. Since most libraries and borrowed code are still in .h & .m it helped to trim the count down to just what I had written. – WCByrne Jan 22 '15 at 18:40
  • 1
    Absolutely. Everyone has their own solution. And if they have their own prefix (eg: "RE"), they could even do `find . "(" -name "RE*.swift" ")" -print0 | xargs -0 wc -l` – RileyE Jan 22 '15 at 19:52
14

Steps to implement CLOC library in Mac as below:

  1. Open Terminal.
  2. Install Homebrew by copying and pasting the below command in the Terminal (including double quotes).

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Enter system password if asked.

You will see the terminal screen as below.

Installing Homebrew

System will popup so many permissions, allow all the permissions

If everything goes fine, you will see terminal screen as below,

Homebrew installation successful

  1. Now its time to install CLOC using below command.

brew install cloc

  1. Navigate to the project directory and run either of the following commands.

cloc . --exclude-dir=Pods (to exclude pod files)

cloc . (including pod files)

If everything goes fine, it will display the number of lines of code as below,

CLOC Result

Isuru
  • 30,617
  • 60
  • 187
  • 303
Arshad Shaik
  • 1,095
  • 12
  • 18
9

Nozzi's version doesn't work for me, but this one:

find . -type f -print0 | xargs -0 cat | wc -l
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Pascalius
  • 14,024
  • 4
  • 40
  • 38
  • 2
    short and concise - but it counts lines also on non-source-code files. So actually the filename-extension filtering is crucial here. The OP wanted to count "source code lines". not any text lines. – Motti Shneor May 30 '18 at 17:43
7

A quick & easy way:

Use a regex search (Find Navigator, choose Find > Regular Expression).

.\n

Works conveniently with Xcode search scopes and you can easily customize it to whatever type of line you'd like to count ;).

Patrick Pijnappel
  • 7,317
  • 3
  • 39
  • 39
  • Now that's a nice one! not the fastest, but without leaving the dev environment. I like it. Probably a better regular expression can avoid counting white-space lines and things like that. – Motti Shneor May 30 '18 at 17:55
  • Keep it simple. I like it. – devios1 Mar 19 '20 at 18:07
6

You can install SLOCCount through MacPorts. Or, more crudely, you can use wc -l.

anthony
  • 40,424
  • 5
  • 55
  • 128
6

I am not familiar with xcode, but if all you need is to count the number of lines from all those specific files within a directory tree, you may use the following command:

find .... match of all those files ... -exec wc -l {} +

Following Joshua Nozzi's answer, in GNU find the regular expression for such files would be like:

find . "(" -name "*.m" -or -name "*.mm" -or -name "*.cpp" -or -name "*.swift" ")" -exec wc -l {} +

or even

find -regex ".*\.\(m\|mm\|cpp\|swift\)$" -exec wc -l {} +

this uses a regular expression to match all files ending in either .m, .mm, .cpp or .swift. You can see more information about those expressions in How to use regex in file find.

If you are working with Mac OS find, then you need a slightly different approach, as explained by Motti Shneor in comments:

find -E . -regex ".*\.([hmc]|mm|cp+|swift|pch)$" -exec wc -l {} +

Both will provide an output on the form of:

234 ./file1
456 ./file2
690 total

So you can either keep it like this or just pipe to tail -1 (that is, find ... | tail -1) so that you just get the last line being the total.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • well --- the find -regex ".*\. etc doesn't work for me on MacOS - it claims an illegal option --r what kind of find are you using? – Motti Shneor May 29 '18 at 19:22
  • @MottiShneor you probably want to check [this question](https://stackoverflow.com/q/5905493/1983854) where they talk about `\|` and others in MacOS. `--regex` is valid in both GNU find (the one I use) and MacOS. – fedorqui May 30 '18 at 06:31
  • well --- no. I found out in MacOS you must find -E for extended regular expressions. Do copy the suggested command above onto whatever OS-X you can get, and see for yourself. man find on MacOS-X tells you you must -E for that. – Motti Shneor May 30 '18 at 17:39
  • Finally, you must also provide a path where to search on OSX. so the variation that does work, looks like this: find -E . -regex ".*\.([hmc]|mm|cp+|swift|pch)$" -exec wc -l {} + – Motti Shneor May 30 '18 at 17:40
  • @MottiShneor thanks for providing the approach in MacOS. I have updated the answer with that. – fedorqui Jun 01 '18 at 06:41
4

line-counter is a good alternative. It's lighter than CLOC and much more powerful and easier to use than other commands.

A quick overview

This is how you get the tool

$ pip install line-counter

Use line command to get the file count and line count under current directory (recursively)

$ line
Search in /Users/Morgan/Documents/Example/
file count: 4
line count: 839

If you want more detail, just use line -d.

$ line -d
Search in /Users/Morgan/Documents/Example/
Dir A/file C.c                                             72
Dir A/file D.py                                           268
file A.py                                                 467
file B.c                                                   32
file count: 4
line count: 839

And the best part of this tool is, you can add .gitignore like configure file to it. You can set up rules to select or ignore what kind of files to count just like what you do in '.gitignore'. Yes, this tool is just invented to make knowing how many lines I have easier.

More description and usage is here: https://github.com/MorganZhang100/line-counter

I'm the author of this simple tool. Hope it can help somebody.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Morgan Zhang
  • 93
  • 1
  • 4
1

Easy way - 100% working Tested

When I’m working on a swift project, I sometime’s get the urge to know how many lines of code I have actually written. It would be interesting to see. However, I’m not just going to sit there counting every single line, we’re programmers after all, we can do better!

Using Terminal Open up terminal and navigate to your project:

cd path/to/your/project/

Next, type in one of the following lines of code to get the total number of lines in your project. It works by reading all the swift files (sorry objc users!) in your directory and counting up the lines.

Not Including Pods

find . -path ./Pods -prune -o -name '*.swift' -print0 ! -name '/Pods' | xargs -0 wc -l

Including Pods


find . -name '*.swift' -print0 | xargs -0 wc -l

Using Cloc If you have homebrew on your mac, there is a much easier way to show the number of lines. Navigate to your project using terminal:

cd path/to/your/project/

Next, install Cloc

brew install cloc

Finally, type in one of the following commands

Not Including Pods

cloc . --exclude-dir=Pods

Including Pods

cloc .

Results:

enter image description here

Mr.Javed Multani
  • 12,549
  • 4
  • 53
  • 52