taT4Nix | Pattern-based Files Searching..

For the past few months, I’ve got into the habit of creating utilities to make my work easy at office. However, it has become little difficult to track so many scripts I’ve created over short period of time. So, I thought of creating another utility to list the scripts (present under my user directory on Linux system, I interact with using Putty) and mail me with the contents of the list (as body of the mail) automatically.

Automating File Search


export SCPT_LST_FILE=$HOME/tmp_lst_of_scpts
if [ -f $SCPT_LST_FILE]; then rm -f $SCPT_LST_FILE; fi

The following series of commands separated by pipeline, will filter out the directories and scripts, from recursive listing of the $HOME directory. Then, the results will be looped for further processing.

ls -1R $HOME | grep -e :$ -e "\.sh$" | while read LINE

The results generated from the above command may have none/one/several directories having no match found. So, in that case we need to filter out such directories and show the complete path of the scripts, for which the match has been found.

    if [ ! -z $(echo $LINE | grep :$) ]; then
    if [ ! -z $DIR ]; then
       if [ ! -z $(echo $LINE | grep "sh$") ]; then
          echo $(echo $DIR | cut -d':' -f1)/$LINE >> $SCPT_LST_FILE

The following code will send the content of the file created above as the body of the mail automatically to my email id, exported in “.profile” as MAIL_ME.

cat $SCPT_LST_FILE | mailx -s "Scripts List --- $(date +%Y%m%d)"  $MAIL_ME

Moving Ahead

You may actually generalize it to search for some PATTERN passed as an argument to the script. Go ahead and explore more possibilities with this utility, as this is just a head start 😉


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s