-
-
Save ardeshir/7325340 to your computer and use it in GitHub Desktop.
Print the tree structure of file location
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use autodie ':all'; | |
use File::Spec::Functions qw(catdir splitdir); | |
# The starting directory will be passed on the command line | |
# Otherwise, use the current directory. | |
my $dir = @ARGV ? $ARGV[0] : '.'; | |
unless( -d $dir) { | |
die "($dir) is not a directory!"; | |
} | |
print_entries( $dir, 0); | |
exit 0; | |
sub print_entries { | |
my ($dir, $depth) = @_; | |
my @directories = grep { $_ } splitdir( $dir ); | |
my $short_name = $directories[-1]; | |
my $prefix = '| ' x $depth; | |
print "$prefix$short_name/\n"; | |
opendir( my $dh, $dir); | |
# grab everything that does not start with a '.' | |
my @entries = sort grep { !/^\./ } readdir($dh); | |
foreach my $entry (@entries) { | |
my $path = catdir( $dir, $entry ); | |
if( -f $path ) { | |
print "$prefix|-- $entry\n"; | |
} elsif ( -d _ ) { | |
print_entries( $path, $depth + 1 ); | |
} else { | |
# skip everything else | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment