I want to copy all files with the extension .beta
from one directory to another and then send an email with following format.
The following files have been moved from directory A to Directory B:
1. la.beta
2. pa.beta
The email must be sent only if such files were found and were successfully moved.
This is what I have so far. The problem with this script is that it errors out if no files were found:
mv: cannot stat `/home/zaree/*.beta': No such file or directory`
Also, I receive an email even though the file is not moved.
#!/usr/bin/env bash
FROM_DIR='/home/zaree'
TO_DIR='/home/zaree/test'
FILE_FOUND=0
BODY=$(printf "$(date)\n\n")
BODY+=$(printf "The following files have been moved from\n")
BODY+=$(printf "%s\nto\n%s\n\n" "$FROM_DIR" "$TO_DIR")
for FILE in /home/zaree/*.beta; do
FILE_FOUND=1
mv $FILE /home/zaree/test
BODY+=$(printf "%s moved\n" "$FILE");
done
{
if (( $FILE_FOUND==1 )); then
printf "$BODY"
fi
} | mailx -s "Dev-Script" zaree@xyz.com