Linux script that monitors file changes in folders (e.g. autospec!)

I want to automatically run the assembly every time the file is changed.

I used autospec (RSpec) in Ruby and loved this.

How can this be done in bash?

+17
linux bash build automation rspec
Jun 04 '10 at 9:08
source share
6 answers

After reading the answers to other posts, I found a message (now gone), I created this script: -

#!/bin/bash sha=0 previous_sha=0 update_sha() { sha=`ls -lR . | sha1sum` } build () { ## Build/make commands here echo echo "--> Monitor: Monitoring filesystem... (Press enter to force a build/update)" } changed () { echo "--> Monitor: Files changed, Building..." build previous_sha=$sha } compare () { update_sha if [[ $sha != $previous_sha ]] ; then changed; fi } run () { while true; do compare read -s -t 1 && ( echo "--> Monitor: Forced Update..." build ) done } echo "--> Monitor: Init..." echo "--> Monitor: Monitoring filesystem... (Press enter to force a build/update)" run 
+5
Feb 10 '11 at 16:54
source share

Take a look at incron and inotify-tools .

+13
Jun 04 '10 at 13:23
source share

keywords - inotifywait and inotifywatch commands

+5
Jun 04 '10 at 9:10
source share

How about this script? Uses the "stat" command to get the file access time and runs the command whenever the access time changes (each time the file is accessed).

 #!/bin/bash while true do ATIME=`stat -c %Z /path/to/the/file.txt` if [[ "$ATIME" != "$LTIME" ]] then echo "RUN COMMNAD" LTIME=$ATIME fi sleep 5 done 
+5
Aug 20 '13 at 17:01
source share

See this example as an improvement on Jan Vaughan's answer:

 #!/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 
+2
Aug 31 '13 at 23:19
source share

If you installed entr , then in the shell you can use the following syntax:

 while true; do find src/ | entr -d make build; done 
+2
Jul 06 '16 at 17:54
source share



All Articles