#!/bin/bash # find_new_files.sh # Meryll Larkin # 20200904 # 20210809 Added determine_days function # Helpful for incremental backups # 20210814 Added excludes ability dir=$1 newer=$2 pid=$$ today=$(date +"%Y%m%d") today_s=$(date +%s) year=$(date +%Y) cwdir=`pwd` outfile="${cwdir}/new_files_${today}_${pid}.txt" function determine_days { echo " Determine days using date of last backup" echo -n " Year of last backup? (4 digits or for $year): " read yr if [ -z "$yr" ] ; then yr=$year fi echo -n " Month of last backup? (2 digits - use 01 for Jan): " read mon echo -n " Day of last month? (1 or 2 digits): " read day # date in seconds since last epoch (1970) # date -d '2008-12-18 12:01:00' +%s lastbup=`date -d "${yr}-${mon}-${day} 12:01:00" +%s` # convert to days since last epoch lastbup=$((lastbup/86400)) today_s=$((today_s/86400)) newer=$((today_s-lastbup)) # Add 2 days padding newer=$((newer+2)) echo echo " Finding files newer than $newer days old" echo echo -n " Does that look right? [Y|n]: " read looksright looksright=`echo $looksright | tr '[:upper:]' '[:lower:]'` if [ "$looksright" = "n" ] ; then echo echo " Quitting due to confusion." exit fi } if [ ! -d "$dir" ] ; then echo -n " Absolute path to target directory? " read dir echo -n " Do you know the number of days since last backup? [y|N]: " read daysknown daysknown=`echo $daysknown | tr '[:upper:]' '[:lower:]'` if [ "$daysknown" != "y" ] ; then # go to the function determine_days determine_days else echo -n " Find files newer than how many days? " read newer fi fi if [ -z "$newer" ] ; then echo -n " Find files newer than how many days? " read newer fi echo echo -n " Are there any directories deep to $dir that you would like to exclude? [y|N]: " read exclude exclude=`echo $exclude | tr '[:upper:]' '[:lower:]'` if [ "$exclude" != "y" ] ; then # go to the function determine_days echo " Continuing..." else echo -n " Provide a space-separated list of directories to exclude (no slashes): " read excludes echo "excludes = $excludes" exclude_array=($excludes) fi echo "Today is $today" > $outfile echo echo " Find files in $dir that are less than $newer days old" | tee -a $outfile for i in ${dir}/* ; do ignore="n" for j in ${exclude_array[@]} ; do if [[ $i = *"$j" ]]; then echo "Ignoring $i because $j is on Excludes list" | tee -a $outfile echo "" | tee -a $outfile echo "****************************************************" | tee -a $outfile ignore="y" fi done if [[ $ignore == "n" ]] ; then echo "find $i -type f -mtime -${newer}" | tee -a $outfile echo "" | tee -a $outfile find $i -type f -mtime -${newer} | tee -a $outfile echo "****************************************************" | tee -a $outfile fi done echo echo "Results also printed to outfile $outfile" exit