I'm trying to list files that are 4 days old, and then perform an
action on them. I don't care about any files that are newer (have been
edited or touched in the last 4 days), only 4 days or older.
I can do this either through SHELL or perl, which ever is easier.
For example
ls -lt d.* | awk '{print $6 $7}' Provides a list of just of the date
of the files. What I would like to do is, if a file has been touched
in the last 4 days, grep for a certain term, and if it exists, pipe to
a file.
I was thinking of doing something like subtracting the day of the
month from system date, but then I run into problems in the first 4
days of the month.
If someone can think of a way to do this, I would be grateful.
Many Thanks,
Joe
(snipped)
> > I'm trying to list files that are 4 days old
> opendir (DIR, "/home/") or die "Cannot open directory: $!";
> my @files = grep { -M < 4 } readdir(DIR);
Warning: Use of "-M" without parens is ambiguous at test.pl line 4
Unterminated <> operator at test.pl line 4.
Purl Gurl
Well, that'll teach me to post untested code. My profound apologies.
Joe, please replace the
{ -M < 4 }
in my post with
{ -M $_ < 4 }
Thank you for catching my mistake, Purl Gurl.
Paul Lalli
> I'm trying to list files that are 4 days old, and then perform an
> action on them. I don't care about any files that are newer (have been
> edited or touched in the last 4 days), only 4 days or older.
>
> I can do this either through SHELL or perl, which ever is easier.
Using option 1:
$ find . -mtime -4
Option 2 can be autogenerated with the find2perl script:
$ find2perl . -mtime -4 -print | perl
(Instead of piping to perl, you could redirect to a file to read and
edit.)
Jon
For files 4 days or more older, I believe what is needed is: -mtime +4
(Check man find to be sure.)
and you can do (in bash or ksh):
process() {
: Do whatever with $1
}
for file in $(find . -mtime +4 -exec grep -l 'pattern' {} ';'); do
process $file
done
--
Jim Cochrane; j...@dimensional.com
[When responding by email, include the term non-spam in the subject line to
get through my spam filter.]
> In article <rcgisfb...@Jon-Ericson.sdsio.prv>, Jon Ericson wrote:
>> jsh...@hotmail.com (joe shaboo) writes:
>>
>>> I'm trying to list files that are 4 days old, and then perform an
>>> action on them. I don't care about any files that are newer (have been
>>> edited or touched in the last 4 days), only 4 days or older.
>>>
>>> I can do this either through SHELL or perl, which ever is easier.
>>
>> Using option 1:
>>
>> $ find . -mtime -4
>
> For files 4 days or more older, I believe what is needed is: -mtime +4
> (Check man find to be sure.)
Right. My manpage says:
TESTS
Numeric arguments can be specified as
+n for greater than n,
-n for less than n,
n for exactly n.
It's also posible that -atime would be required instead of -mtime.
> and you can do (in bash or ksh):
>
> process() {
> : Do whatever with $1
> }
>
> for file in $(find . -mtime +4 -exec grep -l 'pattern' {} ';'); do
> process $file
> done
I'm not sure what the point of using a function is. I'd probably
write something like:
$ find . -mtime +4 | xargs whatever
(As you can tell, I sometimes have trouble remembering the find
syntax. ;)
Jon
Just convenience - I prefer to not let one command line get too complex,
but it's not necessary.
>
> $ find . -mtime +4 | xargs whatever
>
> (As you can tell, I sometimes have trouble remembering the find
> syntax. ;)
>
> Jon
Right you are. I am sorry about the gibberish I posted. I actually
noticed it just as I reread the message on the board, but I figured
people could get the gist of the problem. I could reverse the
situation for which ever case I needed (older or newer than 4 days).
In any event, the simplest method for my means is to use the find
command with the -mtime. I had forgotten about that flag.
So, again thanks for your responses.
Joe
The OP's specifications are contradictory. They say "have been edited or
touched in the last 4 days", then say "only 4 days or older"
Funny, I didn't even notice that. I guess I just read it like a perl
compiler compiling a block - since the specs. end with: "edited or
touched in the last 4 days), only 4 days or older.", I guess I took the
last expression in the phrase "only 4 days or older" and took that as
the final value of his spec. :-)
(snipped)
> I'm trying to list files that are 4 days old, and then perform an
> action on them. I don't care about any files that are newer (have been
> edited or touched in the last 4 days), only 4 days or older.
> if a file has been touched in the last 4 days, grep for a certain term,
You are contradicting yourself.
Initially, you indicate files four days or older. Next
you state you want to parse files modified during
the last four days.
Are you looking for files which have been modified or
created four days or more, back, rather than simply
date stamp touched?
Work towards providing parameters which are clear,
concise, coherent and free of contradiction. As is,
your article is borderline gibberish.
Work with epoch seconds rather than traditional
date stamps as you indicate you are trying to do.
This is logical because stat will return epoch
seconds, not a typical datestamp.
Add your own keyterm parser.
Purl Gurl
--
#!perl
print "Content-type: text/plain\n\n";
opendir (DIRECTORY, ".");
while (defined ($filename = readdir (DIRECTORY)))
{
if ($^T - (4 * 86400) >= (stat ($filename)) [9])
{ push (@Array, $filename); }
}
@Array = sort (@Array);
for (@Array)
{ print "Filename: $_\n" }
exit;
> joe shaboo wrote:
(snipped)
> Add your own keyterm parser.
You may also parse for directory names before
pushing files into an array by disqualifying
directories with a -d test or qualifying files
with a -f test. Be careful to provide a full
path with your directory or file test; without
a full path, -d and -f will fail. This is
a common mistake by programmers. This pops
up especially when a directory location
has changed during transversal.
You may also parse out "." and ".." type
directory and child directory listings quite
easily with simple efficient code.
if (-f ($filename))
That will fail.
if (-f ("c:/path/to/$filename"))
That will succeed.
Here you will find a simple directory utility which
performs a variety of tasks. You may look at this
code to see how others create directory paths and
filenames when transversing a directory tree.
http://www.purlgurl.net/~purlgurl/perl/direct/dir_list.html
Purl Gurl
BZZT! You are checking the wrong files.
my $dir = '/home';
opendir my $DIR, $dir or die ...;
my @files = grep { -M "$dir/$_" < 4 } readdir DIR;
Ben
--
I've seen things you people wouldn't believe: attack ships on fire off
the shoulder of Orion; I watched C-beams glitter in the dark near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die. b...@morrow.me.uk