Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Unix size again

by Anonymous Monk
on Feb 08, 2012 at 05:51 UTC ( [id://952410]=note: print w/replies, xml ) Need Help??


in reply to Unix size

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??

Replies are listed 'Best First'.
Re: Unix size again
by tobyink (Canon) on Feb 08, 2012 at 06:51 UTC
    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

        Then $location is not what you think it is. Debug your script.

        Well, then that means that $location is an empty string, or perhaps undefined. So the problem lies elsewhere in your code. Post more of the script.

Re: Unix size again
by JavaFan (Canon) on Feb 08, 2012 at 07:51 UTC
    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 @_?
Re: Unix size again
by Khen1950fx (Canon) on Feb 08, 2012 at 07:58 UTC
    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.

Re: Unix size again
by MidLifeXis (Monsignor) on Feb 08, 2012 at 13:41 UTC

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

    --MidLifeXis

Re: Unix size again
by pvaldes (Chaplain) on Feb 08, 2012 at 12:26 UTC
    opendir(my $fo, $ARGV[0]); chdir $fo; print `du -sk`; closedir $fo; __END__ usage: perl script.pl /home/pandabeer perl script.pl /etc

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://952410]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-16 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found