You certainly know the old tree command.

Or maybe you don't, because it's not installed by default anymore on new distributions.

Why ? Probably because it never kept the promise of an overview of the current directory as a tree.

Try it on any directory whose content you want to see: you'll see only the tail of a long list and you'll learn nothing.

But there's a better solution, a command you can install to get a better tree.

As you'll have it aliased to tree, that's how I'll call it from now, the better tree.

Usage Overview

Just the tree

Assuming we have a small terminal, in any directory, launch our better tree:

tree

The overview fits the terminal, you don't have to scroll.

All first-level directories are shown, because their count is smaller than the count of available lines.

Some of those directories have "unlisted": not everything in them can be displayed.

And the second-level directories are all followed by an ellipsis to show that their content isn't displayed.

If there was less, or more, content, it could have been different but

  1. the display always fits the screen
  2. you get a reasonnable overview of the content of the current directory

Technically, this representation is based on a "Breadth-First Search" exploration of the directory.

Show sizes

Our better tree function can show sizes, including directory size (recursively computed):

Run tree -s

tree-s

Show hidden and ignored files

If you pay close attention, you may notice that sizes in the last screenshot don't add up: the total directory size is 36M and is clearly bigger than the sum of files inside.

The reason is that tree, by default, don't show hidden and ignored files.

-h will show hidden files, and -i will show files ignored because of a .ignore or .gitignore file.

Let's check with -shi:

tree-shi

Show more information

Other available information include the permissions (on unix) and the date (of the most recently file for directories).

So you may run eg tree -sdp

tree-sdp

Set the height

Maybe you just want a very small overview that will stay readable when you type other commands ?

Or maybe you want a tree tailored to a given height so that you can paste it in a document ?

Whatever the reason, you may specify the height with eg tree --height 15:

tree--height

Note: What you really specify is the "available height", which includes the previous and following prompts. If you want a specific number of lines, add 2 to that number.

Install

This better tree function is based on the broot command. Install it.

Once it's installed, tree can be defined as a shell function.

For example in your .bashrc or .zshrc:

function tree {
     br -c :pt "$@"
}

Interactive Exploration

As you saw, the tree function is just calling broot with the instruction to print the tree (:pt).

This tree function is useful as a replacement of the standard tree command, but when you want to explore or modify the directory, search, preview, etc. then you should use broot which leverages the same idea of a BFS overview in a full file manager.