简体   繁体   English

osx bash 上的树命令

[英]tree command on osx bash

I'm following a screen cast on a ruby gem called pry .我正在跟踪一个名为pry的红宝石宝石的屏幕投射 At 8:10, the .tree command is used, which I believe is a Unix command. 8:10 使用了 .tree 命令,我认为这是一个 Unix 命令。

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 引用了一个 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

from the context of bash I tried using the command tree with no luck:从 bash 的上下文中,我尝试使用命令树但没有运气:

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?这看起来非常有用,我怎样才能得到这个命令?

Using homebrew :使用自制软件

brew install tree

Using macports :使用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 ; <rant>所有系统都应该带有tree I use it a lot.我经常使用它。 And we can post directory structures as text, not pics.</rant>我们可以将目录结构作为文本发布,而不是图片。</rant>

For a simple approach you can also add the following alias to your ~/.bashrc or ~/.zshrc file:对于一个简单的方法,您还可以将以下别名添加到您的~/.bashrc~/.zshrc文件中:

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

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:在这里找到了这个解决方案:

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

Add this to your shell startup file( ~/.bashrc or ~/.zshrc ):将此添加到您的 shell 启动文件( ~/.bashrc~/.zshrc ):

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

After restarting your shell, you can use the tree command like so:重新启动 shell 后,您可以像这样使用 tree 命令:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM