I have a directory where files are dropped arbitrarily in server A. I want to dump these files into server B and delete them from server A.
I would like write an Bash script where the code iterates through each file,
puts the file across to server B, and deletes it straight after. This will be ammended to the
cron and called hourly to scrape the files across to server B
The code I have attempted is:
#!/bin/bash MY_SCRIPT_NAME=`basename "$0"` PATH_TO_METRICS='/home/some/directory' if pidof -o %PPID -x $MY_SCRIPT_NAME > /dev/null; then echo "$MY_SCRIPT_NAME already running; exiting" exit 1 fi sftp -i my_priv_key -oPort=12345 email@example.com <<EndOfTransfer cd $PATH_TO_METRICS for filename in $PATH_TO_METRICS; do PUT "$filename" rm "$filename" done EndOfTransfer
- Execute command in sftp connection through script
- FTP file transfer, loop through a directory and copy old files
How does one iterate through files in a given a directory and use sftp to put each of the files across to another server, and delete the file if a successful transfer has happened?