Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Well, this isn't much of a Perl script -- it's mostly a Perl wrapper around a shell command. That might be OK, but as you're finding out, it has limitations. You can either try to anticipate any strangeness (and changes) in the shell output, or you can eliminate the shell altogether by using File::Find.

I wrote this little tool that lists the top $ARGV[0] largest files found at or below the current directory. This was written for Windows, so it doesn't do symlink exclusion (you'll want to add that, or symlinks will be reported as large files), and I haven't bothered to do "pretty-printing" of directory sizes in KB/MB -- all sizes are in bytes.

This is by no means fantastic code, but it works; and, with a touch of mod, it will work the way you want it to:

#!/usr/bin/perl -w use File::Find qw/find/; use File::Spec; my @Files; my $total_size; find(\&wanted, File::Spec->curdir()); @Files = sort_size(@Files); my $limit = @ARGV ? $ARGV[0] : @Files; for (@Files[0..$limit-1]) { printf "%-60s : %d\n", $$_{-name}, $$_{-size} } print "Total size: $total_size\n"; sub wanted { my @stat = stat; #~ print STDERR "$File::Find::name\n" unless $stat[7]; return unless $stat[7]; my %file = ( -name => $File::Find::name, -size => $stat[7] ); $total_size+=$stat[7]; push @Files,\%file; return 1; } sub sort_size { my $desc = $_[0] ? shift : 0; shift unless $desc; my @array = @_; if ($desc) { @array = sort { $$b{-size} <=> $$a{-size} } @array; } else { @array = sort { $$a{-size} <=> $$b{-size} } @array; } return @array; }

Anima Legato
.oO all things connect through the motion of the mind

In reply to Re: drive space script dies on certain characters... by legato
in thread drive space script dies on certain characters... by stevenrh

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2023-09-27 08:17 GMT
Find Nodes?
    Voting Booth?

    No recent polls found