How to View or Print Directory Structure in Linux
This post will guide you how to display directory tree structure using a Linux command from the command line. How do I view directory tree structure through Tree command under Linux operating systems.
Print Directory Structure
If you want to see a directory tree structure for a given directory in Linux, and you can use a Linux command called Tree. And this command is not installed on your CentOS or Ubuntu system by default. So you need to install it firstly with following command:
For CentOS or RHEL Linux:
To install Tree command in your Linux system, and you need to run the following yum command to install tree command, type:
$ sudo yum install tree
Outputs:
[root@devops ~]# yum install tree
Loaded plugins: fastestmirror
Determining fastest mirrors
epel/x86_64/metalink | 18 kB 00:00
(1/3): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(2/3): epel/x86_64/primary_db | 6.8 MB 00:00:00
(3/3): remi-safe/primary_db | 1.6 MB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=========================================================================================================================================================
Package Arch Version Repository Size
=========================================================================================================================================================
Installing:
tree x86_64 1.6.0-10.el7 base 46 k
Transaction Summary
=========================================================================================================================================================
Install 1 Package
Total download size: 46 k
Installed size: 87 k
Is this ok [y/d/N]: y
Downloading packages:
tree-1.6.0-10.el7.x86_64.rpm | 46 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : tree-1.6.0-10.el7.x86_64 1/1
Verifying : tree-1.6.0-10.el7.x86_64 1/1
Installed:
tree.x86_64 0:1.6.0-10.el7
Complete!
For Ubuntu or Debian linux:
If you are working on a Ubuntu Linux system, and you can type the following apt command to install the tree command, type:
$ sudo apt install tree
Outputs:
devops@devops:~$ sudo apt install tree
[sudo] password for devops:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
tree
0 upgraded, 1 newly installed, 0 to remove and 143 not upgraded.
Need to get 40.7 kB of archives.
After this operation, 105 kB of additional disk space will be used.
Get:1 http://mirrors.aliyun.com/ubuntu bionic/universe amd64 tree amd64 1.7.0-5 [40.7 kB]
Fetched 40.7 kB in 0s (147 kB/s)
Selecting previously unselected package tree.
(Reading database ... 186122 files and directories currently installed.)
Preparing to unpack .../tree_1.7.0-5_amd64.deb ...
Unpacking tree (1.7.0-5) ...
Setting up tree (1.7.0-5) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Once the Tree command is installed on your Linux system, and you can run tree command to view the directory tree structure.
For example, you want to print directory tree structure for root directory, you just only need to run tree command without any options, type:
$ tree | less
Outputs:
devops@devops:~$ tree | less . ├── banned-ips.json ├── banned-players.json ├── Desktop ├── Documents ├── Downloads ├── eula.txt ├── examples.desktop ├── gradle-5.5.1-bin.zip ├── helloworld.py ├── IdeaProjects │ └── HelloWorld │ ├── HelloWorld.iml │ └── src │ └── HelloWorld.java ├── incubating-netbeans-11.0-bin.zip ├── jetbrains-toolbox-1.15.5796 │ └── jetbrains-toolbox ├── jetbrains-toolbox-1.15.5796.tar.gz ├── latest.zip ├── logs │ ├── 2019-06-05-1.log.gz │ ├── 2019-06-05-2.log.gz │ └── latest.log ├── LxdDirectory ......
If you want only to print the directory tree structure for a given directory, such as: /boot directory, just issuing the following command:
$ tree /boot
Outputs:
devops@devops:~$ tree /boot /boot ├── config-5.0.0-25-generic ├── config-5.0.0-27-generic ├── grub │ ├── fonts │ │ └── unicode.pf2 │ ├── gfxblacklist.txt │ ├── grub.cfg │ ├── grubenv │ ├── i386-pc │ │ ├── 915resolution.mod │ │ ├── acpi.mod ...... │ │ ├── video_colors.mod │ │ ├── video_fb.mod │ │ ├── videoinfo.mod │ │ ├── video.lst │ │ ├── video.mod │ │ ├── videotest_checksum.mod │ │ ├── videotest.mod │ │ ├── xfs.mod │ │ ├── xnu.mod │ │ ├── xnu_uuid.mod │ │ ├── xnu_uuid_test.mod │ │ ├── xzio.mod │ │ ├── zfscrypt.mod │ │ ├── zfsinfo.mod │ │ └── zfs.mod │ ├── locale │ │ ├── en_AU.mo │ │ ├── en_CA.mo │ │ ├── en_GB.mo │ │ └── en@quot.mo │ └── unicode.pf2 ├── initrd.img-5.0.0-25-generic ├── initrd.img-5.0.0-27-generic ├── memtest86+.bin ├── memtest86+.elf ├── memtest86+_multiboot.bin ├── System.map-5.0.0-25-generic ├── System.map-5.0.0-27-generic ├── vmlinuz-5.0.0-25-generic └── vmlinuz-5.0.0-27-generic 4 directories, 298 files
If you want only list only directories in a tree-like format for a given directory, you need to run tree command with -d option, type:
$ tree -d /boot
Outputs:
devops@devops:~$ tree -d /boot /boot └── grub ├── fonts ├── i386-pc └── locale 4 directories
If you want to get more information about tree command, and you can run the following man command to check its manual help page.
$ man tree
Outputs:
LISTING OPTIONS -a All files are printed. By default tree does not print hidden files (those beginning with a dot `.'). In no event does tree print the file system constructs `.' (current directory) and `..' (previous directory). -d List directories only. -l Follows symbolic links if they point to directories, as if they were directories. Symbolic links that will result in recursion are avoided when detected. -f Prints the full path prefix for each file. -x Stay on the current file-system only. Ala find -xdev. -L level Max display depth of the directory tree. -R Recursively cross down the tree each level directories (see -L option), and at each of them execute tree again adding `-o 00Tree.html' as a new option. -P pattern List only those files that match the wild-card pattern. Note: you must use the -a option to also consider those files beginning with a dot `.' for matching. Valid wildcard operators are `*' (any zero or more characters), `?' (any single character), `[...]' (any single character listed between brackets (optional - (dash) for charac‐ ter range may be used: ex: [A-Z]), and `[^...]' (any single character not listed in brackets) and `|' separates alternate patterns. -I pattern Do not list those files that match the wild-card pattern.
Conclusion
You should know that how to list contents of directories in a tree-like format using Tree command under CentOS or RHEL Or Ubuntu Linux system.