Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I once wrote a script much like the one above.  du -b "$@" | sort -n is nice, but du -h is nice too, so I had a shell script for du -b "$@" | perl -pe's/ome/complicated/regex' | sort -n for a while.

Recently, I rewrote it in pure Perl.

#!/usr/bin/perl -w use strict; use File::Find; my %conf = (a => 0, c => 0, s => 0, x => 0); my @dirs = (); while (defined ($_ = shift)) { if ($_ eq "--") {push @dirs, @ARGV; last} elsif (/^-(.*)$/s) { for (split //, $1) { if ($_ eq "a" and !$conf{s}) {$conf{a} = 1} elsif ($_ eq "c") {$conf{c} = 1} elsif ($_ eq "s" and !$conf{a}) {$conf{s} = 1} elsif ($_ eq "x") {$conf{x} = 1} else { print STDERR "$0 [-a] [-c] [-s] [-x] [--] ...\n"; exit 1; } } } else {push @dirs, $_} } s/\/*$//s for @dirs; @dirs = qw(.) unless @dirs; my %spec = (no_chdir => 1); if ($conf{a}) { $spec{wanted} = sub { stat; my $s = -f _ ? -s _ : 0; $File::Find::name =~ /^\Q$dirs[0]\E\/?(.*)$/s; my @a = split /\//, $1; for (unshift @a, $dirs[0]; @a; pop @a) { $_{join "/", @a} += $s; } }; } elsif ($conf{s}) { $spec{wanted} = sub { stat; $_{$dirs[0]} += -f _ ? -s _ : 0; }; } else { $spec{wanted} = sub { stat; my $s = -f _ ? -s _ : 0; $File::Find::name =~ /^\Q$dirs[0]\E\/?(.*)$/s; my @a = split /\//, $1; ! -d _ and pop @a; for (unshift @a, $dirs[0]; @a; pop @a) { $_{join "/", @a} += $s; } }; } if ($conf{x}) { $spec{preprocess} = sub { my $dev = (lstat $File::Find::dir)[0]; grep {$dev == (lstat "$File::Find::dir/$_")[0]} @_; }; } while (@dirs) { find(\%spec, $dirs[0] eq "" ? "/" : $dirs[0]); $_{""} += $_{$dirs[0]} if $conf{c}; shift @dirs; } $_{$_} < 1024 ** 1 ? printf "%s «%-6.6sB» %s\n", $_{$_}, sprintf("%6.6 +f", "$_{$_}" / 1024 ** 0), $_ : $_{$_} < 1024 ** 2 ? printf "%s «%-6.6sK» %s\n", $_{$_}, sprintf("%6.6 +f", "$_{$_}" / 1024 ** 1), $_ : $_{$_} < 1024 ** 3 ? printf "%s «%-6.6sM» %s\n", $_{$_}, sprintf("%6.6 +f", "$_{$_}" / 1024 ** 2), $_ : $_{$_} < 1024 ** 4 ? printf "%s «%-6.6sG» %s\n", $_{$_}, sprintf("%6.6 +f", "$_{$_}" / 1024 ** 3), $_ : $_{$_} < 1024 ** 5 ? printf "%s «%-6.6sT» %s\n", $_{$_}, sprintf("%6.6 +f", "$_{$_}" / 1024 ** 4), $_ : $_{$_} < 1024 ** 6 ? printf "%s «%-6.6sP» %s\n", $_{$_}, sprintf("%6.6 +f", "$_{$_}" / 1024 ** 5), $_ : $_{$_} < 1024 ** 7 ? printf "%s «%-6.6sE» %s\n", $_{$_}, sprintf("%6.6 +f", "$_{$_}" / 1024 ** 6), $_ : $_{$_} < 1024 ** 8 ? printf "%s «%-6.6sZ» %s\n", $_{$_}, sprintf("%6.6 +f", "$_{$_}" / 1024 ** 7), $_ : printf "%s «%-6.6sY» %s\n", $_{$_}, sprintf("%6.6 +f", "$_{$_}" / 1024 ** 8), $_ for sort {$_{$a} <=> $_{$b} or $a eq "" ? 1 : $a cmp $b} keys %_;
  • Not so good:
    • I don't implement qw(-D --exclude-from -l -L --max-depth -S -X) like the real du does.
    • I probably shouldn't do my own argument parsing, and I hope there's a better way to do the printing at the end.
  • That being said,
    • it seems to work well, and
    • I like the output format.
      0 «0.0000B» /usr/src/linux
      298763905 «284.92M» /usr/src/linux-2.6.11.6
      306941731 «292.72M» /usr/src/linux-2.6.11-morph6
      306986302 «292.76M» /usr/src/linux-2.6.11-morph5
      912691938 «870.41M» 

You like?


In reply to Re: What's eating all your disk space? by chibiryuu
in thread What's eating all your disk space? by hawson

Title:
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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 09:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found