trouble with this code in deleting files older than 60 days

Discussion in 'Perl Misc' started by George Lewycky, Jul 22, 2005.

  1. 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
     
    George Lewycky, Jul 22, 2005
    #1
    1. Advertising

  2. George Lewycky wrote:
    > 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
     
    John W. Krahn, Jul 22, 2005
    #2
    1. Advertising

  3. George Lewycky

    Guest

    "George Lewycky" <> wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jul 22, 2005
    #3
  4. George Lewycky

    Guest

    "George Lewycky" <> wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jul 22, 2005
    #4
  5. George Lewycky

    Anno Siegel Guest

    Jim Gibson <> wrote in comp.lang.perl.misc:
    > In article <>,
    > George Lewycky <> wrote:
    >
    > > 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
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Jul 25, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kbass
    Replies:
    2
    Views:
    1,797
    Graham Fawcett
    Nov 13, 2003
  2. Sameer
    Replies:
    6
    Views:
    331
  3. pilgrim773
    Replies:
    2
    Views:
    1,853
    Giampaolo RodolĂ 
    May 19, 2010
  4. Clement Ow
    Replies:
    7
    Views:
    224
    Clement Ow
    Apr 22, 2008
  5. SteveO
    Replies:
    12
    Views:
    351
    Tad McClellan
    Mar 7, 2006
Loading...

Share This Page