Skip to content

Instantly share code, notes, and snippets.

@mmcgrana

mmcgrana/copies Secret

Last active March 19, 2019 17:00
Show Gist options
  • Save mmcgrana/82b78d790fc61a687a06b4e17d9c50f1 to your computer and use it in GitHub Desktop.
Save mmcgrana/82b78d790fc61a687a06b4e17d9c50f1 to your computer and use it in GitHub Desktop.
#!/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