Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux: automated sftp file transference by an ssh connection

Tags:

file

linux

ssh

sftp

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

like image 322
Luis Andrés García Avatar asked Jan 18 '26 16:01

Luis Andrés García


1 Answers

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
like image 139
Marco Avatar answered Jan 21 '26 09:01

Marco