Using a Mac Equivalent of Unix “tree” Command to View Folder Trees at Terminal

Sep 9, 2016 - 9 Comments

View Folder Tree Mac Command Line

Mac users who come from a unix background may appreciate knowing how to implement the equivalent to the Unix “tree” command in macOS and Mac OS X. There are actually a few different ways to show a folder tree in the Terminal of Mac OS X, we’ll cover an easy tree equivalent achieved through an alias, as well as how to install native ‘tree’ on a Mac just like what you see in Ubuntu or elsewhere in Linux.

This is obviously aimed at command line users, but if you spend more time in the Finder of the Mac you might appreciate listing files and folder contents recursively there, which can be similar but is obviously not displaying a directory tree at the Terminal.

Make a tree Equivalent to View Folder Trees in Terminal for Mac OS X

A simple alias will allow you to view folder trees from the command line of Mac OS:

  1. Launch Terminal or iTerm if you haven’t done so already
  2. Open your .bashrc or .zshrc profile in your preferred text editor, we’re using nano because nano is easy:
  3. nano .zshrc

  4. On a new line, paste the following alias:
  5. alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"

  6. Hit Control+O and Control+X to save and exit out of nano (or quit from vim or emacs as usual), your tree command for printing directory trees is now ready to use

View folder tree on Mac with tree alias

Open a new Terminal or reload your Terminal profile and you’re ready to use the new tree alias.

Showing Directory Tree Structure with ‘tree’ on the Mac

Now that you have your alias implemented, you can use the ‘tree’ command to show the hierarchical structure of the present working folder or directory at the command line. For example, if you’re in the root / of a Mac and hit ‘tree’, you will display the hierarchical structure of everything on the Mac (this will take a while and is not recommended, but offers a demonstration of how it works)

tree

The tree command is really best used in subdirectories with some level of containment otherwise you’ll be dumping the structure of the entire filesystem outward from the present working directory.

Mac tree equivalent command line show directory tree

Installing ‘tree’ for the Mac Command Line

If you want a little more control over ‘tree’ like the ability to specify a directory, or you just want the exact ‘tree’ equivalent that comes from the unix world, you can use homebrew or macports to install tree directly in macOS and Mac OS X:

Installing ‘tree’ with Homebrew:

brew install tree

Installing ‘tree’ with MacPorts:

sudo port install tree

My preference is towards Homebrew but use whichever works for you. Once installed from either, typing ‘tree’ will display the folder tree of any directory on the Mac.

Note to avoid conflict, you won’t want to use a tree alias in the first step and then also install the tree command. You can implement both, but you’d probably want to rename the alias to ‘treed’ or something similar.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS X, Tips & Tricks

9 Comments

» Comments RSS Feed

  1. cynsa says:

    yay tree! thank you!

  2. Ted says:

    Humanity is quite interesting. In the 80s, we were sitting behind the green or orange monochromatic displays typing esoteric commands, listing directories. But we wanted more. Better. And lo and behold, the graphical UI was invented. With a mouse. Yay! As time went by, the UI became more refined, more beautiful.

    And then humanity (…ok, just some people…) said, that SUCKS! We need to go back to the 80s and look at the monochromatic displays and typing esoteric terminal commands and the prompt. Yay!

    I just couldn’t figure out why people haven’t tossed their mobile phones yet and switched over to rotary phones.

    • Paul says:

      True and for most Mac users the Terminal is just going to over complicate their use. A lot of advanced users regularly use the command line though, either for remote administration, or for scripting, easy access to perl, python, git, versioning, web servers, etc. It’s not for everyone, but for many power users the command line use is necessary and essential. In fact, Terminal in MacOS is the reason some use a Mac rather than a Linux machine or Windows machine, which is kind of interesting to think about.

    • Dan says:

      Try administering more than a handful of Macs and you see why the terminal is available. There are things you can do in there that are inaccessible from the GUI.

    • Dimitris says:

      I am afraid that’s not the case. GUIs have won the war since day 1 and there is no going back. Terminal vs GUI will always win GUI and people will always prefer GUI.

      The problem however in Terminal vs GUI is that it implies that GUI exists for that existing comparison.

      Unfortunately nothing exists without its pros and cons. GUIs are lengthy and difficult to make. Take a look at most pro apps out there and you should not be suprised to find out that almost 50% of the codebase is dedicated just to the GUI.

      To make things even worse GUIs require a designer skill, making the code simply is not enough. Its an optical interface so design will always play a vital role.

      Coders do not like to design, write a lot of code and foremost maintain a lot of code.

      On the other hand even a baby can make Terminal interface, it requires minimal code, its low on the technical side and requires zero designer skills.

      Guess which one devs prefer to make.

      Hence why developers when they make an interface they start with a terminal. Because devs make many more terminal interfaces than GUIs , especially when it comes to apps that the average user most likely will not use, they can easily get away with. However when it comes to ease of use and power GUIs win every race by a long margin so its extremely rare nonwdays to find an app that its main interface in terminal one and is targeted towards non coders.

      So the love of terminal is nothing more than love of convenience and the fact because terminal interfaces are much easier to make they developers have time to focus more on features than looks which make terminal apps usually far more powerful than its GUI counterparts.

      If your most powerful tools are terminal tools , then yes you will heavily rely on terminal.

  3. Joe says:

    An easier solution to ‘tree’ is to just drag any ‘directory/folder’ into an open window of Text Wrangler or BBEdit… voila !

  4. Amer Neely says:

    I love your ‘tree’ alias and took your advice to name it ‘treed’. Also installed ‘tree’ via homebrew which I use for other packages.

    A subtle difference between the two is that you must be in the directory you want listed in order to use the alias. I tried editing the alias to use $1 instead of the ‘.’ as an argument to find but that does not work. How can I do that? I want to be able to pass a Volume and or directory to my alias and redirect it to a file in my current directory.

    Thanks, eh!

  5. Ali says:

    Thank you :)

  6. HappyUser says:

    Thank you very much for share your knowledge.

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Recent Posts