Wrote this 'cause I couldn't get the tree program online to work on my computer.
#!/usr/bin/perl use Term::ANSIColor; use Getopt::Long; my $depth = 0; my $directory = "."; my $totaldir = 0; my $totalfile = 0; my %CONFIG = (); my $output = 'STDOUT'; Getopt::Long::Configure ("bundling_override"); GetOptions(\%CONFIG, 'n=i', 'c', 'r', 'o', 'L'); if (exists($CONFIG{'o'})) { $CONFIG{'o'} = 'tree_dir'; $output = <FILE>; open($output, ">$CONFIG{'o'}") or die "Can't open $output : $!"; } if (!(-d $ARGV[0]) && ($ARGV[0])) { print "Directory does not exist.\n"; print "Exiting\n"; exit; } else { $directory = (!$ARGV[0] ? "." : $ARGV[0]); print "\nAtlasing \"$directory\" and it's sub-directories.\n\n"; } &tree($directory); sub tree { my (@directory, @dirs, @files); my $i = 0; chdir "$_[0]"; chomp(@directory = glob("*")); $depth++; foreach my $cur (@directory) { (-d $cur) ? push(@dirs, $cur) : push(@files, $cur); } $totaldir += @dirs; $totalfile += @files; if ($CONFIG{'r'}) { (@dirs, @files) = reverse(@files, @dirs); } @directory = (@dirs, @files); foreach my $cur (@directory) { print $output "\n|" . (" " x ($depth-1)); if (exists($CONFIG{'n'}) and ($i >= $CONFIG{'n'}) and (!(-d $c +ur))) { print $output "+ " . (($#directory - $i) + 1) . " more fil +es in this directory"; last; } else { print $output "-"; print color((-d $cur) ? 'bold blue' : 'red') if ($CONFIG{ +'c'}); print $output $cur; print color 'reset'; print $output "\/" and tree($cur) if (-d $cur); if (exists($CONFIG{'L'}) && (!(-d $cur))) { $mtime = gmtime((@stat = stat($cur))[9]); print " $mtime"; } $i++; } } chdir ".."; $depth--; return; } print $output "\n\nTotal number of directories: $totaldir."; print $output "\nTotal number of files: $totalfile.\n"; close($output);

Replies are listed 'Best First'.
Re: tree (Unix program)
by svenXY (Deacon) on Dec 16, 2008 at 09:12 UTC
    Hi,
    nice one, anon++ (you should have published this with your monk name for some well deseved XP ;-) )
    Two things come to my mind:
  • it doesn't compile with use strict;
  • there is no usage message or POD that describes the parameters
    Regards,
    svenXY
      Agreed svenXY...also, in general, the layout ...
      • preparation
      • sub call
      • sub definition
      • wrap up
      seems a little odd to me

      A user level that continues to overstate my experience :-))