There's generally no simple portable (POSIX) way to get file modification times.
Note that if your Unix has a version of find
that includes GNU extensions (like -printf
) you can use find
to get the date of the original logfile. If your version of date
does not include the -v
for adjusting the date string forward and backward in time, then you have to find some way to convert the date to Epoch dates (seconds since long ago) and adjust the dates using expr
(+/- 86400*5) and convert that to a format usable with touch
.
You've told us you're using QNX so with the QNX version of find
you will have the -printf
extension. This means you can create your values with find
but not able to adjust them +/- 5 days with date -v
or convert them to Epoch times for modification with expr
. The QNX documents seems silent on how you might do this in a simple obvious way, illustrating why a simple portable (POSIX shell) way to get file modification times, set the date, convert date formats, etc. etc. would be so nice to have in the real world. You'll need to ask QNX gurus for more help. Sorry!
To complement Jaypal Singh's GNU coreutils solution BSD/POSIX and Perl approaches follow.
For BSD derived systems (FreeBSD, DragonFly, possibly OS/X) replacing the commands that create the $start
and $end
variables in @JS's solution with the following might work:
#!/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$$
The last part of the script as @JS already described.
Another alternative involves someone riding a cross-platform camel to the rescue ... like this :-)
This could be much nicer of course but here's a perl
approach cribbed from find2perl
output:
#!/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 it's for interactive use you could add:
if ($#ARGV != 1) { # require 2 args (last array index +1)
usage() ;
}
above/before the sub wanted
runs and something like sub usage { print "whatever \n"; exit;}
to make it more fancy.
Cheers,
touch
) and then use them as-newer
and! -newer
arguments tofind
. – Seal