ruby osx bash上的树命令

au9on6nz  于 2023-04-11  发布在  Ruby
关注(0)|答案(5)|浏览(126)

我在一个名为pry的ruby gem上跟踪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

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

xqkwcwgp

xqkwcwgp1#

使用homebrew

brew install tree

使用macports

sudo port install tree

使用the source

Follow these directions.(注意事项;你应该使用有意义的标志/等等。)
所有系统都应配备tree;我经常使用它。而且我们可以把目录结构作为文本发布,而不是图片。

cyvaqqii

cyvaqqii2#

对于一个简单的方法,您也可以将以下tree别名添加到~/.bashrc~/.zshrc文件中:

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

编辑完文件后,重新启动终端时将加载该命令,或者您可以通过使用source ~/.bashrcsource ~/.zshrc获取配置文件来在现有终端中加载该命令。
结果如下:

$ 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

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

xqkwcwgp

xqkwcwgp3#

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

ljo96ir5

ljo96ir54#

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

find .

也可以只指定列表目录

find -type d

或者如果您只想查看文件

find -type f

也可以指定深度

find -type d -maxdepth 2
tvokkenx

tvokkenx5#

将以下内容添加到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

相关问题