Shell script to monitor new file in a directory and mail the file content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to monitor new file in a directory and mail the file content
# 1  
Old 03-19-2013
Oracle Shell script to monitor new file in a directory and mail the file content

Hi

I am looking for a help in designing a bash script on linux which can do below:-

1) Look in a specific directory for any new files
2) Mail the content of the new file

Appreciate any help

Regards
Neha
# 2  
Old 03-19-2013
Use a for loop to read file names from directory, email them and move them to another directory once handled.

Here is something to start with:
Code:
#!/bin/bash

dir="/path_to_your_directory/"

for file in ${dir}/*
do
        mkdir -p "${dir}/HANDLED"
        [[ "$file" =~ HANDLED ]] && continue
        mailx -s "Subject" user@domain.com < "$file"
        mv "$file" "${dir}/HANDLED/"
done

This User Gave Thanks to Yoda For This Post:
# 3  
Old 03-19-2013
Thanks Yoda for the instant reply.
I have tested the script, it works as desired.

However is it possible, not to move txt files to new directory and identify new file based on some mechanism - like comparing the date stamp from last reported file or so.

Thanks
Neha
# 4  
Old 03-19-2013
Another option is to have a file with the list of files that you handled and check against it each time you run your script:
Code:
for file in ${dir}/*
do
        [[ "$file" =~ handled.dat ]] && continue
        if grep -w "$file" "${dir}/handled.dat" > /dev/null 2> /dev/null
        then
                continue
        fi
        mailx -s "Subject" user@domain.com < "$file"
        printf "%s\n" "$file" | tee -a "${dir}/handled.dat"
done

This User Gave Thanks to Yoda For This Post:
# 5  
Old 03-19-2013
How about linking the file instead of moving it?

Code:
dir="/path_to_your_directory/"

mkdir -p "${dir}/HANDLED" || exit 1
cd $dir || exit 1

for file in *
do
        [ "$FILE" = "HANDLED" ] && continue
        # Skip files which are already linked
        [ -h "HANDLED/$FILE" ] && continue

        mailx -s "Subject" user@domain.com < "$file"
        ln -s "../$FILE" HANDLED/$FILE
done

This User Gave Thanks to Corona688 For This Post:
# 6  
Old 03-19-2013
The best way to monitor filesystem events is incron (a cron-like utility for filesystem events). I have a script that kicks off when something is written to the anonymous ftp jail (using the write-close flag if I recall correctly) to move the files elsewhere. I also have one that handles web page input without using CGI and the security risks that come with it.

inotify - get your file system supervised

Mike
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Monitor a file and send mail

I want to monitor the maillog file in real time and send a mail when a certain grep condition is met. Every time the grep condition is met a mail will be sent. I wish to ignore all grep conditions 30 mins after each mail and thereafter continue monitoring. For example: Condition is met, mail... (1 Reply)
Discussion started by: proactiveaditya
1 Replies

2. Shell Programming and Scripting

Shell Script to Dynamically Extract file content based on Parameters from a pdf file

Hi Guru's, I am new to shell scripting. I have a unique requirement: The system generates a single pdf(/tmp/ABC.pdf) file with Invoices for Multiple Customers, the format is something like this: Page1 >> Customer 1 >>Invoice1 + invoice 2 >> Page1 end Page2 >> Customer 2 >>Invoice 3 + Invoice 4... (3 Replies)
Discussion started by: DIps
3 Replies

3. Shell Programming and Scripting

Script to process file from a directory and grep the required content and print

Hi All, below script reads the perticular files from the directory. Am trying to fetch status and print them in the required format. It needs to read line and search for string "Passed/Failed" and print them under correct sub header. script : BASE_DIR=/tmp/test/REPORT/CollectReport #... (16 Replies)
Discussion started by: Optimus81
16 Replies

4. Shell Programming and Scripting

Looking for shell script to monitor CPU utilization and send mail once exceed 75%

Dear Group, I'm look for shell script to Monitor CPU usage and send mail once it exceed 75% I'm running Suse10.4. (3 Replies)
Discussion started by: clfever
3 Replies

5. Shell Programming and Scripting

Shell script to remove some content in a file

How can I remove all data that contain domain e.g zzgh@something.com, sdd@something.com.my and gg@something.my in one file? so that i only have data without the domain in the file. Here is the file structure "test.out" more test.out 1 zzztop@b.com 1 zzzulll 1 zzzullll@s.com.my ... (4 Replies)
Discussion started by: Mr_47
4 Replies

6. Shell Programming and Scripting

Monitor file generation for an hour using Korn shell script

Hi, I am new to this unix scripting, I got a requirement like.. Files with *.XML extension will be generating in a /home/sample/ folder for every 15 mins. I need to monitor those files in that particular folder for every hour. If no file has been generated in that particular folder for an... (7 Replies)
Discussion started by: siri_886
7 Replies

7. Shell Programming and Scripting

Delete content of file 1 in file 2 with shell script

OK, best is I explain what the operating enviroment is. Linux, but Motomagx. It is a Linux operated mobile phone, Motorola V8. I am writting a shell script, but got stuck. I have to delete the complete content of file 1 in file 2. I have attached the 2 files. You can see that the content of... (2 Replies)
Discussion started by: rasputin007
2 Replies

8. UNIX for Advanced & Expert Users

Reading a file and sending mail based on content of the file

Hi Gurus, I am having an requirement. i have to read a list file which contains file names and send mail to different users based on the files in the list file. eg. if file a.txt exists then send a mail to a@a.com simillary for b.txt,c.txt etc. Thanks for your help, Nimu (6 Replies)
Discussion started by: nimu1979
6 Replies

9. Shell Programming and Scripting

shell script to search content of file with timestamps in the directory

hello, i want to make a script to search the file contents in my home directory by a given date and output me the line that has the date... (10 Replies)
Discussion started by: psychobeauty
10 Replies

10. Shell Programming and Scripting

shell script to edit the content of a file

Hi I need some help using shell script to edit a file. My original file has the following format: /txt/email/myemail.txt /txt/email/myemail2.txt /pdf/email/myemail.pdf /pdf/email/myemail2.pdf /doc/email/myemail.doc /doc/email/myemail2.doc I need to read each line. If the path is... (3 Replies)
Discussion started by: tiger99
3 Replies
Login or Register to Ask a Question