View Directories and Files in a Tree Structure
Without using Tree package
If the package “tree” is not installed in the computer
In a Linux computer,
find . | sed -e "s/[^-][^\/]*\// |/g" -e "s/|\([^ ]\)/|-\1/"
find . | sed -e "s/[^-][^\/]*\// |/g" -e "s/|\([^ ]\)/|-\1/" > output.txt
What is the difference between this command and running tree
?
Tree preserves the order. So, if numerical or alphabetical order is important, installing tree is a good choice.
By using Tree package
How To View Directory Tree Structure In Linux
How to view directory structure using Tree command?
This command will display the contents of a directory in a tree-like format. You might wonder why on the earth someone would use this command whilst we already have ls command to list the contents of a directory. Unlike ls command, Tree command is a recursive directory listing program that produces a depth indented listing of files. It is quite useful to find the directories that contains lot of sub-directories in Unix-like systems.
Install Tree
Tree command is available in the default repositories of most Linux distributions. So, it can be installed from the distribution’s default package.
- On Arch Linux and its derivatives: $ sudo pacman -S tree
- On RHEL, CentOS, Fedora: $ sudo yum install tree Or, $ sudo dnf install tree
- On SUSE/openSUSE: $ sudo zypper install tree
- On Debian, Linux Mint, Ubuntu: $ sudo apt-get install tree
View Directory Tree Structure In Linux
-
Without any arguments
If you run the tree command without any arguments, the tree command will display all contents of the current working directory in a tree-like format.
$ tree
Upon completion of listing all files/directories found, tree returns the total number of files and/or directories listed. As you see in the above output, the current directory contains 3321 sub-directories, and 40023 files.
-
A specific directory
To list the files of the specific directory in a tree-like format, say for example /etc, run:
$ tree /etc/
-
Include hidden files
By default, Tree will not list the hidden files. If you want to list the hidden files, use -a parameter like below.
$ tree -a /etc/
-
View results in colored format
To view the directory structure in a colored format, use -C parameter.
$ tree -C /etc/
This is useful to easily distinguish the directories and files.
-
List only directories
As you may have noticed, all of the above commands lists the sub-directories and files. You can also list only the directories using -d parameter like below.
$ tree -d /etc/
-
Listing directories line by line
You can also display the directory listing line by line using the following command:
$ tree -d /etc/ | less
-
Limit the depth or level of recursion
By default, Tree command will list all sub-directories and the files inside the main directory. To limit the depth or of level of recursion, use -L parameter like below.
$ tree -L 2 /etc
Here, L indicates the maximum display depth of the directory tree.
-
Options
/A
- Specifies that alternative characters (plus signs, hyphens, and vertical bars) be used to draw the tree diagram so that it can be printed by printers that don’t support the line-drawing and box-drawing characters (DOS Versions 4 and 5)./F
- Displays the names of the files found within each directory listed.
-
Documentation
For more details, refer the man pages.
$ man tree
In Windows machines
$ tree /a /f > Catalog.txt
Conclusion
As you can see in this guide, tree command will give you a nice graphical tree view of the directory structure. You can use this command when you want to view the contents of directories that have tons of other files/folders nested inside their folders.