CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
How do I delete a file when diskfull?



Jan 11, 2000, 12:12 PM

Post #1 of 6 (2904 views)
How do I delete a file when diskfull? Can't Post

I am using Solaris, and I need a script to delete a file when the disk is full. The files are listed in numerical order, and when disk space is low, it should delete the oldest file.

I was trying to use df -k and comparing disk space usage and deleting, but it doesn't work.



Jan 11, 2000, 4:50 PM

Post #2 of 6 (2904 views)
Re: How do I delete a file when diskfull? [In reply to] Can't Post

I have never done this but I did a simple pipe open on df and I got this.
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

Filesystem 1k-blocks Used Available Use% Mounted on
/dev/hdb1 5890092 2187600 3403284 39% /
/dev/hda1 7158976 2541688 4617288 36% /win</pre><HR></BLOCKQUOTE>
Here is how I called the program:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

open DF, "/bin/df|" or die "Broken pipe: $!";
while (<DF> ) {print}</pre><HR></BLOCKQUOTE>
Of coarse you are not going to just print it. All you have to do at this point is parse the input from df.
If you need help with a way to parse it let me know and I will see what I can come up with.



Jan 12, 2000, 5:33 AM

Post #3 of 6 (2904 views)
Re: How do I delete a file when diskfull? [In reply to] Can't Post

Hi, Thanks!

Yes, a way to find the larger and smaller disk spaces would be a great help... the code above is about where I got to.. and since I'm not too familar with the other coding, this is where I'm stuck.

I got the line "ls -p|egrep '/'|head -n 1" to get the directory of the smallest directory. But I don't know how to use the rmdir to get rid of it if space is low.

Thanks for replying!


Jan 12, 2000, 6:01 AM

Post #4 of 6 (2904 views)
Re: How do I delete a file when diskfull? [In reply to] Can't Post

I'm sorry I don't quite understand what you are wanting to do. Do you want to see what Partition is full and then delete stuff from that partition? Do you want to delete specified directory trees? If so based on what?
I am easily confused Smile


Jan 12, 2000, 11:12 AM

Post #5 of 6 (2904 views)
Re: How do I delete a file when diskfull? [In reply to] Can't Post


Sorry for the confusion. What I want is this:

I am trying to create a script that will run on a cron job. This is on Solaris 2.6, and PerL is installed. What I need is something that can check if the /log (1 gig) directory is more than 90% full. If it is, then it should remove the smallest directory.

The directories are named after the date the logs are created in the format yyyymmdd. An example is 19991201.

What I have now is this line:

ls -p| egrep '/'|head -n 1

This gets me the name of the smallest directory. I also am trying to make PERL script, since I have no idea if there is a function to check for low disk space in Unix.

I also have:
df-v which list the utilization, and I can grep the line that I need.

But if you have any other suggestions, or a script that I can use, it would be great!

Thanks,I would really appreciate any help!


Jan 13, 2000, 6:33 PM

Post #6 of 6 (2904 views)
Re: How do I delete a file when diskfull? [In reply to] Can't Post


Here is something that may help. Please understand this is not tested code...
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

#!/usr/bin/perl -w

use strict;
use vars '%size';
# This is a lazy way to recurse and
# you may have to install this module
use File::Recurse;

open DF, "/bin/df|" or die "Broken pipe: $!";
my $used;
while (<DF> ) {
if (m| (\d+)% +/log$|) {
$used = $1;
close DF or warn "Pipe closed prematurly: $?";

if ($used >= 90) {
opendir LH,'/log' or die "Could not open /log: $!";
my @logs = grep /\d{8}/, readdir LH;
closedir LH;

# I do not know of a way to get the size of an
# entire directory in perl without recursing it.
# There may be a way I do not know of. The two
# ways I know are to either recurse it our self
# or use /usr/bin/du. In this example we will
# do it our self

# If someone knows a better way to do this
# please let me know
recurse(\&add,"/log/$_",$_) for @logs;
sub add { $size{$_[1]} += (-s) if (-f) }
my @sortedkeys = sort { $size{$a} <=> $size{$b} } keys %size;

# OK now we need to remove the directory recursivly
# again I know of no built in way to do this so we
# can either recurse is our self or use /bin/rm -r
# Here I will use /bin/rm -r sence it is a common program
system('/bin/rm','-r',"/log/$sortedkeys[0]") == 0
or die "System /bin/rm -r /log/$sortedkeys[0] failed: $?";


Please Please test this before you use it.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives