Terminal – calculate number of lines of code in a directory

We had an interesting question, can we calculate how many lines of code we have written for an entire project? It turns out this isnt the easiest thing to calculate for a web-project, but we gave it a go. This is the best we have come up with so far for all code we have written to calculate the number of lines of code in all PHP, CSS, JS, HTML and HTM pages.

( find ./ -name '*.php' -print0 -o -name '*.css' -print0 -o -name '*.js' -print0 -o -name '*.html' -print0 -o -name '*.htm' -print0 | xargs -0 cat ) | wc -l

The answer for our particular project was 1500784 lines of code!

If you wanted to do just PHP pages its rather easier:

( find ./ -name '*.php' -print0 | xargs -0 cat ) | wc -l

Ubuntu – count all files in a directory recursively

Ubuntu – count all files in a folder recursively.

Took a while to figure out how to do this on a single command, but so very useful to check if all files have copied successfully.

find . -type f | wc -l

How this works:
find . -type f finds all files ( -type f ) in this ( . ) directory and shows everything as one file on each line in a list.

The second command comes after the pipeĀ | into wc (word count) the -l option tells wc to only count lines of its input.

Together they count all files in the folder you are in and all sub-folders.