LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   To delete folders older than 14 days in perl script (https://www.linuxquestions.org/questions/linux-newbie-8/to-delete-folders-older-than-14-days-in-perl-script-4175427169/)

ramyamanickam 09-13-2012 11:52 PM

To delete folders older than 14 days in perl script
 
Hi,

I am new to perl script. I need one line command to delete folders. So 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
my $command="`find /D/archieve/Test -ctime +14 -exec rm -rf {} \{\} \`";

Dis code not showing error but not deleting my old folders

pan64 09-14-2012 03:51 AM

Sorry, but I do not understand that line.
my $command="` something `";
should execute that something and the variable command will get the return value of that command. But in your case you want to execute a find and a rm command and do not take care about the returned string.
You have a backslash before the second backtick (\`), therefore it is escaped. So this line will not work that way, because the first ` has no pair.
I do not know what is that \{\} good for. What was your intention?

The most simple way would be: find /D/archieve/Test -ctime +14 -exec rm -rf {} \;

chrism01 09-14-2012 04:49 AM

If you are going to do it that way, you don't need Perl.
Specify dirs only (-type d)
Code:

find /D/archieve/Test -type d -ctime +14 -exec rm -rf {} \;
and I suspect you mean /D/archive/Test

If you are going to use Perl, use it as a programming lang
http://perldoc.perl.org/
and lookup opendir(), readdir(), rmdir()

ramyamanickam 09-14-2012 05:35 AM

Hi

As suggested I tried the below command. But still not working and not showing error. I want to implement unix command in Perl.I need one line command not perl prog.Kindly assist me.
#!/usr/bin/perl
"`find /D/archieve/Test -type d -ctime +14 -exec rm -rf {} \`";



Regards
Ramya M

chrism01 09-14-2012 05:41 AM

That's because you are confusing shell (usually bash on Linux) with Perl.
They are completely different languages.
Decide which one you want to use and stick to it.

This
Code:

#!/usr/bin/perl
is a request to the kernel to use Perl.

The remainder is a shell cmd and does not require quotes; see my example above.

pan64 09-14-2012 05:47 AM

in perl you can execute any shell command by: system($command).
But again, \` is not ok at the and of your code, probably ; is missing or ?? I don't know. Also you not need backticks (`) at all with system.

ramyamanickam 09-14-2012 07:22 AM

system("find /D/Archieve/Test -type d -ctime +14 -exec rm -rf {} \;");.

system("find /D/Archieve/Test -ctime +14 -exec rm -rf {} ");.

Its showing error . How will implement find command in system. I jst need any one liner command to delete folders using perl.


Regards
Ramya


All times are GMT -5. The time now is 06:42 PM.