[Melbourne-pm] finally there

Mirko Fluher mir at pax.apana.org.au
Thu Dec 20 04:08:56 PST 2007


On Sun, Dec 16, 2007 at 10:40:21PM +1100, Mirko Fluher wrote:

If you remember this was my original request... 
> Could someone help with this mixture of Unix and Perl.
> 
> foreach $_ (`tree -isuDRf /dir1/fileserver/dir2/documents`)
> {
>         next if(/^\/dir1/);
>         ($line1, $line2) = /(.*?)\/dir1\/fileserver\/dir2\/documents(\/.*$)/;
>         $line1 =~ tr/[]//d;
>         $_ = $line1;
>         ($userid, $size, $month, $day, $year) = /^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/;
>         print "$userid,$size,$month,$day,$year,$line2\n";
> }
> # to run this prog.
> # ./make_index_docu_csv.pl|sort +1 -rnt, > /dir1/fileserver/dir2/documents/documents_index.csv

I noticed one problem I had was with 'tree' ... it seems to truncate userid afer 8 char....
Thought of using Unix 'find' instead ... but then also thought there must be a way in Perl ... 
no so simple if you haven't done much work with modules before ... 

Anyway, thanks to all those who replied! no matter what code I received, it was very useful!!
Here is what I have so far ... I tested it and it produces very similar output ... except for date..
because tree gives mmddyy and if yy is current it gives time instead... but I am not too worried about
that. ... here it goes... 

------------
#!/usr/bin/perl -w

use File::Find;
use Date::Format;

$dirname = "/dir1/fileserver/dir2/documents";

find(sub {$size{$File::Find::name} = -s if -f;}, $dirname);
@files = sort {$size{$b} <=> $size{$a}} keys %size;

foreach (@files)
{
        my ($uid, $mtime) = ((stat($_))[4,9]);
        my $userid = getpwuid($uid);
        my $date = time2str('%Y-%m-%d', $mtime);
        printf "%s,%s,%s,%s\n", $userid, $size{$_}, $date,$_;
}
--------------------
Happy to read any other useful comments as I am sure the above code can be improved.
Cheers,

-- 
Mirko Fluher  http://pax.apana.org.au

Il semble que la perfection soit atteinte non quand il n’y a plus rien à
ajouter, mais quand il n’y a plus rien à retrancher.
Perfection is apparently achieved not when nothing more can be added, but
rather when nothing else can be removed.
– Antoine de Saint-Exupéry, Terres des Hommes 



More information about the Melbourne-pm mailing list