Print Linux Directory Structure as a Tree


Introduction

Linux is an open-source operating system that has gained a lot of popularity among developers and system administrators. It offers various powerful tools and commands to manage files, directories, and other resources on system. One such command that comes in handy is tree command, which helps in displaying directory structure of system in a tree-like format. In this article, we will explore tree command and its usage in detail.

What is tree command?

The tree command is a command-line tool that displays directory structure of a file system in a tree-like format. It shows nested structure of directories, sub-directories, and files in a hierarchical tree-like structure. tree command is available on most Linux distributions and can be installed on others using package manager.

The syntax of tree command is as follows −

tree [options] [directory]

Options − tree command provides various options that can be used to customize output. Some of commonly used options are −

  • -d or --dirs-only − It displays only directories and not files.

  • -L level or --max-depth level − It limits depth of tree to specified level.

  • -a or --all − It displays hidden files and directories as well.

  • -I pattern or --exclude pattern − It excludes files and directories that match specified pattern.

Directory − directory argument specifies directory for which tree structure needs to be displayed. If no directory is specified, it defaults to current directory.

Examples of using tree command

Let's look at some examples of how to use tree command to print directory structure of a Linux system.

Example 1: Display directory structure of current directory

To display directory structure of current directory, open terminal and type following command −

tree

This will display directory structure of current directory in a tree-like format. By default, it will display all files and directories in current directory.

Example 2: Display only directories in directory structure

To display only directories in directory structure, use -d option as follows −

tree -d

This will display only directories in current directory in a tree-like format.

Example 3: Limit depth of tree structure

To limit depth of tree structure, use -L option followed by depth level as follows −

tree -L 2

This will limit depth of tree structure to 2 levels. It will display directory structure of current directory and its immediate sub-directories only.

Example 4: Display hidden files and directories

To display hidden files and directories in directory structure, use -a option as follows −

tree -a

This will display all files and directories in current directory, including hidden ones.

Example 5: Exclude files and directories from directory structure

To exclude files and directories from directory structure, use -I option followed by pattern to exclude as follows −

tree -I "*.txt"

This will exclude all files and directories that match *.txt pattern from directory structure.

Advantages of using tree command

The tree command offers various advantages that make it a useful tool for system administrators and developers. Some of advantages are −

  • Helps in understanding directory structure − tree command helps in visualizing directory structure of a system in a hierarchical tree-like format. It makes it easier to understand nested structure of directories and sub-directories.

  • Customizable output − tree command provides various options to customize output.

  • Easy to use − tree command is easy to use and requires minimal command-line knowledge. It provides a simple way to display directory structure of a system.

  • Saves time − tree command saves time by displaying directory structure in a tree-like format. It helps in quickly identifying location of files and directories in system.

  • Portable − tree command is available on most Linux distributions and can be installed on others using package manager. It is a portable tool that can be used across different Linux systems.

Advanced Usage of tree command

Apart from basic usage of tree command, there are several advanced use cases where tree command can be a very helpful tool. Let's look at some of them.

Export directory structure to a file

The tree command can be used to export directory structure to a file in a text format. This can be helpful in generating a report or documenting directory structure. To export directory structure to a file, use -o option followed by file name as follows −

tree -o directory_structure.txt

This will export directory structure to a file named directory_structure.txt in current directory.

Display directory structure in color

The tree command can display directory structure in different colors to make it more visually appealing. This can be helpful in identifying directories and files based on their type or permissions. To display directory structure in color, use -C option as follows −

tree -C

This will display directory structure in color in terminal.

Display directory structure in XML format

The tree command can also display directory structure in XML format. This can be helpful in integrating directory structure with other tools or systems. To display directory structure in XML format, use -X option as follows −

tree -X

This will display directory structure in XML format in terminal.

Display directory structure in ASCII format

The tree command can also display directory structure in ASCII format. This can be helpful in generating a report or documentation that can be easily printed. To display directory structure in ASCII format, use -A option as follows −

tree -A

This will display directory structure in ASCII format in terminal.

Conclusion

The tree command is a powerful tool for displaying directory structure of a Linux system in a hierarchical tree-like format. It offers various options to customize output and make it easier to understand nested structure of directories and sub-directories. tree command is easy to use and saves time in identifying location of files and directories in system. It is a portable tool that can be used across different Linux systems.

Updated on: 24-Mar-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements