#! /usr/bin/perl -w use strict; my $cols = $ENV{COLUMNS} || 80; ## you need to "export COLUMNS" for this to work.. my $dir = shift || '.'; opendir(DIR, $dir) || die "can't opendir $dir: $!"; my @words = grep { !/^\./ } readdir(DIR); closedir DIR; my $maxlength = 0; for(@words) { my $leng = length($_); $maxlength = $leng if($leng > $maxlength); } my $numcols = int($cols / $maxlength); my $rem = $cols % $maxlength; $numcols-- if($rem < $numcols); #at least 1 space between columns.. my $colwidth = int($cols / $numcols); for(my $i = 0; $i <= $#words; $i++) { printf("%-${colwidth}s", $words[$i]); print "\n" if((($i + 1) % $numcols) == 0); } print "\n";