Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Fastest way to calculate directory size in linux

by Anonymous Monk
on Feb 01, 2012 at 10:51 UTC ( #951174=note: print w/replies, xml ) Need Help??

in reply to Fastest way to calculate directory size in linux

du from GNU coreutils.

  • Comment on Re: Fastest way to calculate directory size in linux

Replies are listed 'Best First'.
Re^2: Fastest way to calculate directory size in linux
by Marshall (Abbot) on Feb 01, 2012 at 11:23 UTC
    I just did some testing with du on my Windows machine. This does "work", but it is not fast - I doubt any faster than File::Find. It visits every file and adds the sizes up - this takes awhile! Its a lot slower than the "properties" click button in the Windows file manager. Satisfying both "fast" and "multi-platform" simultaneously is a pretty tall order.

    maybe if we look at your File::Find code, we can see some improvements?

    Update: This is the fastest way that I know how, that also satisfies "multi-platform". This code runs on my Windows machine.

    #!/usr/bin.perl -w use strict; use File::Find; my $byte_total; my $file_total; find (\&sum_bytes, "C:/temp"); sub sum_bytes { return() unless (-f $File::Find::name); $byte_total += -s _; # not a typo! # "_" different than "$_"! # see [id://951317] $file_total++; } print "total bytes in C:/temp: $byte_total in $file_total files\n"; # prints on my Windows system: # total bytes in C:/temp: 656201485 in 2554 files
    One "expensive" file operation is run for each file system entry. The results of that "file system query", "stat" operation are re-used in a subsequent file operation. This is multi-platform, but not completely optimal for Windows NTFS.

    You can get "fastest" and you can get "multi-platform", but not both together. There is a Windows NTFS way to get this number faster. But this is the "fastest" multi-platform solution of which I am aware.

      File::Find has to visit all files as well... And while I don't claim to have any Windows related knowledge, I do know that on Unix, you'll have to visit all files (or rather, their inodes) to calculate the "size of the directory".

        Indeed, if there was a faster, portable way to calculate the space taken up by a directory, I'm sure that the GNU folk would have coded "du" to use the faster method.

      Thanks for the script but it does'nt give the correct size of the directory(maybe some system files etc issue)

Re^2: Fastest way to calculate directory size in linux
by Anonymous Monk on Feb 01, 2012 at 11:12 UTC

    can'nt that be done by a perl script ??

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://951174]
marioroy At a large firm, had to awaken a security guy a few times so that nobody would catch him sleeping.
marioroy On a large dairy farm, almost went off the road with a 10 wheeler from exhaustion. On the farm, almost went off the silo with a big tractor. At the Fransiscan monastery, almost slipped off the dome while painting it.
[marioroy]: In Saudi Arabia caught fire but didn't burn. I think angels exist.
Lady_Aleena clean up after dinner finally.
[Discipulus]: uch! so we are twice lucky to have MCE..;=)
[marioroy]: Working a midnight graveyard shift, on the way home ran out of gas. just 2 minutes later somebody stops with a small tank of gas. It's wierd. There's always someone nearby for some reason.
[Lady_Aleena]: I was the one who stop[ed in the middle of a cold night to pick up a small family walking to a gas station. I got a few gallons of free gas for the round trip, and the grandmother said she'd pray for me.
[marioroy]: Folks do not know it, but MCE was ridiculously difficult. At Niagra Falls, heard a voice on enabling Grace while in the mist boat. That 10x performance is possible.
[marioroy]: Lady_Aleena Angels on earth are real people. That's awesome.

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2017-05-29 08:26 GMT
Find Nodes?
    Voting Booth?