getting directory size 
Author Message
 getting directory size

I recently wrote a perl program to tell me which users of ours have web
pages in there directories, and if so, how much space it was taking up.
Now I know my code is very sloppy, please forgive.  What I am trying to
accomplish is to get it to go faster.......

I am looking for a native perl function or library that will have
something similar to the "du" command in it so I can get directory
sizes.  Also feel free to critisize my code and tell me any other places
I am doing something wrong and could make it better/faster.

When you have to check several thousand users directories its nice to be
able to make something go faster:

#!/usr/bin/perl

$hasweb=0;
chdir "/home/cust";
while(<*>) {
    $currentdir=$_;
    chdir $currentdir;
    while(<*>) {
        tr/A-Z/a-z/;
        if(/htm/ || /HTM/) {
            $size=`du -bs`;
            $size=~s/\.//;
            chop($size);
            $total=$total+$size;
            $percent=(($size/6000000)*100);
            printf "%s\t%s\t%d%%\n",$currentdir,$size,$percent;
            last;
        }
    }
    chdir "..";

Quote:
}

closedir DIR;
print "Total space used: $total\n";

--

|Brian Feeny              | USR TC Hubs |ShreveNet Inc. (318)222-2638 |
|Systems Administrator    | Perl, Linux |Web hosting, online stores,  |
|ShreveNet Inc.           |  USR Pilot  |Dial-Up 14.4-56k, ISDN & LANs|
|89 CRX DX w/MPFI, lots of|-=*:Quake:*=-| http://www.*-*-*.com/ ;     |
|mods/Homepage coming soon|LordSignal/SN|Quake server: 208.206.76.3   |
\------------------------318-222-2638 x109----------------------------/



Tue, 06 Jun 2000 03:00:00 GMT  
 getting directory size

Quote:

> sizes.  Also feel free to critisize my code and tell me any other places
> I am doing something wrong and could make it better/faster.

You should use File::Find. There might be a problem with chdir '..' --
if you came through symlink, you might get back somewhere else.

--
------------------------------------------------------------------------

                   I can take or leave it if I please
------------------------------------------------------------------------



Wed, 07 Jun 2000 03:00:00 GMT  
 getting directory size

Quote:

> I am looking for a native perl function or library that will have
> something similar to the "du" command in it so I can get directory
> sizes.  

Maybe you want to use stat, or the filetests like -s?

Quote:
> chdir "/home/cust";

It's probably good to check that this chdir succeeded. Or, better, avoid
it altogether - there's no pressing reason that a directory need to be
your current working directory if you're merely examining its contents.
(This assumes that you'll avoid using du, or other external programs, of
course. But if you want it to be faster, avoiding externals is a good
idea.)

Quote:
> while(<*>) {

I'd use readdir instead of a glob. It won't forget about the dot files,
and it will be faster, since it won't take extra time sorting the list of
names.

Quote:
>         if(/htm/ || /HTM/) {

Maybe you mean to use /\.html?$/i there. You're not interested in a file
named HTML.txt, are you? :-)

Quote:
>             $percent=(($size/6000000)*100);

It's probably better to not hardcode a constant like that. I'd use
something like this at the top of the script...

    use constant WHATEVER => 6_000_000;

...except I'd use a better name than WHATEVER, one which would make it
clear(er) what's going on here. Say, STANDARD_QUOTA or something.

Quote:
>             printf "%s\t%s\t%d%%\n",$currentdir,$size,$percent;

Coming from a C programming background, are you? :-)  It's better to use
print instead of printf in general. First, round off the value of
$percent, if needed, then you can use this.

        print "$currentdir\t$size\t$percent%\n";

Quote:
> closedir DIR;

How'd that get in there? :-)

Hope this helps!

--
Tom Phoenix           http://www.teleport.com/~rootbeer/

Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/
              Ask me about Perl trainings!



Wed, 07 Jun 2000 03:00:00 GMT  
 getting directory size

[original author automagically cc'd via e-mail]

Quote:

>I recently wrote a perl program to tell me which users of ours have web
>pages in there directories, and if so, how much space it was taking up.
>Now I know my code is very sloppy, please forgive.  What I am trying to
>accomplish is to get it to go faster.......

>I am looking for a native perl function or library that will have
>something similar to the "du" command in it so I can get directory
>sizes.  Also feel free to critisize my code and tell me any other places
>I am doing something wrong and could make it better/faster.

The combination of stat() and File::Find ought to do what you need.

Jeremy
--


Wood County Free Net (Ohio)       http://www.wcnet.org/



Thu, 08 Jun 2000 03:00:00 GMT  
 getting directory size



[snip]

Quote:

>The combination of stat() and File::Find ought to do what you need.

The script below will work when called from an html, which will give you
a live directory listing with size count. If called from a crontab it
will generate an html/stm file.

Butcher and enjoy!

#!/usr/local/bin/perl -w
use File::Find;
#
# This is the location of your homepages on the server
$startloc = '/usr/www/foobar';
$homestart = '/usr/home/foobar';

# This variable will contain the webpage that called
# it or by default crontab
$referer = 'crontab';
$sitesize = 0;
$referer = $ENV{'HTTP_REFERER'} if $ENV{'HTTP_REFERER'};

if (index($referer, 'http://www.shadow.org.uk/developer/') == 0) {
        print "Content-type: text/html\n\n";

Quote:
} else {

        # Path and name of actual sitemap.html generated by this script
        $sitemap = '/usr/www/foobar/path/sitemap.stm';
        open(MAP,">$sitemap") or die "Error opening $sitemap: $!";
        select(MAP);

Quote:
}

print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n" .
      "<html>\n" .
      "<head>\n" .
      "<title>MArk's or Marks or Marks': Site Map</title>\n" .
# Help me out here Rhodri:-)
      "</head>\n\n" .
      "<body background=\"/images/bg-wallpaper.gif\">\n";

print "<p>\n";
print "<center>\n";
print "<H2><font color=#0000f0>Site Map</font></H2>\n";
print "</center>\n";
print "<HR SIZE=6>\n";
print "</p>\n";

print "This was generated by/from: $referer\n<br><br><hr>\n";

print "<H3>WWW directory</H3>\n";
print "<UL>\n";
find (\&tell_time, $startloc);  # Call tell_time for every file under
                                # current directory
print "</UL>\n\n<br><br>\n\n";

if (index($referer, 'crontab') == 0) {
        print "<H3>HOME directory</H3>\n";
        print "<UL>\n";
        find (\&tell_time, $homestart);
        # Call tell_time for every file under
        print "</UL>\n";
        }

print "Total Used: $sitesize\n";

print "</BODY>\n</HTML>\n";

if (index($referer, 'URL/path/') == 0) {
        close (MAP);
        }

sub tell_time {
        return if (-d $_);      # Not if it is a directory
        return if (-l $_);      # Not if it is a symbolic link
        return if (-z $_);      # Not if it is a zero length file

        # Now Subtract our start path to give us the full URL minus
        # the http part ofcourse
        $urlpath = substr($File::Find::name, length($startloc)+1,
(length($File::Find::name) - length($startloc))-1);

        print "    <li><a href=\"/" . $urlpath . "\">$urlpath</a> ", (-s
$_) , " bytes\n";
        $sitesize = $sitesize + (-s $_);

Quote:
}

--
Mark Worsdall (Webmaster) - WEB site:- http://www.shadow.org.uk


Any opinion given is my own personal belief...


Sun, 11 Jun 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Getting directory size on NT

2. Getting NTFS Directory structure size

3. CGI.pm Bug: file upload file size mismatch when file size is close to buffer size

4. Getting Traffic Size in Bytes from W3C Extended Format Log

5. Getting partition size from perl script

6. getting object size?

7. Getting Remote File Size

8. HELP! Getting Remote File Size

9. Getting File Size

10. Getting the size of the mail queue.

11. Getting image size

12. problem getting file size in forked code

 

 
Powered by phpBB® Forum Software