Linux 如何从 /bin/sh 判断文件是否早于 30 分钟?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2005021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I tell if a file is older than 30 minutes from /bin/sh?
提问by magol
How do I write a script to determine if a file is older than 30 minutes in /bin/sh?
如何编写脚本来确定 /bin/sh 中的文件是否超过 30 分钟?
Unfortunately does not the stat
command exist in the system. It is an old Unix system, http://en.wikipedia.org/wiki/Interactive_Unix
不幸的是,系统中不stat
存在该命令。这是一个旧的 Unix 系统,http://en.wikipedia.org/wiki/Interactive_Unix
Perl is unfortunately not installed on the system and the customer does not want to install it, and nothing else either.
不幸的是,系统上没有安装 Perl,客户也不想安装它,也没有别的。
回答by slebetman
The following gives you the file age in seconds:
以下以秒为单位为您提供文件年龄:
echo $(( `date +%s` - `stat -L --format %Y $filename` ))
which means this should give a true/false value (1/0) for files older than 30 minutes:
这意味着这应该为超过 30 分钟的文件提供真/假值 (1/0):
echo $(( (`date +%s` - `stat -L --format %Y $filename`) > (30*60) ))
30*60
-- 60 seconds in a minute, don't precalculate, let the CPU do the work for you!
30*60
-- 一分钟60秒,不要预先计算,让CPU为你做事!
回答by Schwern
Here's one way using find
.
这是使用find
.
if test "`find file -mmin +30`"
The find
command must be quoted in case the file in question contains spaces or special characters.
find
如果相关文件包含空格或特殊字符,则必须引用该命令。
回答by Rob Wells
You can do this by comparing to a reference file that you've created with a timestamp of thirty minutes ago.
您可以通过与您创建的时间戳为 30 分钟前的参考文件进行比较来完成此操作。
First create your comparison file by entering
首先通过输入创建您的比较文件
touch -t YYYYMMDDhhmm.ss /tmp/thirty_minutes_ago
replacing the timestamp with the value thirty minutes ago. You could automate this step with a trivial one liner in Perl.
用三十分钟前的值替换时间戳。您可以使用 Perl 中的一个简单的单行代码自动执行此步骤。
Then use find's newer operator to match files that are older by negating the search operator
然后使用 find 的 newer 运算符通过否定搜索运算符来匹配较旧的文件
find . \! -newer /tmp/thirty_minutes_ago -print
回答by Dale Hagglund
What do you mean by older than 30 minutes: modified more than 30 minutes ago, or created more than 30 minutes ago? Hopefully it's the former, as the answers so far are correct for that interpretation. In the latter case, you have problems since unix file systems do not track the creation time of a file. (The ctime
file attribute records when the inodecontents last changed, ie, something like chmod
or chown
happened).
超过 30 分钟是什么意思:修改超过 30 分钟,或创建超过 30 分钟?希望是前者,因为到目前为止的答案对于该解释是正确的。在后一种情况下,您会遇到问题,因为 unix 文件系统不跟踪文件的创建时间。(ctime
文件属性记录inode内容最后一次更改的时间,即类似chmod
或chown
发生的事情)。
If you really need to know if file was created more than 30 minutes ago, you'll either have to scan the relevant part of the file system repeatedly with something like find
or use something platform-dependent like linux's inotify.
如果你真的需要知道文件是否是在 30 分钟前创建的,你要么必须用类似的东西重复扫描文件系统的相关部分,find
要么使用像 linux 的inotify这样的平台相关的东西。
回答by alvherre
If you're writing a sh script, the most useful way is to use test
with the already mentioned stat trick:
如果您正在编写 sh 脚本,最有用的方法是使用test
已经提到的 stat 技巧:
if [ `stat --format=%Y $file` -le $(( `date +%s` - 1800 )) ]; then
do stuff with your 30-minutes-old $file
fi
Note that [
is a symbolic link (or otherwise equivalent) to test
; see man test
, but keep in mind that test
and [
are also bash builtins and thus can have slightly different behavior. (Also note the [[
bash compound command).
请注意,这[
是到test
;的符号链接(或其他等效链接)。see man test
,但请记住,test
和[
也是 bash 内置函数,因此行为可能略有不同。(另请注意[[
bash 复合命令)。
回答by Schwern
Ok, no stat and a crippled find. Here's your alternatives:
好吧,没有统计数据和残缺的发现。这是您的替代方案:
Compile the GNU coreutilsto get a decent find (and a lot of other handy commands). You might already have it as gfind.
编译GNU coreutils以获得不错的 find(以及许多其他方便的命令)。您可能已经将它作为 gfind 使用了。
Maybe you can use date
to get the file modification time if -r
works?
date
如果-r
有效,也许您可以用来获取文件修改时间?
(`date +%s` - `date -r $file +%s`) > (30*60)
Alternatively, use the -nt
comparision to choose which file is newer, trouble is making a file with a mod time 30 minutes in the past. touch
can usually do that, but all bets are off as to what's available.
或者,使用-nt
比较来选择哪个文件较新,麻烦的是制作一个修改时间为 30 分钟过去的文件。 touch
通常可以做到这一点,但所有的赌注都是关于可用的。
touch -d '30 minutes ago' 30_minutes_ago
if [ your_file -ot 30_minutes_ago ]; then
...do stuff...
fi
And finally, see if Perl is available rather than struggling with who knows what versions of shell utilities.
最后,看看 Perl 是否可用,而不是纠结于谁知道 shell 实用程序的版本。
use File::stat;
print "Yes" if (time - stat("yourfile")->mtime) > 60*30;
回答by dderton
#!/usr/bin/ksh
## this script creates a new timer file every minute and renames all the previously created timer files and then executes whatever script you need which can now use the timer files to compare against with a find. The script is designed to always be running on the server. The first time the script is executed it will remove the timer files and it will take an hour to rebuild them (assuming you want 60 minutes of timer files)
set -x
# if the server is rebooted for any reason or this scripts stops we must rebuild the timer files from scratch
find /yourpath/timer -type f -exec rm {} \;
while [ 1 ]
do
COUNTER=60
COUNTER2=60
cd /yourpath/timer
while [ COUNTER -gt 1 ]
do
COUNTER2=`expr $COUNTER - 1`
echo COUNTER=$COUNTER
echo COUNTER2=$COUNTER2
if [ -f timer-minutes-$COUNTER2 ]
then
mv timer-minutes-$COUNTER2 timer-minutes-$COUNTER
COUNTER=`expr $COUNTER - 1`
else
touch timer-minutes-$COUNTER2
fi
done
touch timer-minutes-1
sleep 60
#this will check to see if the files have been fully updated after a server restart
COUNT=`find . ! -newer timer-minutes-30 -type f | wc -l | awk '{print }'`
if [ $COUNT -eq 1 ]
then
# execute whatever scripts at this point
fi
done
回答by tommy.carstensen
For those like myself, who don't like back ticks, based on answer by @slebetman:
对于像我这样不喜欢倒勾的人,根据@slebetman 的回答:
echo $(( $(date +%s) - $(stat -L --format %Y $filename) > (30*60) ))
回答by user8050735
if [[ "$(date --rfc-3339=ns -r /tmp/targetFile)" < "$(date --rfc-3339=ns --date '90 minutes ago')" ]] ; then echo "older"; fi