1

I would like to ignore list of files from the find command :

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

In the above format, I have to give files individually. Can I put them into an array instead??

kaya3
  • 47,440
  • 4
  • 68
  • 97
waiting
  • 45
  • 6

4 Answers4

1

To answer your question, no, you can't put the files to ignore into an array and expect to have find know about them. An array is an artifact of your shell (bash I assume), and the find tool is a separate binary, outside your shell.

That said, you can use an array to generate options for find.

#!/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[@]} )"

There's no doubt a more elegant way to handle the exclusion of -o from the first file found that {Dennis,chepner,Etan,Jonathan,Glenn} will point out, but I haven't had coffee yet this morning.

ghoti
  • 45,319
  • 8
  • 65
  • 104
0

find supports regular expressions. See How to use regex with find command?. If your file names have some kind of pattern this could solve your problem.

Community
  • 1
  • 1
NaN
  • 7,441
  • 6
  • 32
  • 51
0

Posix extended regex are a good way to go

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

I'd use File::Find in perl:

#!/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" ); 

You can read your filenames out of a file, or inline the array. Or use regular expression tests as well.

Sobrique
  • 52,974
  • 7
  • 60
  • 101