DEV Community

Cover image for Basic Linux command (tree)
Cheulong Sear
Cheulong Sear

Posted on

Basic Linux command (tree)

tree command a utility used to display the contents of directories in a tree-like, hierarchical format.

tree command may not be pre-installed on all Linux distributions. To install it use

# Debian/Ubuntu sudo apt install tree 
Enter fullscreen mode Exit fullscreen mode
# Display current directory tree # Display a specific directory tree /path/to/directory 
Enter fullscreen mode Exit fullscreen mode

1

Use -d to show only directory

cheulong@master-node:~/parent$ tree -d . ├── child1 ├── child2 ├── child3 ├── child4 └── child5 
Enter fullscreen mode Exit fullscreen mode

Use -f to show full path prefix for each file

cheulong@master-node:~/parent$ tree -f . ├── ./child1 │   └── ./child1/text.txt ├── ./child2 ├── ./child3 ├── ./child4 └── ./child5 
Enter fullscreen mode Exit fullscreen mode

Use -p to show permission for each file

cheulong@master-node:~/parent$ tree -p [drwxrwxr-x] . ├── [drwxrwxr-x] child1 │   └── [-rw-rw-r--] text.txt ├── [drwxrwxr-x] child2 ├── [drwxrwxr-x] child3 ├── [drwxrwxr-x] child4 ├── [drwxrwxr-x] child5 └── [-rw-rw-r--] file 
Enter fullscreen mode Exit fullscreen mode

Use -L <level to limit the display depth to a specified level.

cheulong@master-node:~/parent$ tree -L 1 . ├── child1 ├── child2 ├── child3 ├── child4 ├── child5 └── file 
Enter fullscreen mode Exit fullscreen mode

Use -sh to show size for each file

cheulong@master-node:~/parent$ tree -sh [4.0K] . ├── [4.0K] child1 │   └── [ 0] text.txt ├── [4.0K] child2 ├── [4.0K] child3 ├── [4.0K] child4 ├── [4.0K] child5 └── [ 225] file 
Enter fullscreen mode Exit fullscreen mode

Other Option

-a: Include hidden files and directories (those starting with a dot).
-F: Append a forward slash (/) to directory names and an asterisk (*) to executable files.
--prune: Exclude empty directories from the output.
-u: Print the username (or UID) of each file.
-g: Print the group name (or GID) of each file.

Leave a comment if you have any questions.

===========
Please keep in touch
Portfolio
Linkedin
Github
Youtube

Top comments (0)