Skip to content

Instantly share code, notes, and snippets.

@DrMcCoy
Created July 1, 2016 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrMcCoy/3151d19a2a683322490dd93299170c4b to your computer and use it in GitHub Desktop.
Save DrMcCoy/3151d19a2a683322490dd93299170c4b to your computer and use it in GitHub Desktop.
#!/bin/bash
PID=
KILLPID() {
if [ -n "$PID" ]
then
echo "$PID"
kill -9 "$PID"
fi
}
KILLIT() {
exit 0
}
trap KILLPID EXIT
trap KILLIT SIGINT
if [ $# -lt 2 ]
then
echo "Usage: "$(basename $0)" <file> <directory> <savename>"
exit 1
fi
F="$1"
FD=$(dirname $(realpath "$F"))
D="$2"
S="$3"
if [ \( ! \( -f "$F" \) \) -o \( ! \( -d "$FD" \) \) -o \( ! \( -d "$D" \) \) ]
then
exit 1
fi
rm "$D"/status &>/dev/null
inotifywait -q -m -o "$D"/status -e close_write -e moved_to -r "$FD" &
PID=$!
sleep 1
echo "$F"
tail -f "$D"/status | while read i
do
if ( ! echo "$i" | grep -q "$F"'$' )
then
continue
fi
if ( ! echo "$i" | egrep -q "(CLOSE_WRITE)|(MOVED_TO)" )
then
continue
fi
T=$(date +%Y%m%dT%H%M%S)
echo "$S""$T"
cp "$F" "$D"/"$S""$T"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment