This tutorial will explore several methods to print a directory tree from the command line on Linux.
The commands covered below are
du, which can all output a directory tree in Linux.
Linux tree Command
The single best tool to view a directory tree is the
tree command. This is mainly due to the fact that the command’s output is structured to be easy to read. Other commands can accomplish the task, but the output isn’t as pretty.
Tree usually isn’t installed by default, but you can install it with the appropriate command below:
Ubuntu, Debian, and Linux Mint:
$ sudo apt install tree
Fedora, AlmaLinux, CentOS, and RHEL:
$ sudo dnf install tree
Arch Linux and Manjaro:
$ sudo pacman -S tree
Example 1. Use the
tree command with no options to print the directory structure for your current directory. Or you can specify the path to a directory you want to see the structure for.
$ tree ~/Downloads /home/linuxnightly/Downloads ├── bsd │ ├── freebsd.iso │ └── openbsd.iso └── linux ├── arch-based │ └── manjaro.iso ├── debian-based │ ├── debian.iso │ ├── mint.iso │ └── ubuntu.iso ├── other │ ├── gentoo.iso │ └── slackware.iso └── rhel-based ├── centos.iso └── fedora.iso 6 directories, 10 files
For structures with a lot of text, it’s helpful to pipe the output to
$ tree ~/Downloads | less $ tree ~/Downloads | more -25
Example 2. By default, the
tree output will show files and directories. You can print directories only by appending the
$ tree -d ~/Downloads /home/linuxnightly/Downloads ├── bsd └── linux ├── arch-based ├── debian-based ├── other └── rhel-based 6 directories
Example 3. Sometimes it’s helpful to see file and directory sizes so you can see how much storage is being consumed across the directory tree. Append
--du to see file and directory sizes, and
-h to format the sizes in human readable format.
$ tree --du -h ~/Downloads /home/linuxnightly/Downloads ├── [4.4G] bsd │ ├── [2.3G] freebsd.iso │ └── [2.1G] openbsd.iso └── [ 36G] linux ├── [3.8G] arch-based │ └── [3.8G] manjaro.iso ├── [ 14G] debian-based │ ├── [3.8G] debian.iso │ ├── [5.0G] mint.iso │ └── [4.7G] ubuntu.iso ├── [6.2G] other │ ├── [3.4G] gentoo.iso │ └── [2.8G] slackware.iso └── [ 13G] rhel-based ├── [7.3G] centos.iso └── [5.3G] fedora.iso 41G used in 6 directories, 10 files
Example 4. Use the
-L option to limit the number of subdirectories that
tree can traverse. For example, to only scan three subdirectories deep:
$ tree -L 3
Directory Tree With find, ls, du
Example 1. The
find command will output a list of all files that match a search query. We can use
find with no parameters to have it output all directories, subdirectories, and files for any path we specify.
$ find . . ./bsd ./bsd/freebsd.iso ./bsd/openbsd.iso ./linux ./linux/arch-based ./linux/arch-based/manjaro.iso ./linux/debian-based ./linux/debian-based/debian.iso ./linux/debian-based/mint.iso ./linux/debian-based/ubuntu.iso ./linux/other ./linux/other/gentoo.iso ./linux/other/slackware.iso ./linux/rhel-based ./linux/rhel-based/centos.iso ./linux/rhel-based/fedora.iso
Example 2. We can instruct
find to output only directories by using the
$ find . -type d . ./bsd ./linux ./linux/arch-based ./linux/debian-based ./linux/other ./linux/rhel-based
Example 3. Use the
-R command with
ls in order to list all files and directories recursively.
$ ls -R .: bsd linux ./bsd: freebsd.iso openbsd.iso ./linux: arch-based debian-based other rhel-based ./linux/arch-based: manjaro.iso ./linux/debian-based: debian.iso mint.iso ubuntu.iso ./linux/other: gentoo.iso slackware.iso ./linux/rhel-based: centos.iso fedora.iso
Example 4. The
du command can list a directory tree as well as the sizes of directories and files. This command is most useful with
-a to also list files, and
-h to list sizes in human readable format.
$ du -ah 2.4G ./bsd/freebsd.iso 2.2G ./bsd/openbsd.iso 4.5G ./bsd 3.8G ./linux/arch-based/manjaro.iso 3.8G ./linux/arch-based 3.8G ./linux/debian-based/debian.iso 5.1G ./linux/debian-based/mint.iso 4.8G ./linux/debian-based/ubuntu.iso 14G ./linux/debian-based 3.4G ./linux/other/gentoo.iso 2.9G ./linux/other/slackware.iso 6.2G ./linux/other 7.3G ./linux/rhel-based/centos.iso 5.4G ./linux/rhel-based/fedora.iso 13G ./linux/rhel-based 37G ./linux 41G .
tree command is the best tool to list a directory tree on Linux. However, default commands can also do the job and will suffice if you don’t need the fancy formatting from
tree. These same commands can also be used to find files based on size.