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

Sep 9, 2016 - 17 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.

.

Related articles:

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

17 Comments

» Comments RSS Feed

  1. Lars Paulsen says:

    On my MacBookPro M1 Max, I did the following in order to get the “tree” effect without installing the software.

    Open the file ~/.zshrc

    $ nano ~/.zshrc

    Put in this line at the end of the file.

    tree(){ find “$1” -print | sed -e ‘s;[^/]*/;|____;g;s;____|; |;g’ }

    Result.

    alias ll=’ls -lah -FG’
    mkdircd(){ mkdir “$1” && cd “$1” ; }
    tree(){ find “$1” -print | sed -e ‘s;[^/]*/;|____;g;s;____|; |;g’ }

    Click ^(Control) X to Exit.

    Click Y to save.

    Open a new terminal window.

    Now you can use “tree” like this.

    ./
    ../
    dir/
    $ tree dir

    Output

    dir
    |____dir2
    | |____dir3
    | | |____dir4
    |____dir3
    | |____dir4
    | | |____dir5
    |____dir1
    | |____dir2
    | | |____dir3

    Hope this helps.

  2. Bucky says:

    Thanks for this _and_ thanks for keeping it online for such an extended period. I needed to share directory information with user(s) unfamiliar with my sense of organization and “tree” does just that.

  3. Pablo says:

    Does anyone know why the tree of the directory is not displaying files in the proper order? files are named 001, 002, etc.

  4. brilliant says:

    Fantastic tip. Was disappointed tree isn’t in mac os (sierra) but this alias worked in a pinch. Needed a visual of an old website with way too much detritus – now found it all

  5. HappyUser says:

    Thank you very much for share your knowledge.

  6. Ali says:

    Thank you :)

  7. 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!

    • Nuno says:

      Hello,

      for whoever is looking for the answer to this. For using the alias and passing the desired directory as a parameter, using $1 in the alias directly doesn’t work indeed, but you can use a function instead. Replace the alias line in your .bashrc (.bash_profile on MacOS) by:
      “`
      function __tree { find $1 -print | sed -e ‘s;[^/]*/;|____;g;s;____|; |;g’; }
      alias treed=’__tree’
      “`

      You can then call in the terminal:
      “`
      $ treed
      “`

  8. Joe says:

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

  9. 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.

      • Tim says:

        You do remote administration of Macs using tree(1)? You use tree(1) output for scripting? You call it from perl/python?

        There are still reasons to use the Terminal, but none of the reasons given here makes any sense for the tree(1) command.

        • Ben says:

          FWIW, I am using this to aid in my team’s refactoring effort to reorganize most of our codebase (it’s a long story). I wanted to print out all the files in a directory so that my team and I can identify where each belongs in the new structure and document it.

          This alias is extremely helpful, thank you!

    • 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.

      • Tim says:

        Have you used ‘dd’, ‘find’, ‘openssl’, ‘git’? I don’t know how you can claim that command line interfaces don’t require design. There are some truly atrocious ones.

  10. cynsa says:

    yay tree! thank you!

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

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

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site