Link to home
Start Free TrialLog in
Avatar of Talmash
TalmashFlag for Israel

asked on

to remove directory recoursive

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
ASKER CERTIFIED SOLUTION
Avatar of chrisoyh
chrisoyh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
use File::Path;
rmtree(['/full/directory/path'], 1, 1);
Avatar of Talmash

ASKER

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";}
         }
     }