Cool! Here's a minor patch to make it ignore hidden entries (those starting with a dot) by default, and add an -a flag for showing those after all, similar to the one that ls has:
--- tree.pl 2015-10-13 11:59:07.665138800 +0200
+++ tree_afr.pl 2015-10-13 11:58:27.145867300 +0200
@@ -5,11 +5,13 @@
use Pod::Usage;
my (
+ $showall, # show all, including entries starting with .
$showfiles, # show also files
$showlinks, # show also a symlink's target
);
help() unless GetOptions(
+ a => \$showall,
l => \$showlinks,
f => \$showfiles,
'h|help' => \&help,
@@ -33,6 +35,7 @@
opendir my $dh, $path or die "Couldn't read $path: $!\n";
# Get all directory content - leaving out files unless asked for
my(@content) = grep { !/^\.\.?$/ and ( $showfiles or not -f "$pat
+h/$_" ) } readdir $dh;
+ @content = grep { !/^\./ } @content unless $showall;
closedir $dh;
# How many eitems are in the directory?
Without this I get a huge tree full of subtrees like .cpan, .git and so on.
EDIT: since this is such a useful little thing, I couldn't keep myself from changing it to Unicode box-drawing characters and also formatting the code according to my preferences:
#!/bin/env perl
use Modern::Perl '2014';
no warnings 'experimental';
use feature 'signatures';
# core modules
use Getopt::Long qw(:config no_ignore_case);
use Pod::Usage;
# line-drawing characters
our %line = (
"horizontal" => "──",
"indent" => " ",
"vertical" => "│",
"branch" => "├",
"corner" => "└",
);
# options
our %opts;
# run program
exit MAIN();
sub MAIN {
# grab options, display help on error
help() unless GetOptions(
a => \$opts{'showall'}, # show all, including entries starting with .
l => \$opts{'showlinks'}, # show also a symlink's target
f => \$opts{'showfiles'}, # also show files
'h|help' => \&help,
'm|man' => \&man,
);
# traverse each specified directory, or . if none was given
foreach my $path (@ARGV ? @ARGV : '.') {
say $path;
traverse($path);
}
}
sub help { pod2usage(-verbose=>1); }
sub man { pod2usage(-verbose=>2); }
sub traverse($path, $depth = '') {
# Open the directory
opendir my $dh, $path
or die "Could not read $path: $!\n";
# Get all directory content - leaving out files unless asked for
my @content =
grep { !/^\.\.?$/ }
grep { $opts{'showall'} or !/^\./ }
grep { $opts{'showfiles'} or not -f "$path/$_" }
readdir $dh;
closedir $dh
or die "Could not close $path: $!\n";
# Number of elements yet to be printed
my $remaining = scalar @content;
# Prepare the standard indent
my $indent = $depth . $line{'vertical'} . $line{'indent'};
# Print all the elements
foreach my $name (@content) {
my $fullname = "$path/$name";
# Prepare indent
$indent = $depth . " " . $line{'indent'} unless --$remaining;
print $depth, ($remaining ? $line{'branch'} : $line{'corner'}), $line{'horizontal'} , $name;
# Is it a link?
if (-l $fullname) {
# Shall they be shown as such
if($opts{'showlinks'}) {
print " -> ", readlink $fullname;
}
print "\n";
next;
}
print "\n";
# Traverse if it's a subdirectory
traverse($fullname, $indent) if -d $fullname;
}
return;
}
=head1 NAME
tree - A script to show a "graphical" representation of a directory structure
=head1 SYNOPSIS
tree options path...
=head1 DESCRIPTION
tree will show a "graphical" representation of a directory structure, including
all files (when B<-f> specified) and link targets (when B<-l> specified).
=head1 OPTIONS
=over 4
=item B<-f>
Show also files.
=item B<-l>
Shhow also link targets.
=item B<-h>
=item B<--help>
show a short help page.
=item B<-m>
=item B<--man>
Show the man-page.
=back
=head1 AUTHOR
Skeeve, member of perlmonks.org (perlmonks DOT org DOT Skeeve AT XoXy DOT net)
Minor changes and updates by Apple Fritter, a fellow Perl monk
=cut
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.