【问题标题】:Open all files in a directory and subdirectories using bash terminal command?使用 bash 终端命令打开目录和子目录中的所有文件?
【发布时间】:2015-05-26 00:12:22
【问题描述】:

我有一个别名,我可以在其中使用 open file1.type file2.typeopen *.type

我想要的是能够在我当前位置的所有子目录中使用它。所以如果我在父目录,有两个子目录,运行命令会和运行open file1.type file2.type child1/file1.type child2/file1.type

所以像open -? *.type 这样的东西就是我要找的。​​p>

【问题讨论】:

    标签: bash terminal zsh


    【解决方案1】:

    find 适用于这种功能。像这样的:

    find . -type f -name \*.type -exec open {} \;
    

    或者在这种情况下,由于open是一个别名,你必须以命令的形式运行shell:

    find . -type f -name \*.type -exec bash -c open {} \;
    

    【讨论】:

    • 请注意:find -exec 大部分情况下都不起作用,如果 open 真的是 alias 就像 OP 提到的那样......
    【解决方案2】:

    如果运行 zsh 或 bash 4.x 并设置了 globstar 选项,** 将递归匹配所有目录。

    #!/bin/zsh
    open **/*.type
    

    ...

    #!/bin/bash
    shopt -s globstar
    open **/*.type
    

    【讨论】:

      猜你喜欢
      • 2021-12-18
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      • 2019-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多