Skip to content

Instantly share code, notes, and snippets.

@RooSoft
Created September 30, 2020 18:01
Show Gist options
  • Save RooSoft/28163c29d3acd86087b9c995842350e1 to your computer and use it in GitHub Desktop.
Save RooSoft/28163c29d3acd86087b9c995842350e1 to your computer and use it in GitHub Desktop.
Watch folder for file manipulation
usage() {
echo "Usage: ${0} folder" >&2
echo "Watches file manipulations in a folder" >&2
exit 1
}
if [[ "$#" -ne 1 ]] ; then
usage
fi
dir=$1
inotifywait -m $dir -e open -e close_write -e close_nowrite -e create -e modify -e delete -e moved_to -e moved_from |
while read dir action file; do
formattedDate=$(date +"%Y-%m-%d %T")
[[ $action =~ .*OPEN*. ]] && {
echo "$formattedDate $file has been opened in $dir"
}
[[ $action =~ .*CLOSE_NOWRITE*. ]] && {
echo "$formattedDate $file has been closed in $dir"
}
[[ $action =~ .*CLOSE_WRITE*. ]] && {
echo "$formattedDate $file has been written to and closed in $dir"
}
[[ $action =~ .*CREATE*. ]] && {
echo "$formattedDate $file has been created in $dir"
}
[[ $action =~ .*MODIFY*. ]] && {
echo "$formattedDate $file has been modified in $dir"
}
[[ $action =~ .*DELETE*. ]] && {
echo "$formattedDate $file has been deleted from $dir"
}
[[ $action =~ .*MOVED_TO*. ]] && {
echo "$formattedDate $file has been moved in $dir"
}
[[ $action =~ .*MOVED_FROM*. ]] && {
echo "$formattedDate $file has been moved out of $dir"
}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment