#!/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 | |
by Bloodnok (Vicar) on Dec 16, 2008 at 12:22 UTC |