Someone asked a question yesterday on how he could go about replacing du(1) with perl. The answer I gave him was perl -MFile::Find -le 'find( sub { $size += -s }, "."); print $size'. He went away happy, but then half an hour later came back and said the the result returned by that was not the same as that of du(1). So after a little tweaking and testing I came to the very clear conclusion that I really didn't understand why it was doing what it was.

I came on here and asked in the chatterbox, and got some very good responses including one from tye but the problem was it all went over the top of my head and I basically didn't understand any of what he was explaining :(

Along with the explanation tye came up with this little bit of code perl -MFile::Find -le 'find( sub { $size += int( (511 + -s $_)/512 ) }, "."); print 512*$size' which is meant to produce results closer to du(1).

When I also went to test it on files with du -b * the filesizes returned by it was also not the same as that returned by -s or from ls -l.

So if anyone could please explain it into newbie terms I would be greatly appreciative. Thanks.


In reply to du and -s by rendler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.