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
#!/bin/ksh export SCPT_LST_FILE=$HOME/tmp_lst_of_scpts if [ -f $SCPT_LST_FILE]; then rm -f $SCPT_LST_FILE; fi touch $SCPT_LST_FILE
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 do
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 DIR=$LINE continue fi if [ ! -z $DIR ]; then if [ ! -z $(echo $LINE | grep "sh$") ]; then echo $(echo $DIR | cut -d':' -f1)/$LINE >> $SCPT_LST_FILE fi fi done
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
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 😉