Solved

to remove directory recoursive

Posted on 2002-03-05
3
318 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 250 total points
Comment Utility
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 84

Expert Comment

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

Author Comment

by:Talmash
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now