Linux tree command - list the contents of a directory in a hierarchical tree diagram

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:

Linux tree command

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
Like(0)

Related

Linux Login Logout Command
Linux login commandLinux logout commandLinux nologin commandLinux exit commandLinux sulogin commandLinux rlogin commandLinux poweroff commandLinux ctrlaltdel CommandLinux shutdown commandLinux halt commandLinux reboot commandLinux init commandLinux runlevel commandLinux telinit command
Linux File Management Command
Linux cat commandLinux tac commandLinux nl commandLinux more commandLinux less commandLinux head commandLinux tail commandLinux rev commandLinux fold commandLinux fmt commandLinux expand commandLinux pr commandLinux sort commandLinux uniq commandLinux cut commandLinux comm commandLinux diff commandLinux join commandLinux diff3 commandLinux cmp commandLinux colrm commandLinux paste commandLinux mkdir commandLinux tr commandLinux split commandLinux csplit commandLinux tee commandLinux unexpand commandLinux patch commandLinux awk commandLinux sed commandLinux od commandLinux pwd commandLinux cd commandLinux ls commandLinux dir commandLinux dirs commandLinux touch commandLinux rmdir commandLinux cp commandLinux mv commandLinux rm commandLinux install commandLinux tmpwatch commandLinux file commandLinux du commandLinux wc commandLinux tree commandLinux cksum commandLinux md5sum commandLinux sum commandLinux dirname commandLinux mkfifo Command
Cron Expressions
Cron Expression to Run Every Day at 12 PMUnderstanding Vue Cron ExpressionsUnderstanding JS Cron ExpressionsA Comprehensive Guide to Cron Expressions for Scheduled TasksUnderstanding Linux Cron ExpressionsUnderstanding Quartz Cron ExpressionsCron ExpressionCron Time ExpressionCron Expression ParsingCron Expression: Executing a Task Every SecondCron Expression for Every Minute ExecutionCron Expression to Execute Every 10 MinutesCron Expression: Executing Every HourCron Expression to Execute Once a YearCron Expression: How to Schedule a Task to Run Daily at Midnight?