Created
June 28, 2020 13:08
-
-
Save 3j14/5e90a5acb7e3967c0de00998815cb8c0 to your computer and use it in GitHub Desktop.
Watch files and upload via scp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
UPLOAD=$1 | |
fswatch -0 . | xargs -0 -I {} sh -c 'p="{}"; f="${p/$(pwd)\/}"; scp $p '"$UPLOAD"'"${f}"' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fswatch
andscp
This requires
fswatch
. Make sure this file is executable by runningchmod +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
/
.How It Works
fswatch -0 .
: Watches file changes within the current directory. Changed files are separated using the0
characterxargs -0 -I {} sh -c
: Execute a shell script for every changed file, separated by0
. The sequence{}
will be replacedwith the actuall file path (absolute)
p="{}"; f="${p/$(pwd)\/}"; scp $p $UPLOAD"${f}"
: Variablep
holds the absolute file path, variablef
holds the relative file path:f="${p/$(pwd)\/}"
removes the current directory ($(pwd)
) fromp
.scp $p $UPLOAD"${f}
will upload the file to$UPLOAD
.