忽略 Find 命令中的文件列表 Linux/ Unix

Ignore list of files in Find command Linux/ Unix

我想忽略来自 find 命令的文件列表:

find .  \( -name file1-o -name file2 \)

以上格式,我要单独给文件。我可以将它们放入数组中吗??

find 支持正则表达式。参见 How to use regex with find command?。 如果您的文件名有某种模式,这可以解决您的问题。

Posix 扩展正则表达式是一个不错的选择

find . -regextype posix-extended -regex '.*(scriptA|scriptB)[0-9]\.pl'

我会在 perl 中使用 File::Find:

#!/usr/bin/env perl
use strict;
use warnings;

use File::Find;

my @skip_names = qw( skip_this_file.txt
                     not_this_either
                   ); 
my %skip = map { $_ => 1 } @skip_names; 


sub finder {
    next if $skip{$_}; 
    ## do everything else. 
}

find ( \&finder, "/path/to/find_in" ); 

您可以从文件中读取文件名,或内联数组。或者也使用正则表达式测试。

要回答您的问题,不,您不能将要忽略的文件放入数组中并期望 find 知道它们。数组是您的 shell(bash 我假设)的产物,并且 find 工具是一个单独的二进制文件,在您的 shell.

之外

也就是说,您可以使用数组生成查找选项。

#!/usr/bin/env bash

a=(file1 file2 file3)

declare -a fopt=()
for f in "${a[@]}"; do
  if [ "${#fopt[@]}" -eq 0 ]; then
    fopt+=("-name '$f'")
  else
    fopt+=("-o -name '$f'")
  fi
done

echo "find . -not ( ${fopt[@]} )"

毫无疑问,有一种更优雅的方法来处理从第一个文件中排除 -o 发现 {Dennis,chepner,Etan,Jonathan,Glenn} 会指出,但我还没喝咖啡然而今天早上。