Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there any equivalent of the Ubuntu tree command for Mac OS X ?

share|improve this question

migrated from stackoverflow.com Nov 21 '11 at 10:41

This question came from our site for professional and enthusiast programmers.

8 Answers 8

up vote 124 down vote accepted

Yes, there is, and it's called tree.

You can install it

  • from source, which could be problematic

  • by using any package manager, like Homebrew, MacPorts, or Fink (sorted in the order I would recommend). Install either of those first — just install one, not all.

    Then run one of the following commands, depending on which package manager you chose.

    brew install tree
    sudo port install tree
    fink install tree
    
share|improve this answer
    
shaunchapmanblog.com/post/329270449/… 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
    
@slhck: Thanks! Installing Homebrew + brew install tree worked like a charm :) –  Misha Moroshko Nov 21 '11 at 22:13
    
@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

Hi: There isn't a tree command per se however you can do this:

Save the following script to /usr/local/bin/tree

#!/bin/bash

SEDMAGIC='s;[^/]*/;|____;g;s;____|; |;g'

if [ "$#" -gt 0 ] ; then
   dirlist="$@"
else
   dirlist="."
fi

for x in $dirlist; do
     find "$x" -print | sed -e "$SEDMAGIC"
done

chmod 755 /usr/local/bin/tree

and enjoy

Of course you may have to create /usr/local/bin

share|improve this answer
    
It misses all the options of tree, but still a nice little solution. –  slhck Nov 21 '11 at 11:01
    
@slhck hehe it was a quick hack solution... –  Ahmed Masud Nov 21 '11 at 11:05
    
You might want to quote your variables though. –  slhck Nov 21 '11 at 11:12
    
$x should be $dirlist shouldn't be quoted... because of the special expansion of "$@" –  Ahmed Masud Nov 21 '11 at 11:24
1  
@JenS. of course you can simply change the find command to deal with that –  Ahmed Masud Sep 24 at 20:41

Or if your administrator won't let you install any of the brew, fink, port tools you can always build it from the source :

curl -O ftp://mama.indstate.edu/linux/tree/tree-1.5.3.tgz
tar xzvf tree-1.5.3.tgz
cd tree-1.5.3/
ls -al

Edit the Makefile to comment linux part and uncomment osx area:

# Linux defaults:
#CFLAGS=-ggdb -Wall -DLINUX -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
#CFLAGS=-O2 -Wall -fomit-frame-pointer -DLINUX -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
#LDFLAGS=-s

# Uncomment for OS X:
CC=cc
CFLAGS=-O2 -Wall -fomit-frame-pointer -no-cpp-precomp
LDFLAGS=
XOBJS=strverscmp.o

And while you're at it, if you want to force tree to always colorize the output, you can always edit the main method of the tree.c file and add force_color=TRUE before setLocale(LC_TYPE,"");

Finally hit make and you're done building tree for mac.

Tribute goes to Shaun Chapman for his original post on his blog.

share|improve this answer
    
Couldn't get to Shaun's website; thanks for the heads up on modifying the Makefile. –  Paul Nathan Oct 26 '12 at 14:27
    
Specifically, you can add force_color = TRUE;. No semicolon and you get a compile error. –  tgrosinger Sep 6 '13 at 15:55
1  
I've just built version 1.7 and instead of setLocale its setlocale. So in tree.c, you look for setlocale(LC_TYPE,""); and thanks! –  Avi Cohen May 2 at 9:49
    
Great answer. Can you add a note on how to move it to /bin (or some path so that it can be used globally)? –  Khanh Nguyen Oct 10 at 4:57

Not exactly the same, but one quick way on the Mac is:

find .

and that's it. It will list all file paths in the current directory as a list.

share|improve this answer

An alternative using find and awk:

#!/bin/bash
find . -print 2>/dev/null | awk '!/\.$/ { \
    for (i=1; i<NF; i++) { \
        printf("%4s", "|") \
    } \
    print "-- "$NF \
}' FS='/'
share|improve this answer

It's not as pretty as gnu tree ... but it's real easy to alias in bash ... You can even add a little color by tacking the G option on to osx's ls color.

alias tree='find . -type d | ls -lARG'
share|improve this answer

I added the following to ~/.bash_profile for use in Terminal.app. Some comments are included to help remember how find is being used.

##########
## tree ##
##########
## example ...
#|____Cycles
#| |____.DS_Store
#| |____CyclesCards.json
#| |____Carbon
#| | |____Carbon.json
# alternate: alias tree='find . -print | sed -e "s;[^/]*/;|____;g;s;____|; |;g"'
# use$ tree ; tree . ; tree [some-folder-path]
function tree {
    find ${1:-.} -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
}

example for the current directory

$> tree

example for some path

$> tree /some/path
share|improve this answer

Here is a Ruby script solution that produces a nice Unicode tree along with useful metadata.

#!/usr/bin/env ruby
def tree_hierarchy( root, &children )
  queue = [[root,"",true]]
  [].tap do |results|
    until queue.empty?
      item,indent,last = queue.pop
      kids = children[item]
      extra = indent.empty? ? '' : last ? '└╴' : '├╴'
      results << [ indent+extra, item ]
      results << [ indent, nil ] if last and kids.empty?
      indent += last ? '  ' : '│ '
      parts = kids.map{ |k| [k,indent,false] }.reverse
      parts.first[2] = true unless parts.empty?
      queue.concat parts
    end
  end
end
def tree(dir)
  cols = tree_hierarchy(File.expand_path(dir)) do |d|
    File.directory?(d) ? Dir.chdir(d){ Dir['*'].map(&File.method(:expand_path)) } : []
  end.map do |indent,path|
    if path
      file = File.basename(path) + File.directory?(path) ? '/' : ''
      meta = `ls -lhd "#{path}"`.split(/\s+/)
      [ [indent,file].join, meta[0], meta[4], "%s %-2s %s" % meta[5..7] ]
    else
      [indent]
    end
  end
  maxs = cols.first.zip(*(cols[1..-1])).map{ |c| c.compact.map(&:length).max }
  tmpl = maxs.map.with_index{ |n,i| "%#{'-' if cols[0][i][/^\D/]}#{n}s" }.join('  ')
  cols.map{ |a| a.length==1 ? a.first : tmpl % a }
end
puts tree(ARGV.first || ".") if __FILE__==$0

You could modify the meta = … line to extract different metadata to display, hand-picking out the split pieces on the next line. With a little more work you could pass arbitrary ls arguments to select the metadata to show.

Sample output (looks nicer in the OS X terminal than the font on Stack Overflow):

phrogz$ tree UCC_IVI/
UCC_IVI/                               drwxr-xr-x  510B  Nov 20 11:07
  ├╴docs/                              drwxr-xr-x  102B  Nov 20 19:21
  │ └╴CANMessages.txt                  -rwxr-xr-x  2.2K  Nov 20 19:21
  │ 
  ├╴effects/                           drwxr-xr-x  204B  Nov 19 17:19
  │ ├╴Depth Of Field HQ Blur.effect    -rwxr-xr-x  2.4K  Nov 19 17:19
  │ ├╴FXAA.effect                      -rwxr-xr-x  1.6K  Nov 17 15:38
  │ ├╴HDRBloomTonemap.effect           -rwxr-xr-x   11K  Nov 17 15:38
  │ └╴SMAA1X.effect                    -rwxr-xr-x  4.4K  Nov 19 17:19
  │ 
  ├╴fonts/                             drwxr-xr-x  136B  Nov 17 15:38
  │ ├╴Arimo-Regular.ttf                -rwxr-xr-x   43K  Nov 17 15:38
  │ └╴OFL.txt                          -rwxr-xr-x  4.3K  Nov 17 15:38
  │ 
  ├╴maps/                              drwxr-xr-x  238B  Nov 19 17:19
  │ ├╴alpha-maps/                      drwxr-xr-x  136B  Nov 17 15:38
  │ │ ├╴rounded-boxes-3.png            -rwxr-xr-x  3.6K  Nov 17 15:38
  │ │ └╴splatter-1.png                 -rwxr-xr-x   35K  Nov 17 15:38
  │ │ 
  │ ├╴effects/                         drwxr-xr-x  136B  Nov 19 17:19
  │ │ ├╴AreaTex-yflipped.dds           -rwxr-xr-x  175K  Nov 19 17:19
  │ │ └╴SearchTex-yflipped.png         -rwxr-xr-x  180B  Nov 19 17:19
  │ │ 
  │ ├╴IBL/                             drwxr-xr-x  136B  Nov 17 15:38
  │ │ ├╴028-hangar.hdr                 -rwxr-xr-x  1.5M  Nov 17 15:38
  │ │ └╴FieldAirport.hdr               -rwxr-xr-x  1.5M  Nov 17 15:38
  │ │ 
  │ ├╴icons/                           drwxr-xr-x  238B  Nov 19 17:19
  │ │ ├╴icon_climate.dds               -rwxr-xr-x  683K  Nov 19 17:19
  │ │ ├╴icon_music.dds                 -rwxr-xr-x  683K  Nov 19 17:19
  │ │ ├╴icon_navigation.dds            -rwxr-xr-x  683K  Nov 19 17:19
  │ │ ├╴icon_phone.dds                 -rwxr-xr-x  683K  Nov 19 17:19
  │ │ └╴icon_surroundView.dds          -rwxr-xr-x  683K  Nov 19 17:19
  │ │ 
  │ └╴materials/                       drwxr-xr-x  102B  Nov 19 17:19
  │   └╴spherical_checker.png          -rwxr-xr-x   11K  Nov 19 17:19
  │   
  ├╴materials/                         drwxr-xr-x  102B  Nov 19 17:19
  │ └╴thin_glass_refractive.material   -rwxr-xr-x  6.0K  Nov 19 17:19
  │ 
  ├╴models/                            drwxr-xr-x  136B  Nov 19 17:19
  │ ├╴BokehParticle/                   drwxr-xr-x  136B  Nov 19 17:19
  │ │ ├╴BokehParticle.import           -rwxr-xr-x  739B  Nov 19 17:19
  │ │ └╴meshes/                        drwxr-xr-x  102B  Nov 19 17:19
  │ │   └╴Mesh.mesh                    -rwxr-xr-x  1.1K  Nov 19 17:19
  │ │   
  │ └╴Glass_Button/                    drwxr-xr-x  136B  Nov 19 17:19
  │   ├╴Glass_Button.import            -rwxr-xr-x  1.2K  Nov 19 17:19
  │   └╴meshes/                        drwxr-xr-x  136B  Nov 19 17:19
  │     ├╴GlassButton.mesh             -rwxr-xr-x   44K  Nov 19 17:19
  │     └╴Icon.mesh                    -rwxr-xr-x  1.8K  Nov 19 17:19
  │     
  ├╴scripts/                           drwxr-xr-x  204B  Nov 19 17:19
  │ ├╴App.lua                          -rwxr-xr-x  764B  Nov 17 15:38
  │ ├╴CANSim.lua                       -rwxr-xr-x   29K  Nov 17 15:38
  │ ├╴ObjectWiggler.lua                -rwxr-xr-x  3.7K  Nov 19 17:19
  │ └╴PathWiggler.lua                  -rwxr-xr-x  2.9K  Nov 17 15:38
  │ 
  ├╴states/                            drwxr-xr-x  170B  Nov 19 18:45
  │ ├╴app-camera.scxml                 -rwxr-xr-x  2.4K  Nov 20 11:07
  │ ├╴app-navigation.scxml             -rwxr-xr-x  590B  Nov 19 18:32
  │ └╴logic.scxml                      -rwxr-xr-x  4.2K  Nov 19 18:59
  │ 
  ├╴tests/                             drwxr-xr-x  102B  Nov 17 15:38
  │ └╴interface-navigation.scxml-test  -rwxr-xr-x   83B  Nov 17 15:38
  │ 
  ├╴UCC_IVI.uia                        -rwxr-xr-x  630B  Nov 19 17:32
  ├╴UCC_IVI.uia-user                   -rwxr-xr-x  832B  Nov 20 17:22
  ├╴UCC_IVI.uip                        -rwxr-xr-x  1.5K  Nov 17 15:38
  └╴UCC_Menu.uip                       -rwxr-xr-x   33K  Nov 19 17:19
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.