osx - Mac OS X equivalent of the Ubuntu "tree" command

20
2014-04
  • Misha Moroshko

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

  • Answers
  • slhck

    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
      
  • Ahmed Masud

    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

  • apouche

    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.

  • jweyrich

    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='/'
    
  • Eddie B

    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'
    

  • Related Question

    Software to generate taxonomy trees
  • Arlen Beiler

    Is there any software that automatically generates something like this from a list? enter image description here


  • Related Answers
  • Dennis Williamson

    You can use the tools that are part of Graphviz. You would have to covert your list to the dot language, but that's fairly simple to do.

    File: list.dot

    digraph relationship {
        One -> Parent
        Two -> Parent
        Three -> Parent
        Four -> Parent
        Five -> Parent
        Six -> Parent
        Seven -> Parent
    }
    

    Produce output:

    dot -Tpng -o graphvizexample.png list.dot
    

    Output:

    Graphviz Example

    You can do a lot of customization of the appearance. This is just a simple example.