我很清楚能够做find myfile.txt -mtime +5
次判断我的文件是否超过5天.然而,我想在我的文件中获取mtime.txt并将其存储到变量中以供进一步使用.我该怎么做?
我很清楚能够做find myfile.txt -mtime +5
次判断我的文件是否超过5天.然而,我想在我的文件中获取mtime.txt并将其存储到变量中以供进一步使用.我该怎么做?
stat
可以为您提供以下信息:
filemtime=`stat -c %Y myfile.txt`
%Y
给出了最后一次修改,即"从纪元开始的秒数",但还有很多其他选项;more info.因此,如果文件在2011年1月22日格林威治标准时间15:30被修改,上面的数字将返回1295710237.
Edit: Ah, you want the time in days since it was modified. That's going to be more complicated, not least because a "day" is not a fixed period of time (some "days" have only 23 hours, others 25 — thanks to daylight savings time).
naive版可能是这样的:
filemtime=`stat -c %Y $1`
currtime=`date +%s`
diff=$(( (currtime - filemtime) / 86400 ))
echo $diff
...但再一次,这是假设一天总是正好86400秒长.
更多关于bash here中算术的信息.