【问题标题】:tree command on osx bashosx bash上的树命令
【发布时间】:2011-11-29 00:09:22
【问题描述】:

我在名为pry 的红宝石宝石上关注screen cast。在 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

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

【问题讨论】:

    标签: ruby bash unix tree pry


    【解决方案1】:

    使用homebrew

    brew install tree

    使用macports

    sudo port install tree

    使用the source

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

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

    【讨论】:

    • 它对我不起作用,这有什么问题?错误:您的用户不可写以下目录: /usr/local/share/man/man5 /usr/local/share/man/man7 您应该将这些目录的所有权更改为您的用户。 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
    • $ 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 tr​​ee 现在可以了,谢谢!
    【解决方案2】:

    对于一个简单的方法,您还可以将以下别名添加到您的 ~/.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
    

    在这里找到了这个解决方案:

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        find .
        

        您也可以只指定列表目录

        find -type d
        

        或者如果您只想查看文件

        find -type f
        

        你也可以指定深度

        find -type d -maxdepth 2
        

        【讨论】:

          【解决方案5】:

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

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

          重启你的shell后,你可以像这样使用tree命令:

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

          【讨论】:

            猜你喜欢
            • 2020-04-13
            • 1970-01-01
            • 2013-12-25
            • 1970-01-01
            • 1970-01-01
            • 2014-09-21
            • 1970-01-01
            • 2017-04-14
            • 1970-01-01
            相关资源
            最近更新 更多