Thu, April 12, 2007
ふたつのファイルの日付を比較して、新しいかどうか調べる sh編
はじめに指定したファイルが、あとから指定したファイルより新しいかどうか を調べる。
code
filechk.sh
if [ -n "`find $1 -newer $2 -print`" ]; then
echo "$1 is newer than $2"
fi
実行
$ ls
filechk.sh foo.txt foo.html
$ touch foo.txt
$ sh filechk.sh foo.txt foo.html
foo.txt is newer than foo.html
$ touch foo.html
$ sh filechk.sh foo.txt foo.html
よく使うパターン
foo.txt を変換して foo.html を生成する場合、 foo.txt が更新されていたときだけ、変換処理したい場合に、 find -newer を使う。
foo.txt が foo.html より 新しいかどうか調べる
txtf=foo.txt
htmlf=foo.html
if [ -n "`find $txtf -newer $htmlf -print`" ]; then
echo "$txtf is newer than $htmlf"
fi