Bash script to automatically email variable filename and corresponding modified datetime stamp

Viewed 298

In short, i am trying to find the duplicate files ( which have been downloaded from FTP site, it is another script file) and email the business about all the duplicate files and its corresponding modified dates.

The file /path/test_mail.txt contains one file name per line (two filenames in this case), ex.

abc.xlsx
def.xlsx

In the below code, i am trying to find the modified datetimestamp for the first filename and pipe it with the respective filename and send an email , similarly the loop runs for the second one.

This is using stat

for val in '/path/test_mail.txt'; do
        { stat path/$val | grep 'Modify: ' | cut -d' ' -f2,3,4 | awk -F"." '{print $1}' ; } |

        $val

done |


mail -s "Duplicate file found ${DATE}" abc@xyz.com

I also tried in another way using ls -ltr

for val in '/path/tj_mail.txt'; do
            { ls -ltr /path/$val | cut -d' ' -f6,7,8 | find $val / -path

$val

    done |


    mail -s "Duplicate file found ${DATE}" abc@xyz.com

i was expecting the email body should be approximately like

Duplicate Filename - xyz.xlsx Uploaded time - 2020-02-17 11:18:10

Duplicate Filename - abc.xlsx Uploaded time - 2020-02-17 11:18:10

The below question is optional, but that would be great if you can help me!

Also i am using another script to find the duplicate filenames in the directory. it works perfectly fine. But i am wondering if i could fit in the same code above in 1 single script file, so that it would be crisp and easy!

{
DATE=`date +"%Y-%m-%d"`
dirname=/path
tempfile=myTempfileName
find $dirname -type f  > $tempfile
cat $tempfile | sed 's_.*/__' | sort |  uniq -d|
while read fileName
do
 grep "$fileName" $tempfile
done
} | tee '/path/tj_var.txt' | awk -F"/" '{print $NF}'  | tee '/path/tj_var.txt' | sort -u | tee '/path/tj_mail.txt' | mail -s "Duplicate file found ${DATE}" abc@xyz.com

This is my actual code

path = /marketsource/SrcFiles/Target_Shellscript_Autodownload/Airtime_Activation
printf "%s" "$(</marketsource/scripts/tj_mail.txt)" | while IFS= read -r filename; do
   mtime=$(stat -c %y "/path/$filename")
   printf 'Duplicate Filename - %s Uploaded time - %s\n' "$filename" "$mtime"
done | mail -s "Duplicate file found ${DATE}" tipalli@allegisgroup.com

 mtime=$(stat -c %y "/path/$filename" 2>/dev/null || echo "unknown (stat failed)")

this is the error!

./tj_mail1.ksh: line 1: path: command not found stat: cannot stat `/path/AirTimeActs_2020-02-08.xlsx': No such file or directory

Little more!!

My aim to find for any duplicates files, if therent arent any duplicate files and find command is empty, then perform the if condition and perform 'mv' command and exit the script entirely, if they are duplicate files, then exit the if condition and pipe the duplicate files and perform the mail and date stamp operation.

    {
DATE=`date +"%Y-%m-%d"`
dirname=/marketsource/SrcFiles/Target_Shellscript_Autodownload/Airtime_Activation
tempfile=myTempfileName
find $dirname -type f  > $tempfile
cat $tempfile | sed 's_.*/__' | sort |  uniq -d|
while read fileName
do
 grep "$fileName" $tempfile
done
}
if ["$fileName" == ""]; then
         mv /marketsource/SrcFiles/Target_Shellscript_Autodownload/Airtime_Activation/*.xlsx /marketsource/SrcFiles/Target_Shellscript_Autodownload/Airtime_Activation/Archive

         mv /marketsource/SrcFiles/Target_Shellscript_Autodownload/Airtime_Activation/*.csv /marketsource/SrcFiles/Target_Shellscript_Autodownload/Airtime_Activation/Archive
        exit 1

fi | tee '/marketsource/scripts/tj_var.txt' | awk -F"/" '{print $NF}'  | tee '/marketsource/scripts/tj_var.txt' | sort -u | tee '/marketsource/scripts/tj_mail.txt'



DATE=`date +"%Y-%m-%d"`
printf "%s\n" "$(</marketsource/scripts/tj_mail.txt)" | while IFS= read -r filename; do
   mtime=$(stat -c %y "/marketsource/SrcFiles/Target_Shellscript_Autodownload/Airtime_Activation/$filename")
   printf  'Duplicate Filename - %s Uploaded time - %s\n\n' "$filename" "$mtime"
done | mail -s "Duplicate file found ${DATE}" ti@allegisgroup.com
1 Answers

I assume the file /path/test_mail.txt has been prepared by some other script (as a list of duplicate files) and the task is to add the modification time of the files listed in /path/test_mail.txt and format the output as shown in the question.

while IFS= read -r filename; do
   mtime=$(stat -c %y "/path/$filename") 
   printf 'Duplicate Filename - %s Uploaded time - %s\n' "$filename" "$mtime"
done < "/path/test_mail.txt"

Instead of parsing the file /path/test_mail.txt you could also add this to a pipe like this

somehow_print_duplicate_file_names | while IFS= read -r filename; do
   mtime=$(stat -c %y "/path/$filename") 
   printf 'Duplicate Filename - %s Uploaded time - %s\n' "$filename" "$mtime"
done | somehow_send_mail

You could add some error handling in case stat fails.

   mtime=$(stat -c %y "/path/$filename" 2>/dev/null || echo "unknown (stat failed)")

or use stat's error message

   mtime=$(stat -c %y "/path/$filename" 2>&1)
Related