21 Jul 2021, 12:12

monitor files in directory and scp them to server

I just posted on Reddit: monitor files in directory and scp them to server

I found Monitor Directory for New Files and now can send to STDOUT a list of file names as they are saved:

inotifywait -mre modify . | stdbuf -oL uniq | sed -e 's/ MODIFY //'

I tried wrapping the above in $( ) to let scp send the modified files to my server:

scp -P 22 -i /home/robnugen/privatekey $(inotifywait -mre modify . | stdbuf -oL uniq | sed -e 's/ MODIFY //') robnugen@remote:path  

But the files are not being sent to the server.

In front of sed, I tried stdbuf -oL and -u but neither helped.

The scp command works if I just use a filename in where I have the backticks above. i.e. this works:

scp -P 22 -i /home/robnugen/privatekey hello.txt robnugen@remote:path

How can I send filenames from STDOUT for use in scp?

16:11 Sunday 25 July 2021 JST

Thanks to /u/xkcd__386 answer, this is what I have been using, sans linewraps:

inotifywait --exclude '.git/*' -mre modify . |
stdbuf -oL uniq | sed -ue 's/ MODIFY //' |
xargs -d$'\n' -I% scp  -P 22 -i /home/robnugen/privatekey % robnugen@remote:path%