Counting Lines In a Script


Ever curious how many lines of code you have just written for an entire project? This extremely small command line will tell you! We will be using the wc command line utility or Word Count.

Counting Lines In a Single File

We use the -l option to output only the line count.

$ wc -l file.php

      137 ./file.php

Counting Lines In An Entire Directory

We can recursively count lines for our entire project using a few utilities. First we use the find command which can be customized as you like, however in the example below it will look for any files within the current working directory. This is then piped to xargs which will create a subset of lists passed to wc -l.

$ find . -type f | xargs wc -l

This should display something similar to:

      ...

      17 ./mi_webshot/.svn/all-wcprops
      55 ./mi_webshot/.svn/entries
       1 ./mi_webshot/.svn/format
       3 ./mi_webshot/.svn/text-base/mi_webshot.info.svn-base
     119 ./mi_webshot/.svn/text-base/mi_webshot.module.svn-base
      11 ./mi_webshot/includes/.svn/all-wcprops
      40 ./mi_webshot/includes/.svn/entries
       1 ./mi_webshot/includes/.svn/format
     270 ./mi_webshot/includes/.svn/text-base/webshot.inc.svn-base
     270 ./mi_webshot/includes/webshot.inc
       3 ./mi_webshot/mi_webshot.info
     119 ./mi_webshot/mi_webshot.module
    9787 total

Retrieving Only The Total Line Count

Although the output above is informative and great, for our application we may only be interested in the total line count. To do this we pipe our previous output to grep which allows us to eliminate lines that do not match 'total', then pipe to awk in order to print the first column which is our total.

$ find . -type f | xargs wc -l | grep 'total' | awk '{print $1}'

      9787

Specific File Types

You may want to utilize the wildcard matching in and the -name option in order to count lines for specific files such as .php, .css, etc.

$ find . -type f -name '*.module' | xargs wc -l

The above will output similar to:

     427 ./mi.module
     813 ./mi_design/mi_design.module
      97 ./mi_import/mi_import.module
     119 ./mi_webshot/mi_webshot.module
    1456 total

or

$ find . -type f | xargs wc -l | egrep '\.(module|inc|php|install)$' | awk '{total+=$1} END {print "Lines " total}'

Portable Bash Function

This Bash function below will output line counts per extension, which can easily be extracted using grep and awk. Check back soon for a full script supporting parsing of comments etc.

#
# Count lines of code in a directory.
#
# [dir]
#
function lc {
  dir=${1:-.}
  awk=''

  # Extensions
  # TODO: use arg 2 for extensions
  exts=( module inc php install js css )
  exts_pattern=`echo ${exts[*]} | tr " " "|"`

  # AWK iteration
  for (( i = 0; i < ${#exts[*]}; i++ )); do
  awk=${awk}' /\.('${exts[$i]}')$/ { '${exts[$i]}'_total += $1 }'
  done

  # AWK print each total
  awk=${awk}' END {'
  for (( i = 0; i < ${#exts[*]}; i++ )); do
  awk=${awk}' printf "'${exts[$i]}' %d \\n", '${exts[$i]}'_total;'
  done

  # AWK print aggregated total
  awk=${awk}' total = '
  for (( i = 0; i < ${#exts[*]}; i++ )); do
  awk=${awk}${exts[$i]}'_total + '
  done
 
  # AWK close
  awk=${awk}'0; printf "total %d \\n", total; }'

  echo -ne $(find "$dir" -type f | egrep '\.('"$exts_pattern"')$' | xargs wc -l | awk "$awk")
}