Skip to content

Instantly share code, notes, and snippets.

@renekreijveld
Last active May 17, 2022 15:32
Show Gist options
  • Save renekreijveld/6077760 to your computer and use it in GitHub Desktop.
Save renekreijveld/6077760 to your computer and use it in GitHub Desktop.
Bash script to detect new and changed php and html files last 3 hours. Skips cache directories.
#!/bin/sh
# jnewfiles
# Detects new and changed php and html files last xxx minutes
# (C) 2014 Rene Kreijveld, enail [at] renekreijveld [dot] nl
# Update 31-12-2013: only send email when changes are found
# Update 04-02-2014: check for new files last three hours
# Update 05-05-2014: check for new html files also
# Update 07-05-2014: filter out ju_chached and DirectAdmin stats folders in html files
# Update 04-06-2014: added duration, subject_prefix, script_fullname and sendnotfound variables.
# added option to send email of nothing found
# All these improvements by Christophe Avonture
# Update 21-10-2014: code rewrite and added auto update feature
# Update 24-10-2014: added -n (no e-mail) option
# Update 29-10-2014: correction in the instructions
# Update 26-11-2014: changed test from -mmin to -cmin, added check for change in .htaccess files.
# Update 26-11-2014: added List generated by message at end of list
# Save this file as /usr/local/sbin/newfiles
# Modify the homedir and tmpdir variables to your needs.
#
# Then add to your local cron to run every 2 hours:
# 5 0,2,4,6,8,10,12,14,16,18,20,22 * * * /usr/local/sbin/jnewfiles -n [email protected]
# Replace [email protected] with your own email address
# To run cron every hour:
# 5 * * * * /usr/local/sbin/jnewfiles -n [email protected]
# Setup variables
myname=$(basename ${0})
version=3.8
server="$(hostname)"
tmpdir=/tmp
homedir=/home
emailmsg="${tmpdir}/new_changed.txt"
changes="${tmpdir}/new_changes.txt"
# 60 minutes -> find any files that have been changed during the last hour
duration=60
# Extra subject prefix for the email message
subject_prefix="Joomla file monitoring agent - server ${server} - `date` "
# Retrieve the name of this script
script_fullname=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)/$(basename "${BASH_SOURCE[0]}")
# Send email if no new files were found? Enter value "yes" or "no"
sendnotfound=no
# Find mail client
if [ -s /bin/mail ]
then
mailprog=/bin/mail
elif [ -s /usr/bin/mail ]
then
mailprog=/usr/bin/mail
fi
# display usage information
usage() {
echo ""
echo "${myname} version ${version}, written by Rene Kreijveld."
echo ""
echo "Usage: ${myname} [-n <email>] [-h]"
echo ""
echo "-n Send notification to <email> (comma-separated list of email addresses, without spaces)"
echo "-h Help. Display this info."
echo ""
exit 0
}
# process the arguments
sendmail="yes"
while getopts n:h opt
do
case "${opt}" in
n)
notify="yes"
receivers=${OPTARG}
;;
h) usage;;
\?) usage;;
esac
done
# Find all changed .php files last <duration> minutes, skip files with "cache" and "php-mail" in filename
find ${homedir} -type f -cmin -${duration} -name "*.php" -exec ls -l {} \; | grep -v cache | grep -v php-mail | grep -v "error.php" > ${changes}
# Find all changed .html files last <duration> minutes
find ${homedir} -type f -cmin -${duration} -name "*.html" -exec ls -l {} \; | grep -v cache | grep -v ju_cached | grep -v "/stats/" >> ${changes}
# Find all changed .js files last <duration> minutes
find ${homedir} -type f -cmin -${duration} -name "*.js" -exec ls -l {} \; | grep -v cache >> ${changes}
# Find all changed .htaccess files last <duration> minutes
find ${homedir} -type f -cmin -${duration} -name ".htaccess" -exec ls -l {} \; >> ${changes}
# Find all changed .php files last <duration> minutes in the temp directory
find ${tmpdir} -type f -cmin -${duration} -name "*.php" -exec ls -l {} \; >> ${changes}
# Only if changes are found, send email
if [ -s ${changes} ]
then
echo "${subject_prefix}" > ${emailmsg}
echo "New and changed files last ${duration} minuntes server ${server}" >> ${emailmsg}
echo "================================================================================" >> ${emailmsg}
cat ${changes} >> ${emailmsg}
echo "================================================================================" >> ${emailmsg}
echo "List generated by ${script_fullname}" >> ${emailmsg}
if [ "${notify}" == "yes" ]
then
${mailprog} -s "New/changed files ${server}" "${receivers}" < ${emailmsg}
else
cat ${emailmsg}
fi
else
if [ ${sendnotfound} == "yes" ]
then
echo "${subject_prefix} No changes found during the last ${duration} minutes" > ${emailmsg}
echo "List generated by ${script_fullname}" >> ${emailmsg}
if [ "${notify}" == "yes" ]
then
${mailprog} -s "${subject_prefix} - No changes found" "${receivers}" < ${emailmsg}
else
cat ${emailmsg}
fi
fi
fi
# Cleanup temporary files
rm -f ${emailmsg} ${changes}
@cavo789
Copy link

cavo789 commented Feb 14, 2014

Will try... Thanks Rene

@renekreijveld
Copy link
Author

Updated to also detect changed .html files.

@cavo789
Copy link

cavo789 commented Jun 2, 2014

Hi Rene; I've finally find the time to test your script :-)

I'm absolutly a dummy at this time in bash script. Neverthless, I've adjusted your script with two changes :

  1. add support of .htaccess files (just a copy/paste of one of your line)
  2. put a "Mail sent by ..." and the fullname of the running script (fg.i. /usr/local/sbin/newfiles).

This second changes to allow, the webmaster who receive the file, to know which script to stop f.i. in order to no more receive the email when changes are detected.

I've added these two lines :

SCRIPT_FULLNAME=$(cd `dirname "${BASH_SOURCE[0]}"` && pwd)/`basename "${BASH_SOURCE[0]}"`
echo "List generated by $SCRIPT_FULLNAME" >> $EMAILMSG

Thanks, once again, for you sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment