hi every one. one of my friends has writen this script and send it to me. this script can find files that add-delete-modify and also send an alert by email
i'm not catch all part of it.
can anyone explain me how this work
#!/bin/bash
START="a.txt"
END="b.txt"
DIFF="c.txt"
mv ${START} ${END} #moving the first file to be compared
find /home -type f -mmin -5 -print > ${START}
diff ${START} ${END} | grep '<' > ${DIFF}
COUNT=$(cat ${DIFF} | wc -l)
if [[ -s ${DIFF} ]] ; then
cat -- ${DIFF} | mail -s "Modified files in last 5 minutes *** ${COUNT} ***" root
fi
If it is working, it's just what you asked your friend for.
Why don't you ask your friend?
Next time, be more precise which part you do not understand instead, thanks.
#!/bin/bash
Use bash explicit as interpreter to run this script
START="a.txt"
END="b.txt"
DIFF="c.txt"
Define 3 variables with a file name.
mv ${START} ${END} #moving the first file to be compared
Renames the start file to the name of the end file. If it already exists, it is overwritten. Saves the data in the file from the previous run to b.txt so they are not lost and can be compared with actual data.
find /home -type f -mmin -5 -print > ${START}
Find files in directory /home that are not older than 5 minutes and write their path and name into the file a.txt.
diff ${START} ${END} | grep '<' > ${DIFF}
Find the lines that are different from the old file b.txt and write them to the file c.txt.
COUNT=$(cat ${DIFF} | wc -l)
Count the number of different lines.
if [[ -s ${DIFF} ]] ; then
cat -- ${DIFF} | mail -s "Modified files in last 5 minutes *** ${COUNT} ***" root
fi
Check if the file c.txt exists and has a size greater 0.
If this is the case, tell cat to not accept any switches and send a mail with the subject "Modified files..." where the mail's body is the cat 'ed different lines to root.
? I do not understand that question. All of this code is executed by the bash shell. The part you mention is just a definition of strings as values to variable names. These 3 lines do actually not touch the files in any way. This comes in the following lines.