• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

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
0
Talmash
Asked:
Talmash
1 Solution
 
chrisoyhCommented:
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
 
ozoCommented:
use File::Path;
rmtree(['/full/directory/path'], 1, 1);
0
 
TalmashAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now