calculate date 4 days ago

J

joe shaboo

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
 
P

Paul Lalli

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);
#perform your task on each member of @files;

Paul Lalli
 
P

Paul Lalli

Warning: Use of "-M" without parens is ambiguous at test.pl line 4
Unterminated <> operator at test.pl line 4.

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
 
J

Jon Ericson

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
 
B

Ben Morrow

Quoth Paul Lalli said:
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
 
J

Jim Cochrane

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
 
J

Jon Ericson

Jim Cochrane said:
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
 
J

Jim Cochrane

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

joe shaboo

Jon Ericson said:
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.


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

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
 
T

Tintin

Jim Cochrane said:
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"
 
J

Jim Cochrane

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top