The Linux tree command is used to list the contents of a directory in a hierarchical tree diagram.
Linux tree command Function Description
Use the tree command to list the contents of a directory in a hierarchical tree diagram.
Linux tree command Syntax
tree [option] [directory]
The meaning of each option in the command is shown in the table.
Option | Description |
---|---|
-a |
All the files will be listed |
-d |
Displays the directory name instead of the contents |
-l |
If you encounter a directory that is a symbolic link, the source directory to which the link points is listed directly |
-f |
Show the full path name before each file or directory |
-i |
Do not list file or directory names in a ladder |
-q |
Use ? symbol replaces the control character and lists file and directory names |
-r |
Sort files in reverse alphanumeric order |
-p |
Displays the permission identifier for each file |
-u |
Lists the user owner or UID of a file or directory |
-g |
Lists the group owner or GID of a file or directory |
-s |
Displays the size of each file in bytes |
-t |
Sort by file and directory modification time (mtime) |
-C |
Color file and directory listings to help distinguish between types |
-D |
Display the modification time of a file or directory (mtime) |
-F |
Show the file type, append /, =, * or |
-I <Style> |
File or directory names that match the style are not displayed |
-P <Style> |
Only file or directory names that match the style are displayed |
-N |
Lists file and directory names directly, including control characters |
-A |
Display dendrograms using ASNI drawing characters instead of ASCII character combinations |
-n |
Do not color file and directory listings |
-x |
Leave only the current file system |
--inodes |
Displays the inode of each file |
--device |
The device ID of each file is displayed |
-v |
Sort files alphanumeric by version |
Linux tree command Examples
List the contents of the /home/deepinout/apidemos.com directory in a hierarchical tree diagram
tree /home/deepinout/apidemos.com
Output:
List the contents of the /boot directory in a tree diagram, step by step, and display permission information
[root@rhel ~]# tree -p /boot
/boot
├──[-rw-r--r--] config-2.6.32-358.el6.x86_64
├──[drwxr-xr-x] efi
| └──[drwxr-xr-x] EFI
| └──[drwxr-xr-x] redhat
| └──[-rwxr-xr-x] grub.efi
├──[drwxr-xr-x] grub
| ├──[-rw-r--r--] device.map
| ├──[-rw-r--r--] e2fs_stage1_5
| ├──[-rw-r--r--] fat_stage1_5
| ├──[-rw-r--r--] ffs_stage1_5
| ├──[-rw-------] grub.conf
| ├──[-rw-r--r--] iso9660_stage1_5
| ├──[-rw-r--r--] jfs_stage1_5
| ├──[lrwxrwxrwx] menu.lst->./grub.conf
| ├──[-rw-r--r--] minix_stage1_5
| ├──[-rw-r--r--] reiserfs_stage1_5
| ├──[-rw-r--r--] splash.xpm.gz
| ├──[-rw-r--r--] stage1
| ├──[-rw-r--r--] stage2
| ├──[-rw-r--r--] ufs2_stage1_5
| ├──[-rw-r--r--] vstafs_stage1_5
| └──[-rw-r--r--] xfs_stage1_5
├──[-rw-r--r--] initramfs-2.6.32-358.el6.x86_64.img
├──[drwx------] lost+found
├──[-rw-r--r--] symvers-2.6.32-358.el6.x86_64.gz
├──[-rw-r--r--] System.map-2.6.32-358.el6.x86_64
└──[-rwxr-xr-x] vmlinuz-2.6.32-358.el6.x86_64
5 directories, 22 files
// The permissions of a file or directory are enclosed in brackets before the file name
List the directory /private/
First level file names
tree /private/ -L 1
/private/
├── etc
├── tftpboot
├── tmp
└── var
Ignore Folders
tree -I node_modules # Ignore the current directory folder node_modules
tree -P node_modules # List the directory structure of the current directory folder node_modules
tree -P node_modules -L 2 # Show the directory tree structure of the directory node_modules at two levels
tree -L 2 > /home/www/tree.txt # Save the results of the current directory to the tree.txt file
Ignore multiple folders
tree -I 'node_modules|icon|font' -L 2
Non-tree structure listing all files under the directory /private/
tree -if /private/
/private
/private/a1
/private/a2
/private/etc
/private/etc/b1
/private/etc/b2
/private/tftpboot