Skip to content

Instantly share code, notes, and snippets.

@mmcgrana mmcgrana/copies Secret
Last active Mar 19, 2019

Embed
What would you like to do?
#!/usr/bin/env bash
if [ "$#" -ne 3 ]; then
echo "Usage: ./copies [source-file] [dest-dir] [seconds-between-checks]" >&2; exit 1
fi
SRC=$1
DEST=$2
SECS=$3
if ! [ -f "$SRC" ]; then
echo "No source-file found at $SRC" >&2; exit 1
fi
BASE=`basename $SRC`
if ! [ -d "$DEST" ]; then
echo "No dest-dir found at $DEST" >&2; exit 1
fi
RE='^[0-9]+$'
if ! [[ $SECS =~ $RE ]]; then
echo "seconds-between-copies not a number" >&2; exit 1
fi
echo "Copying new versions of $SRC to $DEST/[timestamp].$SRC every $SECS seconds."
echo "Use CTRL-C to stop."
echo
LAST=""
for (( ; ; ))
do
STAMP=`date -u +"%Y-%m-%d.%H-%M-%S."`
STAMPED_DEST="$DEST/$STAMP$BASE"
if [ -z "$LAST" ] || ! $(cmp --silent "$SRC" "$LAST"); then
echo "Changed: cp $SRC $STAMPED_DEST"
cp $SRC $STAMPED_DEST
LAST=$STAMPED_DEST
else
echo "Unchanged"
fi
sleep $SECS
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.