50

I'm following a screen cast on a ruby gem called pry. At 8:10, the .tree command is used, which I believe is a Unix command.

It does not appear to be working on my system:

[24] pry(main)> .tree
\Error: there was a problem executing system command: tree

and I have traced the issue to here, in which pry references a shell command:

Pry::CommandSet.new do

  command(/\.(.*)/, "All text following a '.' is forwarded to the shell.", :listing => ".<shell command>") do |cmd|
    if cmd =~ /^cd\s+(.+)/i
      dest = $1
      begin
        Dir.chdir File.expand_path(dest)
      rescue Errno::ENOENT
        output.puts "No such directory: #{dest}"
      end

    else
      if !system(cmd)
        output.puts "Error: there was a problem executing system command: #{cmd}"
      end
    end
  end

from the context of bash I tried using the command tree with no luck:

projects/sms(apps2)$ tree
-bash: tree: command not found
~/projects/sms(apps2)$ .tree
-bash: .tree: command not found

This looks incredibly useful, how can I get this command?

JZ.
  • 21,147
  • 32
  • 115
  • 192

5 Answers5

106

Using homebrew:

brew install tree

Using macports:

sudo port install tree

Using the source:

Follow these directions. (Caveat; you should use the flags/etc. that make sense.)

<rant>All systems should come with tree; I use it a lot. And we can post directory structures as text, not pics.</rant>

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • it does not work for me, what' wrong with that? Error: The following directories are not writable by your user: /usr/local/share/man/man5 /usr/local/share/man/man7 You should change the ownership of these directories to your user. sudo chown -R $(whoami) /usr/local/share/man/man5 /usr/local/share/man/man7 And make sure that your user has write permission. chmod u+w /usr/local/share/man/man5 /usr/local/share/man/man7 – xgqfrms Sep 14 '19 at 01:51
  • $ sudo chown -R $(whoami) /usr/local/share/man/man5 /usr/local/share/man/man7 $ chmod u+w /usr/local/share/man/man5 /usr/local/share/man/man7 $ brew install tree it's OK now, thx! – xgqfrms Sep 14 '19 at 02:00
8

For a simple approach you can also add the following tree alias to your ~/.bashrc or ~/.zshrc file:

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

After editing the file, the command will be loaded when you restart your terminal, or you can load the command in your existing terminal by sourcing the config file using source ~/.bashrc or source ~/.zshrc.

This results in the following:

$ tree
.
|____.git
| |____config
| |____objects
| | |____pack
| | |____info
| |____HEAD
| |____info
| | |____exclude
| |____description
| |____hooks
| | |____commit-msg.sample
| | |____pre-rebase.sample
| | |____pre-commit.sample
| | |____applypatch-msg.sample
| | |____pre-receive.sample
| | |____prepare-commit-msg.sample
| | |____post-update.sample
| | |____pre-applypatch.sample
| | |____pre-push.sample
| | |____update.sample
| |____refs
| | |____heads
| | |____tags

Found this solution here:

Grokify
  • 15,092
  • 6
  • 60
  • 81
  • 1
    Probably worth mentioning the `source ~/.bashrc` or `source ~/.zshrc` file before running tree. might help those who don't know or want to restart the terminal. – Val Apr 10 '23 at 19:45
2

Use brew install tree command on the terminal if you're using Homebrew on your Mac.

Kalhara Tennakoon
  • 1,302
  • 14
  • 20
1

Not exactly the same, but gives you a list of all directories and files in those directories using:

find .

You can also specify list directories only

find -type d

or if you want to see files only

find -type f

you can also specify depth

find -type d -maxdepth 2
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
1

Add this to your shell startup file(~/.bashrc or ~/.zshrc):

tree() {
    find $1 -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
}

After restarting your shell, you can use the tree command like so:

% tree Downloads
Downloads
|____ideaIU-2020.1.3-no-jbr.tar.gz
|____Firicico.ttf
Shail
  • 881
  • 9
  • 20
  • 37