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.
| [reply] |
|
|
| [reply] |
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. | [reply] |
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
| [reply] [d/l] |
|
|
'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
| [reply] [d/l] [select] |
|
|
-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
| [reply] [d/l] [select] |
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? | [reply] |
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?? | [reply] [d/l] |
|
|
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";
| [reply] [d/l] |
|
|
| [reply] |
|
|
|
|
|
|
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 @_?
| [reply] |
|
|
#!/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
| [reply] [d/l] |
|
|
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.
| [reply] [d/l] |
|
|
| [reply] |
|
|
opendir(my $fo, $ARGV[0]);
chdir $fo;
print `du -sk`;
closedir $fo;
__END__
usage:
perl script.pl /home/pandabeer
perl script.pl /etc
| [reply] [d/l] |