Skip to content

Instantly share code, notes, and snippets.

@3j14
Created June 28, 2020 13:08
Show Gist options
  • Save 3j14/5e90a5acb7e3967c0de00998815cb8c0 to your computer and use it in GitHub Desktop.
Save 3j14/5e90a5acb7e3967c0de00998815cb8c0 to your computer and use it in GitHub Desktop.
Watch files and upload via scp
#!/bin/bash
UPLOAD=$1
fswatch -0 . | xargs -0 -I {} sh -c 'p="{}"; f="${p/$(pwd)\/}"; scp $p '"$UPLOAD"'"${f}"'
@3j14
Copy link
Author

3j14 commented Jun 28, 2020

fswatch and scp

This requires fswatch. Make sure this file is executable by running chmod +x watchscp.sh.

Usage

The script will catch any changes to files within the directory (including sub-directories) it was executed in using fswatch.
The only argument required is the destination consisting of the user, the host and the path. Note that the path has to end with /.

./watchscp.sh user@host:/dest/

How It Works

  • fswatch -0 .: Watches file changes within the current directory. Changed files are separated using the 0 character
  • xargs -0 -I {} sh -c: Execute a shell script for every changed file, separated by 0. The sequence {} will be replaced
    with the actuall file path (absolute)
  • p="{}"; f="${p/$(pwd)\/}"; scp $p $UPLOAD"${f}": Variable p holds the absolute file path, variable f holds the relative file path:
    f="${p/$(pwd)\/}" removes the current directory ($(pwd)) from p. scp $p $UPLOAD"${f} will upload the file to $UPLOAD.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment