Introduction

cloc stands for Count Lines of Code, and that is what it does, it counts lines, but not just like wc may do, cloc makes that and some more.

You can count lines, blank lines, comments lines, and code lines, cloc will give you a summary of the files.

cloc works on files and on folders, when on folders, it will give you a nice summary of all files in that folder, you can also use it among other things to strip out the comments and blank lines.

Installation

Chances are that you can find it on your package manager tool, I have found it in AUR repositories of Arch Linux, anyway being it just a perl script, you can actually download it from the cloc.pl home page

Once you have it on your system, you only need to have perl also installed there to use it.

Using cloc

Let’s see some examples about how to use cloc.

Given this file: how-to-copy-file-permissions.html

<h2>Introduction</h2>

<p>You may know how to change file permissions using <code>chmod</code> but, if you already have a file with the permissions you want to assign to some other files, you can use <code>--reference</code> option in <code>chmod</code></p>

<h2>How to copy or replicate file permissions, using another file as reference</h2>

<p>If you want to use a file as reference to copy its permissions to other files you can do that using this command</p>

<pre><code>chmod --reference &lt;reference-file&gt; &lt;target-file&gt;
</code></pre>

<p>As an example, if you have this:</p>

<pre>
-rwxrwxrwx 1 ggarron ggarron 0 Dec 20 15:35 1.txt
-rwx------ 1 ggarron ggarron 0 Dec 20 15:35 2.txt
</pre>

<p>And you run:</p>

<pre><code>chmod --reference 1.txt 2.txt
</code></pre>

<p>You should then have this:</p>

<pre>
-rwxrwxrwx 1 ggarron ggarron 0 Dec 20 15:35 1.txt
-rwxrwxrwx 1 ggarron ggarron 0 Dec 20 15:35 2.txt
</pre>

<p>Just be sure, you are the owner of the files, you that you have permission to change them.</p>

If you run:

cloc how-to-copy-file-permissions.html

This is the output

       1 text file.
       1 unique file.                              
       0 files ignored.

http://cloc.sourceforge.net v 1.53  T=0.5 s (2.0 files/s, 62.0 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
HTML                             1             11              0             20
-------------------------------------------------------------------------------

Now if we run:

cloc how-to-copy-file-permissions.html  --strip-comments=bak

We’ll have this:

<h2>Introduction</h2>
<p>You may know how to change file permissions using <code>chmod</code> but, if you already have a file with the permissions you want to assign to some other files, you can use <code>--reference</code> option in <code>chmod</code></p>
<h2>How to copy or replicate file permissions, using another file as reference</h2>
<p>If you want to use a file as reference to copy its permissions to other files you can do that using this command</p>
<pre><code>chmod --reference &lt;reference-file&gt; &lt;target-file&gt;
</code></pre>
<p>As an example, if you have this:</p>
<pre>
-rwxrwxrwx 1 ggarron ggarron 0 Dec 20 15:35 1.txt
-rwx------ 1 ggarron ggarron 0 Dec 20 15:35 2.txt
</pre>
<p>And you run:</p>
<pre><code>chmod --reference 1.txt 2.txt
</code></pre>
<p>You should then have this:</p>
<pre>
-rwxrwxrwx 1 ggarron ggarron 0 Dec 20 15:35 1.txt
-rwxrwxrwx 1 ggarron ggarron 0 Dec 20 15:35 2.txt
</pre>
<p>Just be sure, you are the owner of the files, you that you have permission to change them.</p>

Conclusion

As you can see, it may be useful for programmers, there are a lot of other options, be sure to run:

cloc

With no parameters, to see all available options.