2

Say I have a list of keywords:

$ cat filenames.txt 
satin
perdido
savoy
stockings
april
lullabye

I want to find all the files in some directory that contains those keywords.

I have tried something like:

find some_dir/ -type f "$(printf '-or -iname *%s* ' $(cat filenames.txt))"

But somehow I am not able to use printf to build the whole find command; I get:

-bash: printf: -o: invalid option
fricadelle
  • 371
  • 2
  • 14

1 Answers1

1

The find command does not search the files' content.

The -iname option selects the filenames.

The error means that printf does not reconize the -o option. In such situation you should use printf -- '-o...

I think the final filter you are looking for is:

find some_dir/ -type f \( -iname \*satin\* -o -iname \*perdido\* ... \)

Note the \( ... \) to enclose all the -iname ...

-o ... is said to be POSIX in the man find

You need to be sure that the filenames.txt file contains at least one value.

If you need to find the filenames in the some_dir/, then give a try to this:

set -f ; find some_dir/ -type f \( $(printf -- ' -o -iname *%s*' $(cat filenames.txt)| cut -b4-) \) ; set +f