How to Use tree to Show a Directory Tree in the Linux Terminal

In the Linux terminal, displaying the contents of a directory can become quite messy, especially when the directory contains multiple subdirectories. Fortunately, the ‘tree’ command comes in handy when you need to show the directory structure with just a few lines of code.
Here is a step-by-step guide on how to use the ‘tree’ command:
Step 1: Open the terminal
The first step is to open the terminal, which can be done by using the keyboard shortcut ‘Ctrl + Alt + T’ or through the Applications menu in Ubuntu.
Step 2: Ensure the tree command is installed
Before you proceed, ensure that the ‘tree’ command is installed on your system. You can verify this by running the command ‘tree –version,’ which will display the version of tree installed.
If the command is not installed, you can install it by running the following command:
sudo apt install tree
Step 3: Navigate to the directory
Use the ‘cd’ command to navigate to the directory you want to display. For example, to navigate to the ‘Documents’ directory, run the command:
cd Documents
Step 4: Run the tree command
Once inside the directory, run the ‘tree’ command to display the directory structure. The basic syntax of the ‘tree’ command is as follows:
tree [options] [directory]
Having no options means that the command will list all the files and directories in the current working directory and its subdirectories.
So, in our example, you can run the command ‘tree’ to display the directory structure of the ‘Documents’ directory.
Step 5: Customize the output
There are several options available to customize the output of the ‘tree’ command, such as:
– Display only directories
tree -d
– Display the full path of each file
tree -f
– Show the file sizes
tree -h
– Display the number of files and directories
tree -N
– Limit the depth of the subdirectories displayed
tree -L [number]
For example, to display only the directories in the current working directory and its subdirectories, run the command:
tree -d
Conclusion
The ‘tree’ command is a useful tool that can help you display the directory structure of your Linux system. It is particularly useful when working with large and complex directories that would otherwise be difficult to navigate. By following the steps outlined in this article, you can use the ‘tree’ command to display the directory structure in a clear and organized manner.