Skip to content

Instantly share code, notes, and snippets.

@ejlevin1
Created March 21, 2019 17:33
Show Gist options
  • Save ejlevin1/c0cca7f82ac9cc206f50d88f18cd4c4c to your computer and use it in GitHub Desktop.
Save ejlevin1/c0cca7f82ac9cc206f50d88f18cd4c4c to your computer and use it in GitHub Desktop.
Script that copies files written to a specific folder to a different backup folder.
#!/bin/bash
SRC_DIR=$1
DST_DIR=$2
if ! [ -d "$SRC_DIR" ]; then
echo "Directory specified to watch does not exist."
fi
if ! [ -d "$DST_DIR" ]; then
echo "Destination directory specified does not exist."
fi
echo "Waiting to backup new files from [$SRC_DIR] to [$DST_DIR]."
inotifywait -m $SRC_DIR -e close_write |
while read path action file; do
if [ 'CLOSE_WRITE,CLOSE' = "$action" ] ; then
echo "Backing up new file: $file"
cp -vf "$path/$file" "$DST_DIR/$file"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment