463

Is there an equivalent to the Ubuntu tree command for Mac OS X?

lucas
  • 157

14 Answers14

654

You can get the tree command on macOS, too. If you have Homebrew:

brew install tree

If you do not have Homebrew installed, try one approach below.


Installing a package manager approach

Follow the instructions on these websites to install Homebrew, MacPorts, or Fink. Do not install more than one package manager at the same time!

Follow the prompt for whichever you installed.

For Homebrew: brew install tree

For MacPorts: sudo port install tree

For Fink: fink install tree

Installing from source approach

  1. Install the Xcode command line tools by running xcode-select --install.

  2. Download the tree source

  3. Change the Makefile to get it to work, which is also explained in @apuche's answer below. Commenting out the Linux options and uncommenting the macOS options should be enough.

  4. Then, run ./configure, then make.

  5. Now you have to move the tree binary file to a location that's in your executable path. For example:

     sudo mkdir -p /usr/local/bin
     sudo cp tree /usr/local/bin/tree
    
  6. Now edit ~/.bash_profile to include:

     export PATH="/usr/local/bin:$PATH"
    
  7. Reload the shell, and now which tree should point to /usr/local/bin/tree.

slhck
  • 228,104
  • http://shaunchapmanblog.com/post/329270449/how-to-install-the-tree-command-on-mac-os-x also seems to have detailed instructions but may cause issues with llvm-gcc living under /Developer if you are running Xcode 4.x; a bit of fiddling around should do the trick. – Ahmed Masud Nov 21 '11 at 11:04
  • @MishaMoroshko Glad it worked. You'll find many programs on Homebrew, so if you ever miss something you had on Ubuntu or thought only Linux can have, Homebrew should help you! – slhck Nov 21 '11 at 22:15
  • "Problematic" how? – David Moles Jan 21 '15 at 17:54
  • 1
    @DavidMoles Because simply running make on OS X doesn't work. First, you have to know how to install the Xcode command-line tools (or generally, have some idea about building software) and then you'll find that it errors out on an undefined symbol. So you have to do some adjustments to the makefile (as explained here). Too much hassle IMO. – slhck Jan 21 '15 at 22:04
  • +1 for clarifying. :) (The Makefile edit is also documented in this answer below, btw.) – David Moles Jan 22 '15 at 17:38
  • Okay, you do have to install the Xcode Command Line Tools in order to compile programs that you want to install from source on OS X, but because the whole discussion is about a command line tool, namely tree, I would guess that most people have already done that, or can do that very simply. Personally, I hate package managers, so I try to install from source whenever I can. It doesn’t require any more effort to install the Xcode Command Line Tools than it does to install a package manager. Finally, suggesting that people need a package manager to install a simple little program like...
  • – 7stud Apr 09 '15 at 02:32
  • tree is overkill. 2) In Yosemite 10.10.2, make does not error out--it gives a warning, which I just ignored, and the tree command installed just fine. But, the source code can be easily modified to get rid of the warning. See

    comments in the other answer. 3) When you download anything that you want to install, the first step is to read the README file, then the INSTALL file. The INSTALL file for tree asks you to uncomment certain lines in the Makefile depending on what OS you are using. Someone went to a lot of trouble to make that extremely easy. It certainly isn't problematic.

    – 7stud Apr 09 '15 at 02:32
  • 4
    @7stud Many people know a few command line tools for very simple CLI tasks, and they may not know how to compile software. People sometimes struggle to understand what ./configure and make exactly do and why those are needed in the first place. Or they don't want to deal with it. Or read any help files, for that matter. They'd rather do something like apt-get install. It's fine if you prefer installations from source (and I personally do, too), but you have to accept that there are solutions that are perceived easier by others, or perhaps even the majority of normal computer users. – slhck Apr 09 '15 at 06:56
  • @7stud I amended my answer to include more info for both ways of installing tree. – slhck Apr 09 '15 at 07:06
  • 2
    People sometimes struggle to understand what ./configure and make exactly do and why those are needed in the first place I've been installing software from source for 15 years, and I have no idea what ./configure and make do. I just know they are steps I need to perform in order to install software. I blindly read the README and INSTALL files, and I do whatever it says. – 7stud Apr 09 '15 at 17:08
  • This worked, but I had to also do this in order for the link to take effect. – aliteralmind Nov 18 '15 at 17:38
  • Can you get the tree command to ignore certain file types e.g. *.json? – Dhruv Ghulati Aug 03 '16 at 17:51
  • 1
    @DhruvGhulati Not that I know of, but you could inverse-grep it. Like tree | grep -v 'json' or similar. – slhck Aug 04 '16 at 08:20
  • @DhruvGhulati tree --help will list all the options, one of which is -P pattern, which lists only those files that match the pattern, and another is -I pattern which omits the files that match the pattern. So tree -I '*.json' should do what you want. (Btw: I don't know what the pattern syntax is, but I found that this one worked: don't omit the single quotes.) – Steve Powell Feb 28 '17 at 10:33
  • I installed teee from brew and the file /usr/local/bin/tree do exist, but when I type tree the system say -bash: tree: command not found. What is the problem? Thanks in advance. – Evan Hu Jan 07 '19 at 03:15
  • @EvanHu When you run echo $PATH, is the /usr/local/bin path in there? Make sure this directory is part of your PATH and that you haven't overwritten it. Also make sure that the tree file is executable. In case of problems please ask a new question. – slhck Jan 07 '19 at 09:49