Cleaning up your code: Find All Source Files with “TO-DO” comments in Linux Shell

Keeping track of your TO-DO‘s is not always easy!

Especially if your using mostly plaintext editors for coding your scripts and programs. It’s a good habit to clean up your source code every once in a while and do what you intended with the TODO remark in the first place. Most modern IDE’s have this TO-DO finding feature already integrated, however, sometimes you can work faster using plaintext editors (and also leave a big mess behind :-) ).

For the same messiness I often exert myself, I created this short script to help me clean up the source code, implement the “dummy functions” and not to forget what I need TO-DO!

Basic Syntax

./todofind [directory=.]

Example of Usage

todofind /home/invision/project/

Sample output:

/home/invision/project/model/CachedResource.php
/home/invision/project/system/lang.layer.php
/home/invision/project/system/template.php
/home/invision/project/system/deprecated/cache.deprecated.php
/home/invision/project/system/session.php
/home/invision/project/system/auxiliary.php
/home/invision/project/base/Project.php
/home/invision/project/install.cli.php
/home/invision/project/dataprovider/MySQLProvider.php
Total: 15 TO-DO(s) in 9 files

Download

You can view or download the script here:

https://invision-web.net/web/sources/todofind

Or obtain it via shell:

wget -qO- https://invision-web.net/web/sources/todofind > todofind
chmod +x todofind

todofind

#!/bin/bash
# search TODO comments in source code
while getopts "h?" opt; do
    case "$opt" in
    h|\?)
                echo "Script for enlisting all files" \
                "and lines with TODO keyword (for source code)"
        echo "Usage:"
        echo "$0"
        exit 0
        ;;
    esac
done

#####################


extensions="php html xml js css sh h c cpp java pl py";


temp=$(mktemp)
for ext in $extensions ;
do
        find $(pwd)/ -name "*.$ext" >> $temp 2> /dev/null;
done;

totalcount=0
filecount=0;

greptemp=$(mktemp)
while read file
do
        cat $file \
                | grep -swiE 'todo|to-do|to do' \
                > $greptemp 2> /dev/null; found=$?

        todocount=$(cat $greptemp | wc -l);
        totalcount=$((totalcount+todocount));
        if [ $found -eq 0 ] ; then
                filecount=$((filecount+1));
                echo $file
        fi
done < $temp ;

echo "Total: $totalcount TO-DO(s) in $filecount files";

rm $temp
rm $greptemp

Linux Shell / Bash Scripting, Tutorials | Posted on July 3, 2013 by .

About Kristijan Burnik

Kristijan Burnik is a Programmer and Web Developer specializing in Server-side and Client-side Technologies and Application Development on Linux Servers and Windows Desktop . Also has experience in Networking, Desktop application development as well as Android mobile application development . Experienced in Programming Languages like Java, C, C++, C#, PHP, Javascript, MySQL and somewhat in other languages like Bash, Perl & Python. Sometimes he works as a Graphical Designer for digital production as well as for printing and advertising. He dedicates his spare time writing Tech Articles on his blog in order to share his work with others, as well as to document his projects for his own use. He's also an Educator & Mentor in field of Algorithms and Programming to young programmers in Zagreb, Croatia.

Leave a Reply