找出文件是否在最近2分钟内被修改过

在一个bash脚本中,我想检查文件是否在最近2分钟内被更改过。

我已经发现,我可以通过stat file.ext -c %y来访问上次修改的date。 我如何检查这个date是否超过两分钟?

我认为这会有所帮助,

 find . -mmin -2 -type f -print 

也,

 find / -fstype local -mmin -2 

完成脚本来做你以后的事情:

 #!/bin/sh # Input file FILE=/tmp/test.txt # How many seconds before file is deemed "older" OLDTIME=120 # Get current and file times CURTIME=$(date +%s) FILETIME=$(stat $FILE -c %Y) TIMEDIFF=$(expr $CURTIME - $FILETIME) # Check if file older if [ $TIMEDIFF -gt $OLDTIME ]; then echo "File is older, do stuff here" fi 

我用这种方法解决了这个问题:获取文件的当前日期和最后修改日期(均为unix时间戳格式)。 从当前日期减去修改日期,并将结果除以60(将其转换为分钟)。

 expr $(expr $(date +%s) - $(stat mail1.txt -c %Y)) / 60 

也许这不是最干净的解决方案,但是效果很好。