#!/bin/sh
##############################################################################################
#   2010-08-01  Author:  pradeepkumarprajapati3@yahoo.co.in / info@expertsguide.info 
#
#   This shell script searches for files named "error_log" under a home dir and mails them.
#   It searches for bulky files also (file size > 500KB).  
#   
#   More such scripts at http://www.expertsguide.info
##############################################################################################

date '+DATE: %d/%m/%y TIME:%H:%M:%S'
echo "This is from cron job on http://www.expertsguide.info/, to show the error_log files under various folders"
#echo $USER
#echo $SHELL
myfile=all_error_log_files.log
#echo $myfile

if test ! -f $myfile ; then 
  echo "file $myfile not exists or not simple text file, creating it ..."
  touch $myfile
  chmod 755 $myfile
else
  echo "File exists, emptying it ..."
  cp /dev/null $myfile
  #cat /dev/null >$myfile  
  #echo -n '' > $myfile
  echo -n "Debug: File size after emptying it is "
  ls -l $myfile | awk '{print $5}'
fi

if test ! -w $myfile ; then 
  echo "File $myfile is not writable file, exiting ..."
  exit 1
fi 


find . -iname "error_log" |grep 'error_log' | xargs ls -l>>$myfile              # extra grep error_log added to handle find failure cases
#find . -size +500k -type f -name "*.*"| xargs ls -l>>$myfile                   # file size > 500 KB
#find . -size +500k -type f -name "*.*" | sed -n 's/.*/\"&"/p' #| xargs ls -l
find . -size +500k -type f -name "*.*" | sed 's/.*/"&"/' | xargs ls -l>>$myfile #work around for filenames having spaces in them 
  
if [ -s $myfile ]  
  then
  mail -s "error_log and bulky files" info@expertsguide.info -c userid@yahoo.co.in<$myfile
else 
  echo "No error_log or bulky files found, input file size is zero"
fi

echo "Completed search for error_log and bulky files"
exit 0
