Use the tree utility:
$ tree
.
├── base
│ ├── headings.css
│ └── typography.css
└── style.css
In Bash (or more generally, on Unix-like systems), the standard and most practical way to print a recursive directory and file tree in the console is to use the tree utility. If tree is not available, there are reliable alternatives using built-in tools.
Below are the primary approaches, from simplest to most portable.
tree (recommended)tree
tree /path/to/directory
tree -a
tree -L 3
tree -f # full paths
tree -d # directories only
If tree is not installed:
# Debian/Ubuntu
sudo apt install tree
# RHEL/CentOS
sudo yum install tree
# macOS
brew install tree
find (available everywhere)find .
find . -print | sed -e 's;[^/]*/;|____;g'
This produces a visual hierarchy similar to tree, though without sorting or formatting options.
find . -type f
find . -type d
ls recursively (not recommended for scripts)ls -R
This prints all files recursively but lacks structure and is harder to read or parse.
tree_bash() {
local dir="$1"
local prefix="$2"
for entry in "$dir"/*; do
[ -e "$entry" ] || continue
echo "${prefix}├── $(basename "$entry")"
[ -d "$entry" ] && tree_bash "$entry" "│ $prefix"
done
}
tree_bash .
This is useful in minimal environments where tree is unavailable.
| Method | Best Use Case |
|---|---|
tree | Human-readable directory trees |
find | Portability and scripting |
ls -R | Quick inspection |
| Bash function | Dependency-free environments |