~/bin/tree

cwest on 2004-01-26T22:25:07

The tree utility hasn't been ported to Mac OS X, and I found it easier to create a close approximation. I find it useful.

use File::Spec::Functions qw[splitpath];
($|, $\) = (1, "\n");
sub crawl {
  my ($dir, $line_prefix) = @_;
  my @entries = glob "$dir/*";
  foreach my $entry_num ( 0 .. $#entries ) {
    my $entry = $entries[$entry_num];
    my $last = $entry_num == $#entries;
    my $prefix = $last?'`':'|';
    print "$line_prefix$prefix-- ".
      (splitpath $entry)[2];
    crawl($entry,
      $line_prefix.($last?' ':'|').' 'x3) if -d $entry;
  }
}
print my $start = shift || '.';
crawl($start,'') if -d $start;

Posted from caseywest.com, comment here.