LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-13-2012, 11:52 PM   #1
ramyamanickam
LQ Newbie
 
Registered: Sep 2012
Posts: 3

Rep: Reputation: Disabled
Unhappy 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
 
Old 09-14-2012, 03:51 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,457

Rep: Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778
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 {} \;
 
Old 09-14-2012, 04:49 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
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()
 
Old 09-14-2012, 05:35 AM   #4
ramyamanickam
LQ Newbie
 
Registered: Sep 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
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
 
Old 09-14-2012, 05:41 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
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.
 
Old 09-14-2012, 05:47 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,457

Rep: Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778Reputation: 2778
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.
 
Old 09-14-2012, 07:22 AM   #7
ramyamanickam
LQ Newbie
 
Registered: Sep 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete backups older than 7 days through shell script Rk_Raj Linux - Newbie 4 04-09-2013 02:47 AM
Shell script to copy all folders older than 7 days from Linux to Windows by samba blackthu80 Linux - General 4 03-17-2011 12:29 AM
script to compress a directory and then delete older than 7 days replica88 Linux - General 3 02-03-2010 07:21 PM
Automated script to DELETE files older than 2 days in a Particular folder siddhartha_ece2004 Linux - Newbie 14 07-11-2008 05:46 AM
script to auto delete files older than X days nocnoc Programming 17 12-06-2006 08:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration