How to display directory tree in Mac Terminal


tree is a recursive directory displaying command that helps to see the in depth indented listing of directories and files.

How to install tree command on macOS Terminal?

You can make use of Home-brew to install the tree package,

  1. Open Terminal,
  2. Type command: brew install tree
Logs:
code2care@mac Desktop % brew install tree
==> Downloading https://homebrew.bintray.com/bottles/tree-1.8.0.arm64_big_sur.bottle.tar.gz
Already downloaded: /Users/code2care/Library/Caches/Homebrew/downloads/ e446cb0fe0006900ce03f20b28170d16fc298e8ffad73a071f4d603df97ecb58 --tree-1.8.0.arm64_big_sur.bottle.tar.gz

==> Pouring tree-1.8.0.arm64_big_sur.bottle.tar.gz
🍺  /opt/homebrew/Cellar/tree/1.8.0: 8 files, 158.5KB

How to use tree command?

Just be in the dir where you want to see the structure of files and folders and type tree,

% tree
 .
├── Java
├── My\ Books
├── Python\ Files
└── Samples
    └── Sub-folder
4 directories, 1 file

As you can see when I ran the tree command on the Desktop - it displayed all the folders and files it contains, with a summary of how many files are folders are present in Desktop. The dot represents the root. and you can see the files or folders within a file with an indent.

If you want to restrict the number of levels you want to see you can use -L option followed the level number to reduce the depth.

Example:
code2care@mac Desktop % tree -L 1
.
├── Java
├── My\ Books
├── Python\ Files
└── Samples

4 directories, 0 files
Install tree command in Mac
Install tree command in Mac





      Topics: Pages | 2015 | Tutorials | Questions | Python