top of page
Search

Visualize Your Mac's File Structure with the 'tree' Command



While macOS offers a user-friendly graphical interface for navigating files and folders, sometimes a visual overview of your file hierarchy can prove invaluable. This is where the 'tree' command steps in, providing a clear, concise visual representation of your filesystem's structure directly within the Terminal. To get there, we'll use homebrew to install 'tree' on the Mac:


  • Open Terminal (located in Applications > Utilities).

  • Type the following, and press Enter.

brew install tree

Once installed, from within terminal, type in the following for it to start generating a file tree:

tree > test.txt

The tree command can be run on its own, but by piping it to the test.txt file, it’s possible to then run a tail on the file in another tab:

tail -f test.txt

This will show what’s happening while the test.txt file is being created. Just a warning, this is gonna’ take a hot minute. The default output is really just an ASCII art-based tree of folders and files. Kinda’ hard to read. To customize the output, consider the following options:


  • Show Hidden Files: Add the -a flag: tree -a

  • Limit Depth: Specify the maximum levels to display: tree -L 2 (shows only two levels)

  • Show File Size: Add the -s flag: tree -s

  • Sort Output: Use -d for directories first or -f for files first: tree -d

  • Colorize Output: Enhance readability with colors: tree -C

  • Combine with Other Commands: Filter results using grep or 'find': tree | grep "search_term"


Remember:

  • If not piping the output, use arrow keys to scroll through the output.

  • Press 'q' to quit the 'tree' display.

  • Explore the tree command's manual for more advanced options: man tree


Another way to do this is with the find command. Use find for a bit more granular control and filtering, and to avoid installing more tools via brew. The basic usage is really just find with a . (especially if the working directory is /), as follows:

find .

Consider using “-type f” at the end of the command to only list files:

find . -type f

Or if looking for an expression, use that - like in the name of a file use -name following by a string that will match, like all txt files:

find . -name “*txt”

Combine options, as well. like -type d with -name *data to find directories with the name data in them  or -size and -mtime for size and modification date filters, respecitively. And as with tree, output can be piped to other tools to do things like use grep for further filtering.


The tree and find commands have extensive options and capabilities. Check out their man pages for comprehensive details and examples. Also, mtree comes built into the operating system, but it’s hard to teach old dogs new tricks!

50 views0 comments

Recent Posts

See All

Comments


bottom of page