The tree command is a powerful tool for visualizing the directory structure of your filesystem in a tree-like format. Unlike standard listing tools like ls, tree displays nested folders and files with indentation, making it easy to understand the layout of your project at a glance.
The tree command displays directory structures in a tree-like format.
tree
This prints the directory tree from the current folder.
Use the -I (capital i) option with a pattern to exclude files or folders:
tree -I 'folder_to_exclude'
Multiple excludes: Separate with | (pipe):
tree -I 'node_modules|dist|venv'
Use the -L option to limit the depth of recursion (how deep tree goes):
tree -L 2
This prints 2 levels (current dir and its immediate subfolders).
To limit number of items per folder (not depth):
There is no built-in option in tree for “max items per folder”, but you can combine with head:
tree | head -n 20
This will print only the first 20 lines of tree output.
Or, for more custom filtering (for advanced users), consider using ls with head:
ls | head -n 10
But this doesn't provide a tree structure.
Exclude node_modules and show only 2 levels deep:
tree -I 'node_modules' -L 2
Show only top 10 lines of tree output:
tree | head -n 10
| Feature | Option/Command | |
|---|---|---|
| Exclude folder | tree -I 'folder' | |
| Exclude multiple folders | `tree -I 'folder1 | folder2'` |
| Limit depth (levels) | tree -L N | |
| Limit items per folder | Not supported directly | |
| Limit output lines | `tree | head -n N` |