Skip to content

Instantly share code, notes, and snippets.

@ardeshir
Created November 5, 2013 20:10
Show Gist options
  • Save ardeshir/7325340 to your computer and use it in GitHub Desktop.
Save ardeshir/7325340 to your computer and use it in GitHub Desktop.
Print the tree structure of file location
#!/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