Here's a rough version of what you're looking for. It will require you to keep a list from the last sync around. (How you authenticate for this is a different topic, but instead of saving the password in a script I'd recommend using ssh keys).
#!/bin/bash
lastlist=/tmp/sftp_list.old
newlist=/tmp/sftp_list.new
localpath=/home/foo/downloads
remotepattern='/home/bar/*.zip'
remotehost='account@0.0.0.0'
notifysubject="New remote files"
notifyaddress="foo@example.com bar@example.com"
cd "$localpath"
if echo "ls -1 $remotepattern" | sftp "$remotehost" >"$newlist"; then
filelist=$(comm -13 "$lastlist" "$newlist")
[ $(echo "$filelist" | wc -l) -gt 0 ] && {
{
echo "New remote files:"
echo "$filelist"
} | mail -s "$notifysubject" "$notifyaddress"
# download new files
echo "$filelist" | sed 's/^/get /' | sftp "$remotehost"
rm -f "$lastlist"
mv "$newlist" "$lastlist"
}
else
echo "Error connecting to the remote host"
fi
This downloads only new files, but it will fail in subtle ways if any of the filenames contain \n
(which is very rare). A simple fix is to re-download all matching files instead of selecting by name: echo "get $remotepattern" | sftp "$remotehost"