Linux脚本,监视文件夹内的文件更改(就像autospec一样!)

我想在文件更改时自动启动构build

我在Ruby中使用了autospec(RSpec),并且非常喜欢。

这怎么能在bash中完成?

Solutions Collecting From Web of "Linux脚本,监视文件夹内的文件更改(就像autospec一样!)"

看看incron和inotify工具 。

关键字是inotifywait&inotifywatch命令

在阅读其他帖子的回复后,我发现一个帖子(现在已经消失),我创建了这个脚本:

#!/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 

这个脚本怎么样? 使用“stat”命令获取文件的访问时间,并在访问时间(访问文件时)发生变化时运行命令。

 #!/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 

如果你已经安装了,那么在shell中你可以使用下面的语法:

 while true; do find src/ | entr -d make build; done 

看这个例子是对Ian Vaughan答案的改进:

 #!/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