Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Consider the below code:

my ($location) = @_; print "\n".'Calculating Scan Statistics...'."\n"; my $size =0; $size= `du -x -sk @_`; print "$size";

when i enter the value for @_ through the terminal it shows the same size for / , /home , /root. How can i get the size for diffrent folders and the main / also?

Replies are listed 'Best First'.
Re: Unix size
by moritz (Cardinal) on Feb 07, 2012 at 13:00 UTC

    I'm not sure I understand your question. If you want to get disc usage for several directories, just include them in the argument list.

    If you want to get disc usage reports per partition, use df instead.

      I want the disk usage of the whole / to be saved in $size. But when i pass / i get disk usage for home not the whole /. pls help

Re: Unix size
by choroba (Cardinal) on Feb 07, 2012 at 13:02 UTC
    You should use @ARGV instead of @_ if the parameters are coming from the command line. @_ contains arguments to subroutines.
Re: Unix size
by flexvault (Monsignor) on Feb 07, 2012 at 13:14 UTC

    You may want to man the 'du' command and see if '--total' on your system is correct. 'du' counts files so you need the '*' after the directory.

    perl -e '@size=qx|du --total /home/*|;print $size[$#size];' my result: "1093296 total"

    Good Luck!

    "Well done is better than well said." - Benjamin Franklin

      'du' counts files so you need the '*' after the directory.

      Not true. Under HP-UX, du -s gives the grand total of disk usage for each of the specified name operands.1 The BSD man page for du also includes the -s flag (aka --summary), so it is not quite the same as --total (which BSD also has, but not HP-UX). The total can be obtained with a perl oneliner: perl -ne '@d=split($_); $t += $d[0]; END{print $t}'

      1 - HP-UX 11.11 du man page

      --MidLifeXis

        -s is also in the POSIX standard (IEEE Std 1003.1-2008):
        Instead of the default output, report only the total sum for each of the specified files
        but not --total
Re: Unix size
by JavaFan (Canon) on Feb 07, 2012 at 13:03 UTC
    I cannot reproduce that. Perhaps @_ doesn't contain what you think it does?
Unix size again
by Anonymous Monk on Feb 08, 2012 at 05:51 UTC

    In unix to get the usage of any directory we use the following command at the terminal du -sk / . In this we specify the directory of which we want the disk usage. Now consider the below code..

    my ($location) = @_; print "\n".'Calculating Scan Statistics...'."\n"; my $size =0; $size= `du -sk @_`; print "$size";

    Now when i use this code to access the du command it gives me the disk usage of the root directory irrespective of the directory specified . example @_=/ gives result 150002 , @_ = /usr gives same result 150002 . I want the individual disk usage of every directory ? help??

      my ($location) = @_; $location =~ s{ ([\\\']) }{ '\\' . $1 }gex; # crude escape print "\n".'Calculating Scan Statistics...'."\n"; my $cmd = "du -sk '$location'"; warn $cmd; # let's see what's actually getting executed! my $size = `cmd`; print "$size";

        Its not working gives this when run: du -sk '' at test_2.pl line 5

      Are you sure @_ contains what you think it does? Where do you populate @_? Does what is printed out actually mention the directory you think is in @_?
      Use File::Find:
      #!/usr/bin/perl -l use strict; use warnings; use File::Find; my $size = 0; find( sub { $size += -f $_ ? -s _ : 0 }, shift(@ARGV)); $size = sprintf( "%.02f", $size / 1024 / 1024); print "$size MB"; __END__ returned: 110.11 MB for /etc
        That's wrong.

        Your solution counts files multiple times if there are multiple links to a file. OTOH, you are not counting the size of directories themselves. (Even an empty directory has typically a size of 4096 bytes).

        du takes care of both things.

      Is this question a continuation of Unix size? There is no need to start a new thread if it is.

      --MidLifeXis

      opendir(my $fo, $ARGV[0]); chdir $fo; print `du -sk`; closedir $fo; __END__ usage: perl script.pl /home/pandabeer perl script.pl /etc