MW

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

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

continue reading...