Adding Tree command to the Terminal (Mac OSX)

Yesterday I saw a tutorial where a person was using the Terminal and navigated (using ‘cd’) to get into a folder. He then typed ‘tree’ and a visual representation of the file/folder structure appeared. It was pretty neat. Of course I ran home to open my Terminal and I typed ‘tree’ and received.

Terminal screen shot showing 'tree' is not a recognized command
Terminal screen shot showing ‘tree’ is not a recognized command

Did you know? One of the coolest things about the Windows Command Line (usually it’s someone bragging about the Terminal) is the ‘tree’ command. It’s native to the system. Point to the folder you want and type ‘tree’ and you’ll get the folder/file structure of that folder (options found here).

So I looked on line and I found this great article that showed me how to add it. Just open the Terminal and add this code:

find . -print | sed -e ‘s;[^/]*/;|____;g;s;____|; |;g’

If you want to print that tree structure to a file called textfile.txt change your code to:

find . -print | sed -e ‘s;[^/]*/;|____;g;s;____|; |;g’ > textfile.txt

If you know how to edit your .~/profile, open your .~/profile and add this:

function tree {
find ${1:-.} -print | sed -e ’s;[^/]*/;|____;g;s;____|; |;g’

Then you can just type ‘tree’ and the folder path and you’ll get the tree visual print out every time.

8 Replies to “Adding Tree command to the Terminal (Mac OSX)”

  1. Nice tip! I was just looking to do exactly the same thing having seen it in a tutorial. The command works but if I add it to my .profile file and run the ‘tree’ command from the terminal I get nothing. I’m using zsh rather than bash and a have also tried placing the function in my .zshrc file but no joy. Any ideas how I can get this working with zsh?

    1. Hey Ian, I think this is exactly what you’re looking for: I’m just a newbie with Mac’s and the Terminal but I’m loving the power and control that comes with it. I just bought a book on zsh and I’m trying to find great resources online. Please forward any that you find. Sadly, most of the Terminal info I find online, is overloaded with geek terminology, poorly written or just too complicated to understand. I admit it, I’m slow on the update but I’m smart enough to know the Terminal is awesome and I want to learn as much as I can about it.

  2. Thanks for the link. There’s plenty of useful tips and tricks in there for zsh. I’ve just switched over from bash and only just getting familiar with it but it’s largely the same as bash. One tip would be to install homebrew ( It’s the best Mac package manager out there and makes installation of common packages a breeze whilst also keeping everything tidy. Then you can simply run

    brew install tree

    Done! :)

  3. I get this error when I try to load the ~/.profile

    -bash: /Users/myname/.profile: line 4: syntax error near unexpected token `;’
    -bash: /Users/myname/.profile: line 4: `find $&;{1:-.} -print | sed -e ’s;[^/]*/;|____;g;s;____|; |;g’’

Leave a Reply

Your email address will not be published. Required fields are marked *