Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

To Delete folders older than 14 days.-Want one liner command

by rrrrr (Novice)
on Sep 14, 2012 at 05:00 UTC ( #993639=perlquestion: print w/replies, xml ) Need Help??
rrrrr has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have tried using find command in perl script to delete the folders older than 14 days. Its not showing error but not deleting the folder. Kindly assist me.The below code not showing error but not deleting my old folders

 my $command="`find /D/archieve/Test -ctime +14 -exec rm -rf {} \{\} \`";

Replies are listed 'Best First'.
Re: To Delete folders older than 14 days.-Want one liner command
by kcott (Chancellor) on Sep 14, 2012 at 08:14 UTC

    G'day rrrrr,

    Welcome to the monastery.

    Firstly, I'd recommend using something like ls -l instead of rm -rf while you're testing: avoid accidentally deleting the wrong files.

    Backticks (`...`) are interpolating quotes in Perl - you don't need the surrounding double quotes. To identify directories, use -type d in the find command. I'm not sure what \{\} \ is doing: I'd use \\; in that position.

    Putting all that together, this works for me:

    my $command = `find . -type d -ctime +14 -exec ls -l {} \\;`;

    Unless you want to capture the output from the find command, consider using system instead of backticks.

    You also might like to take a look at Perl's file test operators: -M, -A and -C.

    -- Ken

      Kcott thanks for ur reply. I tried as u suggested but below code showing parameter not correct error. Kindly help me . And I used ls-l its not recognized. Regards rrrrr

      #!/usr/bin/perl my $command=`find D/Archieve/Test -type d -mtime +14 -exec rm -rf {} \ +\;`; system $command; print $command;

        ls is one of the very first *nix commands one learns. By writing ls-l, and not seeing why that is wrong, you give the impression that you have virtually no *nix knowledge. I'd seek out a tutorial before proceeding further - perhaps ask your system administrator. You may end up doing irreparable damage using dangerous commands such as rm -rf when you don't really appear to know what you're doing.

        I suggested using system instead of backticks; not in addition to backticks. I took the time to provide you with a link to the system command; perhaps you could take the time to read the documentation it points to.

        You originally showed this path /D/archieve/Test; now you show D/Archieve/Test. (I'm even wondering whether that should be Archive or archive in the middle of the pathname.) Find out what the real pathname is before deleting directories and all their contents recursively and forcefully!

        -- Ken

Re: To Delete folders older than 14 days.-Want one liner command
by Anonymous Monk on Sep 14, 2012 at 05:31 UTC
Re: To Delete folders older than 14 days.-Want one liner command
by BillKSmith (Priest) on Sep 14, 2012 at 14:46 UTC

    I do not think that "one liner command" should ever be a requirement. The are often very convenient. Use them only when they are!


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://993639]
Approved by Ratazong
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2017-04-27 06:38 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (501 votes). Check out past polls.