Script to find, grep and email


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to find, grep and email
# 1  
Old 09-11-2008
Script to find, grep and email

running a suse linux 10.

I'm trying to write a script that searches for a file with certain name and that has been modified less than 30 minutes ago, then search for a certain string in that file and email this string out. I have tried different combinations of find, grep and email but no luck so far.

Your help would be appreciated.
# 2  
Old 09-11-2008
Show us what you've tried and we'll help you correct it.
# 3  
Old 09-13-2008
I have tried the following:But i get the error command LINE 7, PRINT: command not valid

#!/bin/bash
#set -x

if [[ $# = "0" ]]
then

print "USAGE: $0 [Search_Directory]"
exit 0

elif [[ ! -d ${1} ]]
then

print "$0 : Error, must provide a directory path to search for files modified in last 30 minutes!"
exit 1

else

SRCHDIR=${1}

fi

FOUNDARRAY=( $(find ${SRCHDIR} -mmin -30|xargs) )

for I in ${FOUNDARRAY[*]}
do

STR="$(grep -i "[mysearch_string_here]" ${I})"

if [[ ! -z ${STR} && -f ${I} ]]
then

echo "${STR}" | mailx -s "File: ${I} has the string" [my email address here]

fi

done
# 4  
Old 09-13-2008
Use the find command.

I created a file called blablabla.txt, in it i placed the text.... "bla bla bla dan

With the command:
find . -cmin -30 -name "b*"

Only blablabla.txt is found.

$ find . -cmin -30 -name "b*"
./blablabla.txt

Add grep to find to search for the text... You want to use the -l command for grep so that the file name is printed instead of the text.

Search for file again, but look for the text dan in it.
$ find . -cmin -30 -name "b*" -exec grep -l "dan" '{}' \;
./blablabla.txt

Search for file again, but with text I know is not in the file.
$ find . -cmin -30 -name "b*" -exec grep -l "crap" '{}' \;
$

I used this web site for examples when I was putting all of that together. I knew what command to use, just not exactly how to do it.

Unix Find Tutorial


Find command has to be one of my favorite commands.

drool
# 5  
Old 09-13-2008
Let me take a stab at this

$ find . -cmin -30 -name "b*" -exec grep -l "dan" '{}' \;
./blablabla.txt


I should have qualified that to be:
find . -type f -name "b*" -cmin -30 -exec grep -l "dan" '{}' \;

That search's recursively in the current directory for files that have lower case 'b' at the beginning of their name, that have been changed in the last 30 minutes, and also has the text dan within the file..

Hope that helps.

drool
# 6  
Old 09-14-2008
Quote:
Originally Posted by basisvasis
I have tried the following:But i get the error command LINE 7, PRINT: command not valid
print is specific to Korn shell.
# 7  
Old 09-20-2008
After playing for another week, I came up with the following. This script works just fine for me except one small problem, i.e, even if there are no files that are less than 30 mins old, it still sends out an email with the blank body. I know i need a better if and then statement, but just dont have expertise to correct it. Please help out.

#!/bin/sh
# this is myscript.sh
newfile=`find /usr/mysearchpath/ -mmin -30`
grep 'searchstring*' $newfile>/usr/mysearchpathagain/resultsfile
if [ $# -eq 0 ]
then
mail -s 'found it' \ email address -t </usr/searchpath/resultsfile
fi
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find a file with a specific pattern for current sysdate & upon find email the details?

I need assistance with following requirement, I am new to Unix. I want to do the following task but stuck with file creation date(sysdate) Following is the requirement I need to create a script that will read the abc/xyz/klm folder and look for *.err files for that day’s date and then send an... (4 Replies)
Discussion started by: PreetArul
4 Replies

2. Shell Programming and Scripting

Script to find string & email

Hi I have a query that some of you may be able to help me with if poss? I'd appreciate it very much. I've got a few log files, that I would like to search for a string. When the string is found, i'd then like to email out the line/sting. If not found, i'd like to email out 'no found'... (2 Replies)
Discussion started by: horhif
2 Replies

3. Shell Programming and Scripting

Using Grep & find & while read line in a script

Hello people! I would like to create one script following this stage I have one directory with 100 files File001 File002 ... File100 (This is the format of content of the 100 files) 2012/03/10 12:56:50:221875936 1292800448912 12345 0x00 0x04 0 then I have one... (0 Replies)
Discussion started by: Abv_mx81
0 Replies

4. UNIX for Dummies Questions & Answers

grep command for email

May I know what is the command-line instruction to show all the subjects and authors of my emails contained in a directory by using egrep? Thanks! (2 Replies)
Discussion started by: marcuslki
2 Replies

5. Shell Programming and Scripting

How to use grep & find command to find references to a particular file

Hi all , I'm new to unix I have a checked project , there exists a file called xxx.config . now my task is to find all the files in the checked out project which references to this xxx.config file. how do i use grep or find command . (2 Replies)
Discussion started by: Gangam
2 Replies

6. Shell Programming and Scripting

Script to find and email selected files

I am trying to come up with a script that will search for selected files and then email them to me. For example, say I have a directory that has the following files: AA_doug.txt AA_andy.txt BB_john.txt APPLE_mike.txt GLOBE_ed.txt GLOBE_tony.txt TOTAL_carl.txt what is the best way to... (2 Replies)
Discussion started by: coach5779
2 Replies

7. Shell Programming and Scripting

Bash script (using find and grep)

I'm trying to make a simple search script but cannot get it right. The script should search for keywords inside files. Then return the file paths in a variable. (Each file path separated with \n). #!/bin/bash SEARCHQUERY="searchword1 searchword2 searchword3"; for WORD in $SEARCHQUERY do ... (6 Replies)
Discussion started by: limmer
6 Replies

8. Shell Programming and Scripting

Grep and Email

Hello All, I want to make a script which reads a files after every one hour and grep a pariticular word on it and if it finds it than email me on my email address. Please tell me how i start and if there is some example available then please tell me the link. One thing more that for the... (7 Replies)
Discussion started by: wakhan
7 Replies

9. UNIX for Dummies Questions & Answers

Speeding up a Shell Script (find, grep and a for loop)

Hi all, I'm having some trouble with a shell script that I have put together to search our web pages for links to PDFs. The first thing I did was: ls -R | grep .pdf > /tmp/dave_pdfs.outWhich generates a list of all of the PDFs on the server. For the sake of arguement, say it looks like... (8 Replies)
Discussion started by: Dave Stockdale
8 Replies
Login or Register to Ask a Question