tree Command in Linux

Topics Covered

Overview

In the vast world of Linux, understanding the file system and navigating directories efficiently is crucial. One command that greatly assists in this endeavor is the 'tree' command in Linux. The tree command is a powerful tool that visually displays the directory structure of a file system in a tree-like format. It's an excellent way to get a quick overview of how directories and files are organized on your system.

Syntax of tree Command in Linux

The basic syntax of the tree command in Linux is as follows:

Where:

  • options: These are optional and represent the flags used to alter the behavior of the tree command. Examples include -a (all files), -d (directories only), -l (follow symbolic links), etc.
  • directory: This is the starting point for the tree command. If not specified, the tree command starts from the current directory.

Options in tree Command in Linux

  1. -a: Displays all files, including hidden ones.

    For example -

    Output:

    By default, tree does not display hidden files. The -a option instructs tree to also include these in the output.

  2. -d: Displays directories only.

    For example -

    Output:

    This option is used when the user is only interested in the directory structure, ignoring individual files.

  3. -L level: Limits the depth of the directory tree to 'level'.

    For example -

    Output:

    This command is handy when dealing with deeply nested directories and you only want to see a limited depth.

Example Usages

  • Display the directory structure for the current directory:

    Output:

    Explanation: Without any arguments, tree will output the entire directory and file structure for the current directory.

  • Display the directory structure for a specific directory:

    Output:

    Explanation: By specifying a directory, tree will output the directory and file structure for that directory.

Tips

  • You can use the tree command with the pipe (|) and less or more commands for better navigation for large directories: tree | less

  • To save the output of the tree command to a file, use redirection: tree > output.txt

Advanced Use Cases of tree Command in Linux

  • Display the size of files and directories:

    Output:

    Explanation: The --du option calculates the size of each directory as the accumulation of sizes of its containing files and sub-directories. The -h option ensures sizes are presented in a 'human-readable' format (e.g., KB, MB).

  • Filter the tree command output by a pattern:

    Output:

    Explanation: The -P option allows you to filter the output by a pattern. In this case, only .txt files are displayed in the output.

  • Using tree command with file report:

    Output:

    Explanation: The -D option prints the last modification time for files and directories, -f prints the full path prefix for each file, -i makes tree not print the indentation lines, and --filelimit does not descend directories containing more than the specified number of files.

Conclusion

  • The tree command in Linux is a powerful tool for visualizing the directory structure in a tree-like format.

  • A variety of options, such as -a, -d, and -L, allow you to customize the tree command's output to suit your needs.

  • Advanced usage of the tree command allows for even more flexibility and utility, including filtering output by pattern and showing file sizes.