【问题标题】:Terminal command 'find' read from txt files从 txt 文件中读取的终端命令“查找”
【发布时间】:2014-12-15 05:00:03
【问题描述】:

我想从一个目录中查找所有 .html 文件,但忽略一些文件。

$ find /Volumes/WORKSPACE/MyProject/ -name '*.html' ! -name 'MyFile.html' 

以上命令仅忽略 MyFile.html。但是我有一个要忽略的文件列表,并希望将忽略文件保留在单个 txt 文件中。

MyFile1.html    
MyFile1.html
MyFile2.html
MyFile2.html

以上是txt文件内容。

我希望将要忽略的文件列表保存在单个 txt 文件中,并在 find 命令中使用该 txt 文件。

请指教。提前致谢。

【问题讨论】:

    标签: text terminal find command


    【解决方案1】:

    您必须从文件中构建类似数组的东西,然后在find 语法中使用它。即

    #!/bin/bash
    while IFS= read -r file; do
      arr+=(-name "$file" -o)
    done < list_of_filenames_to_omit.txt
    
    #remove trailing -o
    unset arr[$((${#arr[@]}-1))]
    
    if ((${#arr[@]}!=0)); then
      find /Volumes/WORKSPACE/MyProject/ -name '*.html' ! \( "${arr[@]}" \)
    else
      #list_of_filenames_to_omit.txt is empty
      find /Volumes/WORKSPACE/MyProject/ -name '*.html' 
    fi
    

    【讨论】:

      猜你喜欢
      • 2021-10-29
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-29
      • 1970-01-01
      相关资源
      最近更新 更多