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
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
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:
#!/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
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
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:
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
How about linking the file instead of moving it?
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
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