-
-
Save DrMcCoy/3151d19a2a683322490dd93299170c4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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