tree.py is a small utility that displays input from stdin in a tree-like structure. It takes a large amount of inspiration from Steve Baker's original and widely deployed tree utility: http://mama.indstate.edu/users/ice/tree/
tree.py operates in a very different way to the traditional tree command, it instead takes input from stdin (so you can pipe it data) and will try and parse it and present it in an intuitive tree-like format.
You can put the file tree.py wherever you like, and set an alias in your
.bashrc file if you use bash, or you can just copy it to your
> sudo cp tree.py /usr/bin/
The traditional way to use tree.py is with no commands (or with a path passed in as an argument) to display the whole directory tree of a target.
> tree [path]
> tree ~/foo/bar
By default, files starting with a "
." are hidden, to include these files, you
can run tree.py with the
-a flag, e.g:
> tree -a ~/foo/bar
Usage with find (or another tool with similar output)
Tree automatically detects when data is being piped to it instead of standalone usage:
> find . | tree
. ├── .git │ └── ... ├── COPYING ├── DISCLAIMER ├── README.md └── tree.py 23 directories, 34 files
You can start to build up powerful commands that display in a nice tree-like way, for example, if you were to filter the above file names like so:
> find . | grep head
You would get this result:
./.git/logs/refs/heads ./.git/logs/refs/heads/master ./.git/refs/heads ./.git/refs/heads/master
And piped into tree.py...
> find . | grep head | tree
... it display like so:
. └── .git ├── logs │ └── refs │ └── heads │ └── master └── refs └── heads └── master 7 directories, 2 files
Usage with grep
If you are using grep to search for text within multiple files, tree.py can
also accept this input, and display a line-match count for each file. For this,
as it is not normal input, you need to pass in
-i grep or just
> grep as ./COPYING ./DISCLAIMER | tree -i g
. ├──  COPYING └──  DISCLAIMER 1 directory, 2 files
And here's a nice example with xargs and find too:
> find . -type f | xargs grep 'as' | tree -i g
. ├── .git │ ├──  COMMIT_EDITMSG │ ├──  HEAD │ ├──  config │ ├── hooks │ │ ├──  commit-msg.sample │ │ ├──  pre-commit.sample │ │ ├──  pre-push.sample │ │ ├──  pre-rebase.sample │ │ ├──  prepare-commit-msg.sample │ │ └──  update.sample │ ├──  packed-refs │ └── refs │ └── remotes │ └── origin │ └──  HEAD ├──  COPYING ├──  DISCLAIMER └──  tree.py 6 directories, 14 files
- Implement tree for non-stdin usage (i.e. without needing
- More input file formats
- Ability to display information on files such as file permissions, size, user, group, timestamps etc...
This is the help message that can be seen after running tree -h
> tree -h Usage: tree.py [-h] [-i [none|normal|grep|g|n]] [other options ...] [target] A small utility that displays input from stdin in a tree-like structure -h, --help show the help message -i, --mode, --input-mode The input type. If ommitted, the default is "auto", if -i is given with no argument, then "normal" is used. Values: - auto: (default when -i is not included in the command) automatically try and detect whether data is being piped to tree, and if so, use "normal" mode, otherwise use "none" (checks if stdin is a tty) - none: don't read from stdin, display a target directory instead! - normal: [or n] (default when -i is included but no value given) Accept input which is one filename per line - grep: [or g] Accept input that is like grep multi-file output (i.e. "file: match" for each line, with single files possibly appearing multiple times) -c, --color, --colour Use color in the tree. Values: - auto: try and automatically detect - always - none -a Include hidden files -e, --encoding Which characters should be used to draw the tree Values: - auto: try and detect which would be best (default) - utf-8 - ascii
tree.py loosely uses the environment variables
TREE_COLORS to decide how to color the output.
TREE_COLORS is given a
higher priority, overriding each individual 'match' in LS_COLORS. You can also
bin to set colours for when you pipe grep input into tree
to colour the matches.
Copyright © 2013 Sam Lanning firstname.lastname@example.org
This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See the COPYING file for more details.