Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

to remove directory recoursive

Posted on 2002-03-05
3
Medium Priority
?
366 Views
Last Modified: 2008-02-01
hi .

I wrote this script (it's a block from the script which
doesn't work properly)

the goal :
1) before creating NEW directory ,
I want to look for OLD directories (2 days before) and rm
them automatically .
2) I want to have to option to change the OLD declare :
     from 2 days to 1 day only .
     this is up to the quota status
*** how do I get quota report on my stations :
solaris :
 10:59{97}galileo184:nothing> df -k | grep $HOST | head -1
/export/home/galileo184 5241858 1283716 3905724    25%    /home/galileo184
linux :
10:58{40}galileo189:FC_11_2_2> df -k | grep $HOST | head -1
/export/home/galileo189 11516540 8992514 2408861    79%    /home/galileo189

*** I need to get the number before the "%" sign ,
and if <number> > 90 then OLD = 1 day .

##########################

#!/usr/local/bin/perltk -w
...    
if ($date_hash{'DAY'} > 2) {
     $tow_days_ago = $date_hash{'DAY'}-2;
     print "Nrun_linux.prl : tow_days_ago = $tow_days_ago , date_hash = $date_hash{'DAY'} \n";
} else {
     $tow_days_ago = 0;
}

...
    # Create DUMP Dir for ncsim
    $dump_dir = "/dumps/".$hostname."/fox";
    if (!(-e "$dump_dir")) {
     $mkdir_res = system("mkdir -p $dump_dir");
     system("chmod 777 $dump_dir");
    }

    if ($tow_days_ago > 2) {
     print "Nrun_linux.prl LOOKING FOR OLD ncsim's to remove for quota saving \n";

     opendir DIRECTORY, "$dump_dir" or die "Cannot Open dumps directory : $!";
     @allfiles = grep /$user/, readdir DIRECTORY;
     @allfiles = grep /$tow_days_ago/, @allfiles;
     
     print "Nrun_linux.prl files located = @allfiles \n";
     
     foreach $file (@allfiles) {
         unlink("$dump_dir/$file/ncsim.dsn");
         unlink("$dump_dir/$file/ncsim.trn");
         rmdir ("$dump_dir/$file");
     }

#     system("\ls  $dump_dir/ | grep ${user} | grep ${tow_days_ago}");
#     system("\rm -r $dump_dir/*$user*$tow_days_ago*");

    closedir (DIRECTORY);

    } elsif ($tow_days_ago == 2) {
     # rm ncsim of former month
    }


#########

what's wrong ?

at the run I get :
Nrun_linux.prl LOOKING FOR OLD ncsim's to remove for quota saving
Nrun_linux.prl files located =  
...
and if there is somthing it didn't removed .

comment : the files are permissioned to removed only by the $USER , and he is the one who use the script .

tal
0
Comment
Question by:Talmash
3 Comments
 

Accepted Solution

by:
chrisoyh earned 1000 total points
ID: 6895734
Hi Talmash,

If you want your Perl script to remove a directory, you can try this:

$result = qx[rm -rf /full/directory/path];

You might want to print out $result though, to see is there anything in stdout and stderr.

rgds,
chrisoyh
0
 
LVL 85

Expert Comment

by:ozo
ID: 6895744
use File::Path;
rmtree(['/full/directory/path'], 1, 1);
0
 
LVL 6

Author Comment

by:Talmash
ID: 6979945
hi ozo/chris

finally I managed to solve it , slowely but surely :

somehow ozos' line didn't worked as chris line .

thanks both ,

points to chris .

Tal

the fixed code :


    if ($tow_days_ago > 2) {
     print "Nrun.prl LOOKING FOR OLD ncsim's to remove for quota saving location = $dump_dir \n";
     print "\n $user \n";
     opendir DIRECTORY, "$dump_dir" or die "Cannot Open dumps directory : $!";
     @allfiles = grep /\.$user\./, readdir DIRECTORY;
     
     printf "Nrun.prl files located = @allfiles \n";
     foreach $file (@allfiles) {
         if ($file =~ /ncsim.shm.*.$user.(\w+)/) {
          $tst_date = $1;
          printf "\n file $file , date = $tst_date ";
          if ($tst_date =~ m/(\d+)(\w{3})/) {
              $day = $1;
              $month = $2;
              printf "day = $day , month = $month \n";
              if (($day <= $tow_days_ago)    ||         # same month older
               ($day > $date_hash{'DAY'}) ||          # other month
               ($month ne $date_hash{'MONTH'}) ) {     # other month
               printf "ENTERED the IF \n";
               $result = qx[rm -rf $dump_dir/$file];   # given by chris
#               rmtree(['/full/directory/path'], 1, 1); # given by ozo at EE talm 30/Apr/2002
              }
             
          } else { print "\n";}
         }
     }
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question