Last active
August 29, 2015 14:16
-
-
Save queerviolet/97038d7edfb9145ea9fa to your computer and use it in GitHub Desktop.
tree utility in ruby
This file contains hidden or 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/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