You’ve probably seen images or GitHub repositories showing project folders in a tree-like structure, with sub-directories as branches and files as leaves. These layouts are visually appealing and provide a clear view of the folder structure. They are especially useful for organizing projects by grouping files based on their function or purpose. This makes it easier to understand the project, its composition, module functions, and their respective roles.
While IDEs like VS Code allow us to visualize folder structures, it’s not easy to copy or fully view the entire structure. However, using the tree package in the Linux command terminal, you can easily view and manage your folder structure.
In this article, let’s explore how to install and use the tree command to view your folder’s structure directly in your terminal. We’ll also look at different flag options that let you customize your folder structure output according to your needs. Additionally, we’ll explore how to save these structures into Text, HTML, XML, and JSON files.
Installation
You can install the tree package from your terminal by following this guide. Make sure to use the correct command for your Linux distribution.
Debian / Ubuntu / Mint
sudo apt-get install tree
RHEL / CentOS / Fedora
sudo dnf install tree
Arch Linux
sudo pacman -Syu tree
OpenSUSE
sudo zypper install tree
Alpine Linux
sudo apk add tree
Using the tree Command
After successfully installing the tree package, you can now use the tree command to view the folder structure directly in your terminal.
To see the folder structure of any directory, open the terminal from that directory and simply run the tree command:
tree
# or
tree /path/name
The output will look something like this:

You can also pass any directory path to see its folder structure. Run the pwd command to get the path of any folder, and by passing that path, you can generate the folder structure from anywhere. See the example below:

Customizing the Output
The tree command creates a tree structure of all files, folders, and hidden files. Sometimes, the structure can look very long and cluttered. For example, if you have a node_modules folder in your project, you know how many folders and files it contains. Or if you have a .git folder, the tree will look huge with unnecessary information. You might wonder if you can customize this. Of course, you can. You can also specify how many nested folders you want to include in the structure, show only folders, and much more. Let’s explore how we can achieve this.
Displaying Hidden Files
Use the -a flag to see all the folders and files, including hidden files:
tree -a

Limiting Directory Depth
To include a limited number of nested directories in the tree structure, specify the number with the -L flag. This will ignore folder files nested more deeply than the specified number:
tree -L 2
This command will include up to 2 levels of nested folders and their files in the folder structure.

Listing Directories Only
With the -d flag, you can ignore files and list only the directories in the folder structure:
tree -d

Ignoring Files and Directories
To ignore specific folders or files in the structured output, use the -I flag followed by the folder or file name. For example, to ignore the node_modules, .git folder and .env file we can specify the command like this:
tree -I 'node_modules|.git|.env'
You can add more patterns if you want; even deeply nested files or folders can be ignored. Just make sure to specify the file or folder paths correctly.

Sorting by Modification Time
Using the -t flag, you can sort the files by their last modification time. The most recently modified files and directories will appear first within each directory.
tree -t

Enabling Colorized Output
By default, the outputs are colorized. If you’re not seeing it, you can generate a colorized structure using the -C flag, making it easier to distinguish between files and directories:
tree -C

Displaying Full Paths
With the -f flag, you can see the folder structure, where each file and folder is shown in the full path format (relative path):
tree -f

Displaying File Sizes
With the -s flag, you can get the file size of each file along with the structure:
tree -s

Using Human-Readable Sizes
While the above method shows file sizes, they are not in a human-readable format. To make it human-readable, use the -h flag:
tree -h

Displaying File Owners
You can also display the owner of the files using the -u flag:
tree -u

Combining Flags
You can combine these flags for your desired output. For example, if you want a colorized structure with 2 levels of folder depth, ignoring node_modules, .git, and .env files, with human-readable file sizes, you can achieve it with:
tree -C -L 2 -I 'node_modules|.git|.env' -h

Saving the Output
Using certain commands, you can also save these tree-like structures in text, HTML, JSON, and XML files.
Saving as Text
To save the output to output.txt, you can run the following command:
tree > output.txt
# or
tree -o output.txt
Saving as HTML
To save the output to output.html, use:
tree -H . -o output.html
Saving as XML
To save the output to output.xml, use:
tree -x -O output.xml
Saving as JSON
To save the output to output.json, use:
tree -J -o output.json
Combining Flags with Output
You can also use different flags along with the output command according to your needs. For example:
tree -C -L 2 -I 'node_modules|.git|.env' -J -o output.json
I hope this article helps you learn the tree command, customize its output, and save the structure in different formats. I would appreciate any feedback and suggestions for improvement. Thank you!