Solaris hates efficiency.
OK, this isn’t entirely true, but it seems like everyone
I know who strives for efficiency in their work also finds that Solaris
userspace tools aren’t up to par. This usually translates to blanket
statements like “Solaris Sucks”. I’m not going to
argue any of this. I abstain please. Call me
Switzerland.
I ran into a limitation of Solaris userspace tools
recently. The find command does not support -printf like the GNU find
command. I need to output the date of a file in a numeric format so
that I can send the output to sort.
This angered me at first, but then I remembered that I was once
quiet profficient in Perl.
#!/usr/bin/perl sub f {
$d=shift;
print "openning $dn";
opendir(DIR,$d);
while( $f=readdir(DIR) ) {
print "$f $d/$fn";
if( $f eq "." || $f eq ".." ) {
print "$f nextn";
next;
}
if ( -f "$d/$f" ) {
@s=stat("$d/$f");
$fs{$s[11].$d.$f}=push @s,"$d/$f";
print "$d/$fn";
} if ( -d "$d/$f") {
print "calling f $d/$fn";
f("$d/$f");
}
}
}
f(".");
foreach (sort { $a <=> $b } keys %fs ) {
print $_, join(" ",@{$fs{$_}}),"n"; }
Yes, it works, but anyone who knows perl a little better knows that it is
WAY too long. I should have use File::Find.
#!/usr/bin/perl use strict;
use File::Find;
find({ wanted =>&data, follow=>1 }, $ARGV[0]);
sub data {
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime) = lstat($_);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday)=localtime($ctime);
$year=$year+1900;
print "$ctime $year-$mon-$mday+$hour:$min:$sec $uid $gid $File::Find::namen";
}
This just outputs the data I need and in true UNIX fashion, I use sort to do the actual sort
Using Perl always makes me feel guilty, mostly because
I frequent MichiPug, and its a python group, not a perl group.
So I wanted to try the same thing using python.
import os, sys
from datetime import datetime
from os.path import join, getsize
for root, dirs, files in os.walk(sys.argv[1]):
print os.stat(root).st_ctime, datetime.fromtimestamp(os.stat(root).st_ctime), root
I don’t know about you, but I know which of these I find most readable. 🙂