trouble with this code in deleting files older than 60 days

G

George Lewycky

My problem is trying to delete all files older than 60 days in the
directory /MLIM_data/archive/cmp through a Perl script.



At the UNIX command prompt, the following command works. It lists all
files older than 60 days and deletes them as well : find
/MLIM_data/archive/cmp -mtime +60 -exec rm -r {} \;



However, I need to use this command thru a Perl script and none of the
following combinations work.



# `find /MLIM_data/archive/cmp -mtime +60 -exec rm -r {} \;`;

# find /MLIM_data/archive/cmp -mtime +60 -exec rm -r {} \;

# `find /MLIM_data/archive/cmp -mtime +60`

# `find /MLIM_data/archive/cmp -mtime +60`;

# system("find /MLIM_data/archive/cmp -mtime +60 -exec

rm -r {} \;");



The only thing that does work from within a Perl script is a listing of
all the files older than 60 days :



system("find /MLIM_data/archive/cmp -mtime +60");



How can I delete all files older than 60 days in the directory
/MLIM_data/archive/cmp through my Perl script?



Thanks



George Lewycky
 
J

John W. Krahn

George said:
My problem is trying to delete all files older than 60 days in the
directory /MLIM_data/archive/cmp through a Perl script.

At the UNIX command prompt, the following command works. It lists all
files older than 60 days and deletes them as well : find
/MLIM_data/archive/cmp -mtime +60 -exec rm -r {} \;

man find2perl


John
 
X

xhoster

George Lewycky said:
My problem is trying to delete all files older than 60 days in the
directory /MLIM_data/archive/cmp through a Perl script.

At the UNIX command prompt, the following command works. It lists all
files older than 60 days and deletes them as well : find
/MLIM_data/archive/cmp -mtime +60 -exec rm -r {} \;

However, I need to use this command thru a Perl script and none of the
following combinations work.

# `find /MLIM_data/archive/cmp -mtime +60 -exec rm -r {} \;`;

In what why does it not work?
# find /MLIM_data/archive/cmp -mtime +60 -exec rm -r {} \;

In what why does it not work?
# `find /MLIM_data/archive/cmp -mtime +60`

In what why does it not work?
# `find /MLIM_data/archive/cmp -mtime +60`;

In what why does it not work?
# system("find /MLIM_data/archive/cmp -mtime +60 -exec

rm -r {} \;");

In what why does it not work? (I suspect you need to backwhack your
backwhack on the ';'. Or use single quotes rather than double quotes. And
fix the line wrapping.)
The only thing that does work from within a Perl script is a listing of
all the files older than 60 days :

system("find /MLIM_data/archive/cmp -mtime +60");

How can I delete all files older than 60 days in the directory
/MLIM_data/archive/cmp through my Perl script?

You could do it all within Perl using File::Find.

Or you could fix the shell-out by replacing "find" with "echo", to make
sure that you are giving the program the arguments you think you are giving
it.

Xho
 
X

xhoster

George Lewycky said:
My problem is trying to delete all files older than 60 days in the
directory /MLIM_data/archive/cmp through a Perl script.

At the UNIX command prompt, the following command works. It lists all
files older than 60 days and deletes them as well : find
/MLIM_data/archive/cmp -mtime +60 -exec rm -r {} \;

However, I need to use this command thru a Perl script and none of the
following combinations work.

# `find /MLIM_data/archive/cmp -mtime +60 -exec rm -r {} \;`;

In what way does it not work?
# find /MLIM_data/archive/cmp -mtime +60 -exec rm -r {} \;

In what way does it not work?
# `find /MLIM_data/archive/cmp -mtime +60`

In what way does it not work?
# `find /MLIM_data/archive/cmp -mtime +60`;

In what way does it not work?
# system("find /MLIM_data/archive/cmp -mtime +60 -exec

rm -r {} \;");

In what way does it not work? (I suspect you need to backwhack your
backwhack on the ';'. Or use single quotes rather than double quotes. And
fix the line wrapping.)
The only thing that does work from within a Perl script is a listing of
all the files older than 60 days :

system("find /MLIM_data/archive/cmp -mtime +60");

How can I delete all files older than 60 days in the directory
/MLIM_data/archive/cmp through my Perl script?

You could do it all within Perl using File::Find.

Or you could fix the shell-out by replacing "find" with "echo", to make
sure that you are giving the program the arguments you think you are giving
it.

Xho
 
A

Anno Siegel

Jim Gibson said:
George Lewycky said:
My problem is trying to delete all files older than 60 days in the
directory /MLIM_data/archive/cmp through a Perl script.
[...]

There is no need to shell out from Perl to find and delete old files:

#!/usr/local/bin/perl
#
use warnings;
use strict;
use File::Find;

my $now = time();
my $then = $now - (60 * 24 * 60 * 60); # 60 days ago
find(
sub {
my $t = (stat)[9]; #mtime
unlink $_ if $t < $then;
},
'MLIM_data/archive/cmp'
);

Nothing wrong with it, but Perl has the -M file test which gives the
file age in days. So (untested)

find sub { -M >= 60 and unlink }, 'MLIM_data/archive/cmp';

should suffice.

Anno
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top