Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
watch is a linux bash script to monitor file modification recursively and execute bash commands as changes occur
#!/usr/bin/env bash
# script: watch
# author: Mike Smullin <mike@smullindesign.com>
# license: GPLv3
# description:
# watches the given path for changes
# and executes a given command when changes occur
# usage:
# watch <path> <cmd...>
#
path=$1
shift
cmd=$*
sha=0
update_sha() {
sha=`ls -lR --time-style=full-iso $path | sha1sum`
}
update_sha
previous_sha=$sha
build() {
echo -en " building...\n\n"
$cmd
echo -en "\n--> resumed watching."
}
compare() {
update_sha
if [[ $sha != $previous_sha ]] ; then
echo -n "change detected,"
build
previous_sha=$sha
else
echo -n .
fi
}
trap build SIGINT
trap exit SIGQUIT
echo -e "--> Press Ctrl+C to force build, Ctrl+\\ to exit."
echo -en "--> watching \"$path\"."
while true; do
compare
sleep 1
done
@smartest

This comment has been minimized.

Copy link

commented Jul 1, 2014

Thanks, very helpful to me.

@swe20144

This comment has been minimized.

Copy link

commented Sep 11, 2014

nice! i didnt know Ctrl+\ is SIGQUIT

@jonnung

This comment has been minimized.

Copy link

commented Mar 3, 2015

Thank you!

@srph

This comment has been minimized.

Copy link

commented Mar 19, 2015

Dayum! 💗

@Gutz-Pilz

This comment has been minimized.

Copy link

commented Jun 11, 2015

nice one. Thanks a lot!

@mkormendy

This comment has been minimized.

Copy link

commented Oct 23, 2015

What's the performance hit on using this?

@cbelth

This comment has been minimized.

Copy link

commented Feb 15, 2016

Thank you!

@fghhfg

This comment has been minimized.

Copy link

commented May 2, 2016

this code can't be stopped in git in windows, ctrl + \ doesn't work.

@popey456963

This comment has been minimized.

Copy link

commented May 16, 2016

@fghhfg Don't you want ctrl + c?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.