tree
is a widely-used and useful command-line tool. We see it often in code tutorials and reference documents.
Here's a real-world example of tree
output from Real Python's article, Python import: Advanced Techniques and Tips:
world/
│
├── africa/
│ ├── __init__.py
│ └── zimbabwe.py
│
├── europe/
│ ├── __init__.py
│ ├── greece.py
│ ├── norway.py
│ └── spain.py
│
└── __init__.py
Wouldn't it be nice if we could copy this output and use it to generate the same file tree on our local machine?
That's the point of untree
.
untree
is a command-line utility. To install it globally, run:
pip install untree
untree [-s schema_file] -o output_dir
Copy the tree output from a website, document or terminal and paste it right into untree
.
# we don't pass a schema file flag here, so it waits for us to enter the schema directly.
# press CTRL-D to signal the end of the text.
$ untree -o /path/to/output/dir
world/
│
├── africa/
│ ├── __init__.py
│ └── zimbabwe.py
│
├── europe/
│ ├── __init__.py
│ ├── greece.py
│ ├── norway.py
│ └── spain.py
│
└── __init__.py
# pipe the output of tree directly into untree
$ untree -o /path/to/output/dir < schema.txt
# pipe the output of tree directly into untree
$ tree -F --noreport /path/to/src/dir | untree -o /path/to/output/dir
$ untree -o /path/to/output/dir -s schema.txt
untree
is designed specifically to accept tree
output as its input. However, the most basic invocation of tree
produces output that, for our purposes, is ambiguous and extraneous.
Therefore, the untree
spec requires a slightly more specific invocation of tree
:
tree -F --noreport <directory name>'
- The
-F
flag adds a trailing slash after directory names to distinguish them from regular files. - The
--noreport
flag suppresses thetree
command's concluding summary.