osx bash上的树命令

时间:2011-11-29 00:09:22

标签: ruby bash unix tree pry

我在名为screen cast的红宝石宝石上跟踪pry。在8:10,使用.tree命令,我相信这是一个Unix命令。

它似乎不适用于我的系统:

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

我已将问题追溯到here,其中pry引用了一个shell命令:

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

从bash的上下文中我尝试使用命令树而没有运气:

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

这看起来非常有用,我怎么能得到这个命令?

5 个答案:

答案 0 :(得分:58)

使用homebrew

brew install tree

使用macports

sudo port install tree

使用the source

Follow these directions.(警告;你应该使用有意义的标志/等。)

&lt; rant&gt;所有系统都应附带tree;我经常使用它。我们可以将目录结构发布为文本,而不是图片。&lt; / rant&gt;

答案 1 :(得分:0)

对于简单方法,您还可以在~/.bashrc~/.zshrc文件中添加以下别名:

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

这导致以下结果:

$ 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

在此处找到此解决方案:

答案 2 :(得分:0)

不完全相同,但是使用以下命令提供了所有目录和这些目录中文件的列表:

find .

您还可以仅指定列表目录

find -type d

或者如果您只想查看文件

find -type f

您还可以指定深度

find -type d -maxdepth 2

答案 3 :(得分:0)

将此添加到您的Shell启动文件(~/.bashrc~/.zshrc):

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

重新启动外壳程序后,可以使用树命令,如下所示:

% tree Downloads
Downloads
|____ideaIU-2020.1.3-no-jbr.tar.gz
|____Firicico.ttf

答案 4 :(得分:0)

如果您在Mac上使用Homebrew,请在终端上使用brew install tree命令。

相关问题