A directory tree on a Linux system is a way to see all of the directory and sub directories in a provided filesystem path. In this tutorial you will learn how to print directory tree in Linux terminal and GUI.
This type of overview can be difficult to achieve in GUI file browsers or by simply changing directories on the command line. But there are a few tools in Linux that give us a birds eye view of how our directories and their contents are structured.
In this tutorial, you will see various ways to print a directory tree using command line or GUI on a Linux system.
In this tutorial you will learn:
How to use tree command and its options
How to use ls, du, and find commands to print directory tree
Privileged access to your Linux system as root or via the sudo command.
Conventions
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command $ – requires given linux commands to be executed as a regular non-privileged user
Print directory tree with tree command on Linux
Let’s just dive right into the best tool for the job. The tree command is not usually included by default on Linux distros, but it is easily installable, and is perfect for lising the directory tree of any path.
In case you do not already have access to the command, you can use the appropriate command below to install tree with your system’s package manager.
Now that you can use the tree command, see some of the examples below to learn how it works.
The most simple way to print a directory tree is by using the tree command and the path you would like to print a directory tree for. If used without specifying a directory, it will print the structure for your present working directory. We recommend piping the output to less if your directory contains many files and subdirectories.
If you want to list only directories, use the -d option.
$ tree -d
If you want to limit tree to displaying only a certain number of directories deep, use the -L option and the number of subdirectories you want tree to traverse. For example, this command will limit tree to 3 subdirectories deep.
If you want to include hidden files and directories in the tree output, append the -a option.
$ tree -a
Add the -h option if you want to include the size of the files in tree output.
$ tree -h
Print directory tree with du, ls, and find commands on Linux
Although tree has to be the ideal command for listing directory trees, Linux comes with a few default commands that can also do the job, namely du, ls, and find. See some of the examples below to learn how to use these commands to list directory trees.
The find command will list all files and directories in a given path. To search the present working directory, just use ..
If you only want the find command to list directories and subdirectories, use the -type d option.
$ find . -type d
Use the -maxdepth option to limit find to only traverse a specified number of subdirectories deep. This command limits find to two subdirectories deep.
$ find . -maxdepth 2
Everyone knows the ls command to list files on Linux, but it can also list subdirectories and their contents with the -R (recursive) option, effectively giving us a directory tree.
The du command can also be used to print a directory tree. The main use of the du command is to list file size and directory size, so our trees will also contain that information. Usually you will want to add the -h option to make the sizes human readable.
Sometimes, it is easier to visualize a directory tree if we use a GUI utility. One such application is called Disk Usage Analyzer, but it may not be installed by default on your Linux distro. Use the appropriate command below to install it with your system’s package manager.
You can use the appropriate command below to install Disk Usage Analyzer with your system’s package manager.
When the program opens, it will ask if you want it to scan the home directory or an entire disk. You can also click the options menu (three stacked lines) for the ability to scan a particular folder.
Make your selection and the utility will begin scanning for files. Once it finishes scanning for content, it will give you a full readout of how your hard disk space is being distributed to various directories on your system. There is also a graphical representation which you can move your mouse cursor over to get an even better idea. It lists directories by size, so you can quickly determine what’s chewing up the most disk space.
Use the arrows next to each directory to expand a list of files and subdirectories, effectively viewing the directory tree of any path you want
Closing Thoughts
In this tutorial, we saw how to print a directory tree on Linux from command line and GUI. The tree command is our best recommendation, as it is meant especially for this purpose and comes loaded with a lot of options. But Linux also includes the default ls, find, and du commands, which can be equally as useful.
If you do not want to fiddle with the command line, then Disk Usage Analyzer works well at not only printing directory trees, but showing you how much space each directory is consuming.