At 05:04 PM 4/13/01 -0700, Paul Fontenot wrote:
>Here is the actual code and output, I am allready using system, but was
>hoping for a perl only solution.

Fair enough.  I'll point you in the right direction, then.  Up to you to 
read the documentation and fill in the details, okay?

Since I don't see something out there that already does what you want, 
you'll need to use File::Find to recurse over directories to do what du 
does.  Something like

use File::Find;

# Put home directory in $home
$disk_space = du($home);

sub du {
   my $total = 0;
   find( sub { $total += -s if -f; return 1 }, shift);
   return $total;
}

This subroutine will return the number of bytes in regular files under the 
directory path passed into it.  To understand it, you'll need to read these 
pages:

File::Find
perlsub
perlref
perlfunc  - look for -X functions

Now, as to your program:

>#!/usr/bin/perl -w


Always program with "use strict".

>#
>#
>open(PASSWD, "/etc/passwd");
>
>while (<PASSWD>) {
>         @fields = split /:/;

Predeclare variables with 'my' (use strict will make this more or less 
essential).

>         if ($fields[2] >= 500) {
>                 chdir $fields[5];
>                 $disk_space = system("du -s .");

system() runs the command, it doesn't save the output.  That's why it 
printed the figure out early and instead in your formatted output you got 
the return code of system (0) instead of the du output.  You need backticks 
(``).  See perlop, "Regexp Quote-Like Operators".

>                 write;
>         }
>}
>close(PASSWD);
>
>format STDOUT_TOP=
>
>                                 Disk Usage Stats
>===============================================================================
>
>Name                    User Name       Home directory          Disk usage
>.
>
>
>format STDOUT=
>@<<<<<<<<<<<<<<<<<      @<<<<<<<<       @<<<<<<<<<<<<<<         @####.## Mb
>$fields[4],             $fields[0],     $fields[5],             $disk_space
>.
>
>And this is what the output looks like:
>
>421676  . <--- This should be under the disk usage column
>
>                                 Disk Usage Stats
>===============================================================================
>
>Name                    User Name       Home directory          Disk usage
>Paul Fontenot           paul            /home/paul                  0.00 Mb
>18132   . <--- So should this
>Heather Fontenot        heather         /home/heather               0.00 Mb
>
>Thanks for any formatting help.
>
>-Paul
>
>On Friday 13 April 2001 16:56, you wrote:
> > At 04:02 PM 4/13/01 -0700, Paul Fontenot wrote:
> > >Hello,
> > >         How can I get a summary of the file sizes in a directory with
> > > perl? Like a
> > >"du -s ." ?
> >
> > du walks the directory tree summing all the components in subdirectories;
> > Perl code to do that is going to be more than a few lines.  Any reason you
> > can't do
> >
> >   system("du -s .")
> >
> > ?  Otherwise you've got some work ahead of you with File::Find, since I
> > can't see any module on CPAN for this.
> > --
> > Peter Scott
> > Pacific Systems Design Technologies
>
>--
>   4:59pm  up  7:53,  3 users,  load average: 1.66, 1.57, 1.73

--
Peter Scott
Pacific Systems Design Technologies

Reply via email to