Tag recursive

Recent Posts

Recursive Wordcount (March 25, 2009)

Yes, I am a Linux user and I really appreciate the freedom I get by using either an awesome desktop environment or the command line - or both!

The function I’m going to present you gives you a good overview of how many words, lines and bytes files in a given folder have. I’m speaking about wc.

The bash script I wrote applies wc to every web file in a folder and every sub folder. Web files are:

  • *.php
  • *.html / *.htm
  • *.tpl
  • *.sql
  • *.js
  • *.css
  • .htaccess
  • files without an extension (e.g. README)

Furthermore you can exclude folders by using the -e parameter. I needed that feature to exclude scripts which are not written by me (see below). But because of that I have had to commit a sin: using eval

Recursive wc

    # usage
    # -s = search path
    # -e = excluded paths
    # examples:
    # current folder:    ./
    # other folder:      ./ -s ../foobar/
    # exclude folder:    ./ -e "*/foobar/*"
    # exclude folder:    ./ -e "*/folder1/*" -e "*/folder2/*"
    # default params
    # read command line params
    while getopts  "s:e:" PARAM
        case "${PARAM}" in
                s)      SEARCH_PATH="$OPTARG";;
                e)      EXCLUDE=$EXCLUDE" -not -path \"$OPTARG\"";;
    if [ "$EXCLUDE" = "" ]
        find $SEARCH_PATH \
            -regextype posix-egrep \
            -type f \
            -regex ".*(\.(php|html?|tpl|css|sql|js))$" \
            -or -name ".htaccess" \
            | xargs wc
        # evil eval
        eval 'find $SEARCH_PATH \
                -regextype posix-egrep \
                -type f \
                \( -regex ".*(\.(php|html|tpl|css|sql|js))$" \
                -or -name ".htaccess" \)\
                '$EXCLUDE' \
                | xargs wc'

continue reading...