1. Introduction to tree command
The tree command is a command used to display the directory structure. It can display all files and subdirectories in a specified directory in the form of a tree diagram. This command is not very commonly used, but it is still very useful when we count the directory structure of a certain directory. We need to use this if we need to completely copy the directory structure of a certain directory, but are not responsible for the files in the directory. Order.
2. Example of using tree command
1. Check the command version
[root@s152 ~]# tree --version
tree v1.6.0 © 1996 - 2011 by Steve Baker, Thomas Moore, Francesc Rocher, Kyosuke Tokoro
2. Get command help
[root@s152 ~]# tree --help
3. View the directory test directory structure
[root@s152 ~]# tree test
test
├── level1-1
│ ├── level2-1
│ ├── level2-2
│ └── level2-3
├── level1-2
│ ├── level2-1
│ ├── level2-2
│ └── level2-3
└── level1-3
├── level2-1
├── level2-2
└── level2-3
12 directories, 0 files
4. Only display the full path of the directory
If we need to recursively obtain the entire directory structure under the directory, we can use the following command. -f means to print the complete path; -d means to print only the directory, not the file; -i means to omit the preceding horizontal line.
[root@s152 ~]# tree -fid test
test
test/level1-1
test/level1-1/level2-1
…
test/level1-3/level2-3
12 directories
5. Do not print statistical information
[root@s152 ~]# tree --noreport test
test
├── a.txt
├── level1-1
│ ├── bb.txt
│ ├── level2-1
│ ├── level2-2
│ │ └── ccc.txt
│ └── level2-3
…
6. Print inode information
The inodes parameter can be used to print the inodes number of files and directories. The inodes number is generally used to delete files when the file name displays garbled characters. You can use the command #find -inum inodes_number -delete to delete garbled files.
[root@s152 ~]# tree --inodes test
test
├── [8576899] a.txt
├── [16851797] level1-1
│ ├── [16878307] bb.txt
│ ├── [25166001] level2-1
│ ├── [ 303089] level2-2
│ │ └── [ 323552] ccc.txt
│ └── [8422822] level2-3
…
12 directories, 3 files
7. Filter empty directories when printing
[root@s152 ~]# tree --prune test
8. Print file attributes, owner, and group when printing and displaying
9. Print the directory first and then the files.
[root@s152 ~]# tree --dirsfirst test
test
├── level1-1
│ ├── level2-1
│ ├── level2-2
│ │ └── ccc.txt
│ ├── level2-3
│ └── bb.txt
├── level1-2
…
12 directories, 3 files
3. Usage syntax and common parameter descriptions
1. Use grammar
#tree [options] [directory]
#tree [options] [directory] -o filename
2. Display parameter description
in the following table
parameter | Parameter Description |
---|---|
-a | List all files. |
-d | Only directories are listed. |
-l | Follow symbolic links like directories. |
-f | Print the full path prefix of each file. |
-x | Just stay on the current file system. |
-L level | Only down level directories deep. |
-R | Rerun the tree when the maximum directory level is reached. |
-P pattern | List only those files matching the given pattern. |
-I pattern | Do not list files matching the given pattern. |
--noreport | Turn off file/directory counting at the end of the tree list. |
--charset X | Use charset X for terminal/HTML and indented line output. |
--filelimit # | Do not have more than # files in the directory. |
--timefmt | Print and format the time according to the <f> format. |
-o filename | Output to file instead of stdout. |
--of | Print directory size. |
--prune | Remove empty directories from output. |
3. File parameter description
parameter | Parameter Description |
---|---|
-q | Print unprintable characters as "?" |
-N | Print unprintable characters as-is. |
-Q | Quote the file name with double quotes. |
-p | Print protection for each file. |
-u | Display the file owner or UID number. |
-g | Displays the file group owner or GID number. |
-s | Print the size of each file in bytes. |
-h | Print dimensions in a way that is easier to read. |
--and | Like -h, but uses SI units (powers of 1000). |
-D | Print the date of last modification or (-c) status change. |
-F | Append "/", "=", "*", "@", " |
--inodes | Print the inode number of each file. |
--device | Print the device ID number to which each file belongs. |
4. Sorting parameter description
parameter | Parameter Description |
---|---|
-v | Sort files alphanumericly by version. |
-r | Sort files in reverse alphanumeric order. |
-t | Sort files by last modified time. |
-c | Sort files by last status change time. |
-U | Leave the file unsorted. |
--dirsfirst | List directories before files (-U disables). |
5. Description of graphics option parameters
parameter | Parameter Description |
---|---|
-i | Do not print indent lines. |
-A | Print ANSI line graphics indented lines. |
-S | Print using ASCII graphic indent lines. |
-n | Always turn off coloring (-C override). |
-C | Shading is always enabled. |
6. XML/HTML option parameter description
parameter | Parameter Description |
---|---|
-X | Print an XML representation of the tree. |
-H baseHREF | Print out the HTML format with baseHREF as the top directory. |
-T string | Replace the default HTML title and H1 title with strings. |
--nolinks | Turn off hyperlinks in HTML output. |
4. Practice of copying directory structure based on tree command
Assume that a system has completed integration testing and needs to be deployed online to the production environment. The system has a large directory for storing files, which contains several subdirectories. A large amount of data was generated during the test, and the total data volume exceeded 5 G. The production environment deployment only needs the directory structure of the stored files and does not need the test files stored in it. What should I do at this time? Here we will introduce using the tree command to write scripts to achieve this requirement.
- View the size of this folder
[root@s152 data]# du -sh *
- Copy directory structure to text document
[root@s152 data]# tree -fid --noreport datafiles -o datafiles.txt
- Copy the text document to the directory path to be stored on the production server
[root@s152 data]# scp datafiles.txt 192.168.0.152:/tmp/
#This is just a simulation test. Just change the IP address to the host and directory you really need to copy to.
- Create a directory using the mkdir -p command
[root@s152 tmp]# mkdir -p
cat datafiles.txt
- Compare the number of directories
[root@s152 tmp]# tree /tmp/datafiles |grep directories
235 directories, 0 files
[root@s152 tmp]# tree /data/datafiles |grep directories
235 directories, 3138 files