<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:base="https://illuminatedcomputing.com/">
  <id>https://illuminatedcomputing.com/</id>
  <title>Illuminated Computing</title>
  <updated>2010-03-02T00:00:00Z</updated>
  <link rel="alternate" href="https://illuminatedcomputing.com/" type="text/html"/>
  <link rel="self" href="https://illuminatedcomputing.com/tags/perl/atom.xml" type="application/atom+xml"/>
  <author>
    <name>Paul A. Jungwirth</name>
    <uri>https://illuminatedcommputing.com/</uri>
  </author>
  <entry>
    <id>tag:illuminatedcomputing.com,2010-03-02:/posts/2010/03/sort-by-file-size-with-du/</id>
    <title type="html">Sort by File Size with du</title>
    <published>2010-03-02T00:00:00Z</published>
    <updated>2010-03-02T00:00:00Z</updated>
    <link rel="alternate" href="https://illuminatedcomputing.com/posts/2010/03/sort-by-file-size-with-du/" type="text/html"/>
    <content type="html">
&lt;p&gt;Here is a handy Perl script I wrote a while back to sort the output from &lt;code&gt;du -sh&lt;/code&gt; by file size. The standard &lt;code&gt;sort&lt;/code&gt; command can’t do this because it doesn’t know how to compare values like “488M” and “5.0K.” My code will sort any lines where this values appear in the first field. I’m sure the Perl could be more compressed, but keeping it easy to read like this is more my style:&lt;/p&gt;

&lt;div class="CodeRay"&gt;&lt;div class="code"&gt;&lt;pre&gt;&lt;code class="language-perl"&gt;#!/usr/bin/perl -w
use strict;
use Data::Dumper;

my @lines;

while (&amp;lt;&amp;gt;) {
  chomp;
  push @lines, [unabbrev($_), $_];
  # print "$_: ", unabbrev($_), "\n";
}

# print Dumper \@lines;

for my $line (reverse sort { return $a-&amp;gt;[0] &amp;lt;=&amp;gt; $b-&amp;gt;[0] } @lines) {
  print $line-&amp;gt;[1], "\n";
}

sub unabbrev {
  my $val = shift;
  if ($val =~ m/^\s*(\d+(\.\d+)?)([KMGB]?)/) {
    if ($3 eq 'K') {
      $val = $1 * 1000;
    } elsif ($3 eq 'M') {
      $val = $1 * 1000000;
    } elsif ($3 eq 'G') {
      $val = $1 * 1000000000;
    } else { # B or nothing
      $val = $1;
    }
  }
  return $val;
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It’d be fun to re-write this in ruby—or even better, add it as a feature to GNU sort.&lt;/p&gt;

&lt;p&gt;UPDATE: Reading the documentation for GNU coreutils (which contains sort), I see that sort does have an -h option (for –human-numeric-sort). Strangely, this option is not documented in the man page on Ubuntu 9.10 and is unrecognized by /usr/bin/sort. I guess I’ve got an old version.&lt;/p&gt;

&lt;p&gt;If anyone is looking for a small open source project, sort’s implementation of this option could still be improved. Right now, according to the online docs, “values with different precisions like 6000K and 5M will be sorted incorrectly.” It’d be great if it fully implemented the rules for &lt;a href="http://www.gnu.org/software/coreutils/manual/coreutils.html#Block-size"&gt;block size&lt;/a&gt; used by other coreutils programs.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <id>tag:illuminatedcomputing.com,2009-03-08:/posts/2009/03/rtouch/</id>
    <title type="html">rtouch</title>
    <published>2009-03-08T00:00:00Z</published>
    <updated>2009-03-08T00:00:00Z</updated>
    <link rel="alternate" href="https://illuminatedcomputing.com/posts/2009/03/rtouch/" type="text/html"/>
    <content type="html">
&lt;p&gt;By the way …&lt;/p&gt;

&lt;p&gt;Upgrading Wordpress is annoying! There was lots of “delete this folder–except for file x and folder y.” Because of how I organize things, at one point I found it useful to write a recursive touch script. Here it is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/usr/bin/perl -w
use strict;
use File::Find;

my @dirs = @ARGV ? @ARGV : ('.');

find sub {
    system("touch", $_);
}, @dirs;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It’s pretty simple: for instance, it doesn’t pass along any options to the touch program. But I thought I’d put that off until I can rewrite it in ruby. This Perl version was just because I needed it done quick.&lt;/p&gt;
</content>
  </entry>
</feed>

