4

Updated According to comments:

I have outlog.txt file which contains multiple filenames, e.g: 2345_535_Dell&HP_3PAR_DEAL.txt, similarly there are many filename but not the actual folder where the files are located and so in code am appending filenames to folderpath to get actual file location. Now,I want to get disk usage of all the files present in outlog.txt and also total disk usage of all files present in outlog.txt.

I have tried two approaches perl -s and my ($size) = split(' ', du `"$folderpath/$_"`) but both approaches are giving me different values and also when I am using du than am getting some numeric value but it does not give me unit, is there a way I can get human readable without using -h option as it is not working on my system ?

Background Information

My goal is to get the size of a file, and currently I am using perl -s to get filesize. I have also tried du and am getting different values for the size of the same file. I am not able to understand how this works.

Q: Why do du and perl -s give different values for size? How do they internally work? Which of the two different values is the more accurate one? Also, I'm not sure why du -h filename gives me an illegal expression error:

bash-2.03$ du -h test.txt
/usr/bin/du: illegal option -- h
usage: du [-a][-d][-k][-r][-o|-s][-L] [file ...]

Code:

my $folderpath = 'the_path';
open my $IN, '<', 'path/to/infile';
my $total;
while (<$IN>) {
    chomp;
    my $size = -s "$folderpath/$_";
    print "$_ => $size\n";
    $total += $size;
}
print "Total => $total\n";

Courtesy: RickF

Update:

Q: How can I get the disk usage value instead of file size for each file present, meaning how can I get du value for each files rather than perl -s values for file ?

OS Information uname :SunOS uname -v :Generic_117350-39

Updated Code: According to brain's approach but still du value prints as zero only and not the actual value, any suggestions ?

Update: If I use my ($size) = split(' ', du "$folderpath/$_"); than I am getting du value but it gives me some number, how can I get it into human readable without using -h option ?

 #!/usr/bin/perl
 use strict;
 use warnings;

my $folderpath = '/Project/upload';
open my $IN, '<', 'outlog.txt';
my $total;
while (<$IN>) {
    chomp;
    #my( $block_size, $blocks ) = ( stat( "$_" ) )[11,12];
    #my $du_size = $block_size * $blocks;
    my ($size) = split(' ', `du "$folderpath/$_"); 
    #my $size = -s "$folderpath/$_";
    print "$_ => $size\n";
    $total += $size;
}
print "Total => $total\n";
Community
  • 1
  • 1
Rachel
  • 100,387
  • 116
  • 269
  • 365

4 Answers4

8

du reports actual disk usage, Perl's -s reports the size of the file. So, if a file is four bytes long it will have a size of four bytes, but disk usage of four kilobytes (depending on how your filesystem is setup).

You will also see a difference in the sizes of sparse files. Sparse files take up less space than they claim to have in them.

Chas. Owens
  • 64,182
  • 22
  • 135
  • 226
4

By default, du displays the number of blocks used by a file (where each block is 512 bytes on most systems), while perl's -s displays bytes.

As to why your copy of du doesn't have a -h option, you don't tell us what operating system you're using; it appears to include a horribly outdated version of the program.

UPDATE: to get disk usage in perl, you can use the Filesys::DiskUsage module.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
Wooble
  • 87,717
  • 12
  • 108
  • 131
3

du stands for "Disk Used", and reports the physical size of the file on disk. If the file is sparse, this may be much smaller than its logical size, which is what -s reports. Both are "accurate", they're just measuring different things.

The error message indicates that the version of du installed on your machine doesn't understand the -h option.

cjm
  • 61,471
  • 9
  • 126
  • 175
  • I found this response confusing in light of [Wooble's](http://stackoverflow.com/questions/3773017/why-do-du-and-perls-s-give-different-values-for-the-file-size/3773074#3773074), if `du` is showing *blocks* (512 *bytes*), that the number is smaller because one *block* can fit 512 *bytes*, not because the file is *sparse*? Unless I'm missing something. – Evan Carroll Sep 22 '10 at 20:27
  • 1
    @Evan Carroll, `du` is capable of reporting sizes in different units. I'm not sure what the default unit is for SunOS. Try `man du`. – cjm Sep 22 '10 at 21:24
1

If you want du to give the same results as Perl's -s, try du -b. If your du supports it, this gives the "apparent size", which is different from disk usage, as others have stated.

But to do this you'll have to update your du.

Update for OP's updated code: Make sure that the file exists within your current working directory. You may have to prepend the directory to make sure that Perl is finding the file.

It may also clarify things if you get away from using $_ everywhere:

while( my $line = <$IN> ) {
  chomp $line;
  my( $block_size, $blocks ) = ( stat( $line ) )[11,12];
  ...
}

This way you're safe from unintentional changes to $_.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
CanSpice
  • 34,814
  • 10
  • 72
  • 86
  • 1
    Evan, the `defined` isn't necessary. When we hit the end of the file, `<$IN>` returns `undef`, which gets assigned to `$line`. The return value of the assignment is thus also `undef`, which gets translated to false, and the `while()` loop exits. This is why I've rolled back your edit that added the `defined` check. – CanSpice Sep 22 '10 at 21:12
  • @CanSpice it is necessary, if you ever get a file called 0, or " " you'll stop execution (not that it really happens too much, but you're leaving your result dependent on the perl truth table, rather than eof()) – Evan Carroll Sep 22 '10 at 21:20
  • 1
    @Evan: please see http://stackoverflow.com/q/3773917/40468, and read http://perldoc.perl.org/perlop.html#I/O-Operators. – Ether Sep 22 '10 at 22:24
  • @Ether: You're right, but the Perl docs are misleading. They say, "If **and only if** the input symbol is the only thing inside the conditional of a while statement" -- but later contradict the "and only if" part by showing that `while (my $line = )` also behaves the same way. Leaving us wondering about exactly which circumstances this DWIMmery will be performed in. – j_random_hacker Sep 22 '10 at 22:44
  • @j_random: thanks; that's something we can get clarified in the next release of the perldocs. – Ether Sep 22 '10 at 23:04
  • @j_random: I just noticed that you (and then I) left this comment on the wrong question. :) – Ether Sep 22 '10 at 23:16