Skip to content

Instantly share code, notes, and snippets.

@glaszig

glaszig/fswatch-scp.sh

Last active Nov 10, 2020
Embed
What would you like to do?
use fswatch to scp changed files to remote host
#!/usr/bin/env sh
# Copyright 2020 glaszig
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
watch_dir="$1"
target="$2"
self=$(basename $0)
if ! command -v fswatch >/dev/null ; then
echo "Please install the fswatch program."
echo "See http://emcrisostomo.github.io/fswatch/"
exit 2
fi
if [ -z "$target" ]; then
echo "usage: $self <watch-dir> <target>"
echo " watch-dir: path to a local directory to watch"
echo " target: an scp target specification, e.g."
echo " user@host.domain:/var/tmp"
exit 1
fi
upload() {
target_path=${1/$watch_dir/""}
target_path=${target_path#/}
echo "=> $target_path"
scp "$1" "${target%/}/${target_path}"
}
echo "watching: $watch_dir"
echo "target: $target"
echo "press ctrl+c to quit"
echo
fswatch -e '.git/' -e "$self" . | while read f; do upload "$f"; done
@adamgen

This comment has been minimized.

Copy link

@adamgen adamgen commented Nov 10, 2020

Thanks! how would I exclude files/directories?

@adamgen

This comment has been minimized.

Copy link

@adamgen adamgen commented Nov 10, 2020

Just realised it's the -e param used at the bottom row. Aliased for --exclude.

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