I need to make a crontab script (executed automatically and periodically) that should find the latest changed file of a folder and then transfer it to another machine, using an sftp connection. The first part of the problem is solved by extracting the name of the desired file:
cd $myFolder
output=$(find . -type f -printf "%C@ %p\n" | sort -rn | head -n 1)
filename=$(echo $output | cut -d'/' -f 2)
But the second part is difficult, because I cannot find the way to type the value of $filename variable in a Linux sftp connection and also the user/password in a non-interactive way. Saving it into a temporary file may be a good solution.
Is there any better alternative?
Thanks
You could use inotify to monitor the
directory and trigger on modify. The file name can be fed to rsync or
scp. Example:
inotifywait \
--quiet \
--event modify \
--format '%f' \
--monitor watch_directory |
while read FILE; do \
scp watch_directory/$FILE host:/destination;
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With