Is there any linux command that I can call from a Bash script that will print the directory structure in the form of a tree?
Yes, the tree command can display directories and files in a hierarchical tree-like format. For example:
tree /path/to/directory
It recursively lists each subdirectory and file, visually representing the structure. Some distributions may not have tree preinstalled, so you might need to install it first (e.g., sudo apt-get install tree
on Debian/Ubuntu, or sudo yum install tree
on CentOS/Fedora).
If you only want to show directories (and not individual files), you can pass the -d
option:
tree -d /path/to/directory
For more advanced usage (like limiting depth, ignoring certain patterns, etc.), check man tree
or tree --help
.
If you’re looking to deepen your overall coding and problem-solving skills, these two courses from DesignGurus.io can help:
-
Grokking Data Structures & Algorithms for Coding Interviews – Strengthen your foundation in data structures and algorithms, making complex tasks like recursive directory traversal easier to approach.
-
Grokking the Coding Interview: Patterns for Coding Questions – Learn essential patterns for solving coding problems, helping you tackle both interview challenges and real-world development issues more efficiently.