Murphy Mac - Screencasts and Tutorials » Page 'Tree Command for Mac'

Tree Command for Mac


tree command for MacThis post is a follow up to Murphy’s post on retrieving a remote file by email. The method described in that post requires you to know the path of the file you’re retrieving. You can use the information in this post to auto-generate a text file with the paths to all your files. Retrieve that file and you’ll have all the information you might need.

For some people the best thing about this post will be this:

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

Enter that line in Terminal, hit return on your keyboard, and you’re pretty close to the tree command you loved so much in Windows. (Thanks to the forums for help with the find and sed commands. ) You can copy and paste the command, or download a shell script containing it here. You’ll need to make it executable.

Murphy posted last week about how to retrieve a file by email. The catch was that you needed to know the path of the file. This screencast will show you how to generate a text file listing all your files in a tree format.  If you prefer, you can use this command:

ls -R

That will also display files and folders, but the format is more conventional. Either command will accomplish our goal.

In the screencast Murphy has the find and sed command listed above inside a shell script. The output is redirected to a text file. You can retrieve that text file by email, just like you would any other file.

If you prefer, create a second Applescript like the one in this post. You could alter it to always send a hard-coded file: the file that lists all your files.

In the screencast, Murphy uses Lingon to configure launchd, which handles scheduling tasks you may have used cron for in the past.

For more information on creating a shell script and making the script executable visit this post. If you want to know more about sed (and awk) there’s a whole book to read.

Watch Now | Permalink

35 comments to “Tree Command for Mac”

  1. I get a syntax error with the command and shell script.

    -bash: syntax error near unexpected token `|’

    I’m running 10.5.2

  2. tv –

    Try now, I think WordPress did something to the text. You should be able to copy and paste it now.

  3. How would i get ls -R to output to a text file?

    Also is there anyway the script could be altered for files too large for emails and have them uploaded via for instance quickshareit and the download link then sent?


  4. dave –

    add the following to the end of a command to redirect the output to a text file


    As for your other questions, yes, something like that could be done. I’m not familiar with quickshareit but there are plenty of alternative methods that could be scripted.

  5. Hi again

    Ive been using it and it works great but i wanna change a little bit

    i know this command creates a textfile with the full path of all the items in the current direcotyr. But how do i change it so it goes into all the subfolders

    ls -l | sed ‘1d; s/\([^ ]* *\)\{7\}/'”${PWD//\//\\/}”‘\//; s/^\/\//\//; s/ -> .*//’ > text.txt

  6. Heads up for like minded nerds: bash needs double quotes in order to make this an alias. This works for me on OSX using bash.
    alias tree=’find . -print | sed -e “s;[^/]*/;|____;g;s;____|; |;g”‘

  7. fantastic–just what i was looking for! i never took the time to learn sed or awk…someday, maybe.

    thanks for the post!

  8. Add this to you ~/.profile

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


    $ source ~/.profile
    $ tree
    $ tree .
    $ tree [some-folder-path]

  9. hm.. really like it..

  10. If you just replace the ‘find . ‘ with ‘find $1 ‘ you can pass the script an argument, eg:

    tree ../

  11. Can you manipulate the regular expression to skip files and directories that start with “.”, i.e. the hidden ones?

  12. Hey. Or you can just build the command from source:

  13. You rock!

  14. I agree with Gunnar. The program he linked has a slightly easier to understand output. I wrote a post on how to install it

  15. Great!

  16. I was using Windows XP from last couple of years but now I got a Mac O S X
    so I just want to ask that what is the command for cd\

    on mac thanks I’ll wait for your reply.

  17. Thankyou, I copied and pasted:

    “william-molesworths-imac:M william$ find . -print | sed -e ‘s;[^/]*/;|____;g;s;____|; |;g’ > filename.txt”

    It worked very nicely!

  18. Why don´t install macport?

  19. sudo port install tree

  20. sudo brew install tree

  21. ty

  22. Better yet, get the debian tree package but running:

    fink install tree

    (Of course, you need to have fink installed first. I love fink. I have access to debian stuff.)

  23. Thank you, Ezra!

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

    Your tree cmd written in sed worked very nicely, and with a path argument, on Mac OS X 10.6 and on my VPS shared host, running centos 4.

    Wonderfully helpful.

  24. Hi Murphy,

    Is it somehow possible to output the dates of the files as well?

  25. Gorgeous hints :)))

    no clue … is it possible to add data of last access to each filename?

    Thx. in advance, cheers

  26. Tree on the Mac doesn’t seem to be respecting the LSCOLORS env variable. When I use ls on a dir, I get dircolors as defined in my ~/.bashrc, but tree seems to use some other mysterious color scheme.

  27. Great information. Lucky mme I recently found your
    blog bby chance (stumbleupon). I have saved as a favorite for later!

  28. Hi there i am kavin, its my first time to commenting anyplace, when i read this piece of writing i
    thought i could also make comment due to this sensible article.

  29. Hello, I am a fresh developer only starting and I have to
    have a portfolio. Are you wanting a blog design totally free?

  30. Hello There. I found your blog using msn. This is a very well written
    article. I’ll mwke sure to bookmark it and return to read more of your useful information.
    Thanks for the post. I’ll certainly return.

  31. Simply wis? to s?y you? article is ?s astounding.

    The clarity ffor ?our post is simply nice annd ? ??n assume ?ou’?e a
    professional oon t??s subject. ?ine with ??ur permisssion allow me to clutch ?our feed to stay
    up to dat? wirh drawing close post. ?hanks 1,000,000 ?nd please keep up the rewarding wor?.

  32. It is not my first time to visit this site, i am browsing this web
    site dailly aand take fastidious facts from here everyday.

  33. That is very attention-grabbing, You’re a very skilled blogger.
    I have joined your feed and sit up for seeking more of
    your great post. Also, I have shared your web site
    in my social networks

  34. I am regular visitor, how are you everybody? This paragraph posted at this
    website is genuinely pleasant.

  35. I see, that needs unique & fresh articles.
    I know it is hard to write content manually everyday, but there is solution for
    this. Just search in g00gle for; Atonemen’s tips

Leave a comment