0

I have a textfile with a list of words. I'm searching for an Unix solution like as follows:

For every word I want to search in all files of a directory if the word is in the file and give back the filename if the word can be found. The files are located in subdirectories of the directory.

Do I need a loop? Or is there a special command which can do that? I have found the "comm" command but it seems to work only for two files and not for the whole folder.

Patricia
  • 2,885
  • 2
  • 26
  • 32

2 Answers2

0

Something like:

find -type f -exec grep -H -f list_of_words.txt {} \;

must fulfills your needs.

The -f grep option makes grep check all word in list_of_words.txt for its search:

grep --help

-f, --file=FILE take PATTERNS from FILE

The type -f find option tells find to only check files.


A refinement would be, by example, to search only in *.txt files and also ignore your word list file list_of_words.txt :

find -type f \( -name \*.txt ! -name list_of_words.txt \) -exec grep -H -f list_of_words.txt {} \;
Picaud Vincent
  • 10,518
  • 5
  • 31
  • 70
0

If all the files to be searched are in the same folder and not within any sub folders within it, below will do

ls -p | grep -v / | xargs grep -lf <file_with_list_of_words>
omuthu
  • 5,948
  • 1
  • 27
  • 37