Skip to content

Instantly share code, notes, and snippets.

@topfunky
Created February 12, 2009 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save topfunky/62872 to your computer and use it in GitHub Desktop.
Save topfunky/62872 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# Graphical tree output of a filesystem
# `tree somepath_to_start_from`
#
# TODO show actual graphical output with dashes and bars
# TODO allow command-line options
use strict;
use File::Find;
my ($startdir) = @ARGV;
# global vars
my ( $current_indent, $lastdir, $default_indent );
MAIN:
{
# TODO add other options and allow adjustment from
# command line
$current_indent = 0;
$lastdir = $startdir || "./";
$lastdir .= '/' unless ($lastdir =~ m#/$#);
$default_indent = count_char($lastdir, "/");
find( { wanted => \&showtree }, $lastdir );
}
#########################
sub showtree
{
my ( $file, $dir, $fullpath ) = ( $_, $File::Find::dir, $File::Find::name );
my $tabs = count_char($fullpath, "/") - $default_indent;
print "\t"x$tabs . "$file\n";
}
sub count_char
{
my ($string, $char_to_count) = @_;
my $counter = 0;
while ($string =~ m#$char_to_count#g) {$counter++;}
return $counter;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment