Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Just another Perl shrine
 
PerlMonks  

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 {} \{\} \`";

Comment on To Delete folders older than 14 days.-Want one liner command
Download Code
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 kcott (Abbot) 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 BillKSmith (Hermit) 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!

    Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://993639]
Approved by Ratazong
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-04-21 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (493 votes), past polls