Solved

to remove directory recoursive

Posted on 2002-03-05
3
322 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
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 84

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

914 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

22 Experts available now in Live!

Get 1:1 Help Now