Skip to content

Instantly share code, notes, and snippets.

@quantombone
Created February 24, 2014 23:41
Show Gist options
  • Save quantombone/9199642 to your computer and use it in GitHub Desktop.
Save quantombone/9199642 to your computer and use it in GitHub Desktop.
Watch directory A, and copy into directory B when something in directory A changes.
#!/bin/sh
#Simple script to watch directory, and when something changes copy all of contents of directory A into directory B
#Requires: md5
#Tomasz Malisiewicz
#the directory to watch
SOURCE_DIR=/tmp/input/
#the directory we copy files into
TARGET_DIR=/tmp/output/
#the temporary file we use to monitor directory contents
#NOTE: this has to be different for each directory you want to monitor
TEMP_FILE=/tmp/list
#how many seconds to wait before checking directory contents
SLEEP_TIME=1
touch ${TEMP_FILE}
while [ true ]
do
F=`ls -l ${SOURCE_DIR} | md5`
O=`cat ${TEMP_FILE}`
if [ "$F" = "$O" ]
then
echo "not changed"
else
echo "Copying files"
cp $SOURCE_DIR/* $TARGET_DIR
fi
echo $F > $TEMP_FILE
sleep ${SLEEP_TIME}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment