Skip to content

Instantly share code, notes, and snippets.

@queerviolet
Last active August 29, 2015 14:16
Show Gist options
  • Save queerviolet/97038d7edfb9145ea9fa to your computer and use it in GitHub Desktop.
Save queerviolet/97038d7edfb9145ea9fa to your computer and use it in GitHub Desktop.
tree utility in ruby
#!/usr/bin/env ruby
# tree.rb prints a directory tree.
# Prints a tree with all the descendants of path
# if path is a directory. If path is not a directory,
# does nothing.
def print_tree(path, prefix='')
return unless File.directory?(path)
Dir.entries(path).each_with_index do |entry, index|
if entry != '.' and entry != '..'
if index < Dir.entries(path).length - 1
puts prefix + "├── #{entry}"
print_tree(path + '/' + entry,
prefix + '│ ')
else
puts prefix + "└── #{entry}"
print_tree(path + '/' + entry,
prefix + ' ')
end
end
end
end
root = ARGV[0] || '.'
puts root
print_tree(root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment