print directory tree
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Printing a directory tree means turning a nested filesystem into a readable hierarchical view. The right solution depends on whether you want a quick shell command, a cross-platform script, or output you can filter and customize for another tool.
The Fastest Option on Unix-Like Systems
On Linux and macOS, the usual command is tree.
Useful variations include:
These options let you limit depth, include hidden files, or exclude noisy directories. If tree is not installed, it is usually available through the system package manager.
Windows Has a Built-In tree Command
On Windows, tree is available from cmd.
/F includes files and /A uses ASCII characters, which is often better for pasting into terminals, logs, or markdown files.
find Is a Useful Fallback
If tree is not available, find can provide a simple hierarchical listing, though it is less visually friendly.
With some filtering:
This does not draw branch characters, but it is available almost everywhere on Unix-like systems and is often enough for scripts or quick debugging.
A Portable Python Script
If you need a directory tree in a portable way, Python is a good fit. pathlib makes the recursion straightforward.
This is a good approach when you need to customize sorting, skip certain directories, or emit the tree into a report file.
Add Filtering Early
Real projects usually contain generated directories, caches, and dependency folders that overwhelm the tree. Filtering makes the output much more useful.
That small filter often matters more than the branch-drawing logic itself.
Decide Whether You Need Files, Directories, or Both
Some tasks need a full tree including files. Others only need folder structure. The command or script should reflect that.
- for structure review, directories only may be enough
- for deployment or packaging checks, include files
- for documentation, cap the depth so the output stays readable
A useful tree is not necessarily the most complete tree. It is the one that answers the question you actually have.
Common Pitfalls
- Printing the entire project root without excluding generated folders, resulting in unreadable output.
- Using Unicode branch characters in environments that need plain ASCII.
- Forgetting depth limits when the repository is large.
- Choosing a custom script when a built-in
treecommand already solves the problem. - Printing files and directories together without considering what the reader actually needs.
Summary
- Use
treewhen you want the quickest human-readable directory hierarchy. - Use Windows
tree /F /Aor Unixtreedepending on the platform. - Use
findas a simple fallback whentreeis unavailable. - Use a Python script when you need portable, customizable output.
- Filter aggressively so the printed tree stays useful instead of noisy.
Related reading
- Print list without brackets in a single row
- Print Specific nodes at a every level calculated by a given function
- Print two-dimensional array in spiral order
- printing all contents of array in C
- Printing all possible subsets of a list
- Printing all possible words from a 2D array of characters
- Printing BFS Binary Tree in Level Order with Specific Formatting
- Printing HashMap In Java

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.