Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

calculate date 4 days ago

1 view
Skip to first unread message

joe shaboo

unread,
May 4, 2004, 3:47:17 PM5/4/04
to
Hi,

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

Purl Gurl

unread,
May 4, 2004, 4:07:44 PM5/4/04
to
Paul Lalli wrote:

> joe shaboo wrote:

(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

Paul Lalli

unread,
May 4, 2004, 4:39:36 PM5/4/04
to

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

Jon Ericson

unread,
May 4, 2004, 5:05:14 PM5/4/04
to
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

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

Jim Cochrane

unread,
May 4, 2004, 6:42:52 PM5/4/04
to
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.)

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.]

Jon Ericson

unread,
May 4, 2004, 7:52:50 PM5/4/04
to
Jim Cochrane <j...@shell.dimensional.com> writes:

> 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

Jim Cochrane

unread,
May 4, 2004, 8:56:58 PM5/4/04
to
In article <rcgad0n...@Jon-Ericson.sdsio.prv>, Jon Ericson wrote:
> Jim Cochrane <j...@shell.dimensional.com> writes:
>
>> In article <rcgisfb...@Jon-Ericson.sdsio.prv>, Jon Ericson wrote:
>>> jsh...@hotmail.com (joe shaboo) writes:
>>>
> ...

>> 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:

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

joe shaboo

unread,
May 5, 2004, 8:53:05 AM5/5/04
to
Jon Ericson <Jon.E...@jpl.nasa.gov> wrote in message news:<rcgad0n...@Jon-Ericson.sdsio.prv>...

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

Tintin

unread,
May 5, 2004, 2:57:24 PM5/5/04
to

"Jim Cochrane" <j...@shell.dimensional.com> wrote in message
news:slrnc9g73...@shell.dimensional.com...

> 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.)

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"


Jim Cochrane

unread,
May 5, 2004, 3:52:59 PM5/5/04
to

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. :-)

Purl Gurl

unread,
May 4, 2004, 4:50:11 PM5/4/04
to
joe shaboo wrote:

(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;

Purl Gurl

unread,
May 4, 2004, 5:04:52 PM5/4/04
to
Purl Gurl wrote:

> 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

Ben Morrow

unread,
May 4, 2004, 5:29:53 PM5/4/04
to

Quoth Paul Lalli <itty...@yahoo.com>:

> On Tue, 4 May 2004, joe shaboo wrote:
>
> > Hi,
> >
> > 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.
> >
>
> perldoc -f -X

>
> opendir (DIR, "/home/") or die "Cannot open directory: $!";
> my @files = grep { -M < 4 } readdir(DIR);

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

0 new messages