一般没有simple portable (POSIX) way to get file modification times。
请注意,如果您的 Unix 具有包含 GNU 扩展的 find 版本(如 -printf),您可以使用 find 获取原始日志文件的日期。如果您的date 版本不包括-v 用于及时向前和向后调整日期字符串,那么您必须找到某种方法将日期转换为纪元日期(很久以前的秒数)并使用调整日期expr (+/- 86400*5) 并将其转换为可用于touch 的格式。
您已经告诉我们您正在使用 QNX,因此对于 find 的 QNX 版本,您将拥有 -printf 扩展名。这意味着您可以使用find 创建您的值,但无法使用date -v 将它们调整为+/- 5 天,或者使用expr 将它们转换为纪元时间以进行修改。 QNX 文档似乎没有说明您如何以一种简单明了的方式做到这一点,说明了为什么simple portable (POSIX shell) way to get file modification times, set the date, convert date formats, etc. etc. 在现实世界中会如此美好。您需要向 QNX 专家寻求更多帮助。对不起!
为了补充 Jaypal Singh 的 GNU coreutils 解决方案,BSD/POSIX 和 Perl 方法如下。
对于 BSD 派生系统(FreeBSD、DragonFly,可能是 OS/X),将在 @JS 的解决方案中创建 $start 和 $end 变量的命令替换为以下可能有效:
#!/bin/sh
#
middle=`stat -r file.txt | cut -d" " -f10` # file.txt could
# come from a script
# positional argument like $1
# the number date range could also come from a positional argument like $2
start=`date -v-5d -r $middle +%Y%m%d%H%S`
end=`date -v+5d -r $middle +%Y%m%d%H%S`
touch -t "$start" /tmp/s$$
touch -t "$end" /tmp/e$$
find . -type f -newer /tmp/s$$ -and ! -newer /tmp/e$$
脚本的最后一部分如@JS 已经描述过。
另一种选择是骑跨平台骆驼进行救援......就像这样:-)
当然,这可能会更好,但这是从find2perl 输出中抄袭的perl 方法:
#!/usr/bin/env perl
# findrange Usage: cd /dir/to/search ; findrange 5 /file/to/compare.txt
use strict; use warnings;
use File::Find ();
use vars qw/*name/;
*name = *File::Find::name;
sub wanted;
# some values to use
my $RANGE = $ARGV[0] ; # get date range
my $REF_FILE = $ARGV[1] ; # get file to compare date
my $AGE_REF = -M $REF_FILE ;
my $start = $AGE_REF - $RANGE ; # +/- days from @ARGV[0]
my $end = $AGE_REF + $RANGE ;
# Descend file system searching/finding.
# from current directory "."
File::Find::find({wanted => \&wanted}, '.');
exit;
sub wanted {
( lstat($_)) && #caches results in "_" for -M to use
-f _ &&
(-M _ > $start) &&
! (-M _ > $end)
&& print("$name\n");
}
如果是交互式使用,您可以添加:
if ($#ARGV != 1) { # require 2 args (last array index +1)
usage() ;
}
在sub wanted 的上方/之前运行和sub usage { print "whatever \n"; exit;} 之类的东西,让它更漂亮。
干杯,