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??
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.
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.
Posix extended regex are a good way to go
find . -regextype posix-extended -regex '.*(scriptA|scriptB)[0-9]\.pl'
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.