I tried now for a while to

  • read from a file test.txt (which has two lines "aaaa" and "bbbb")
  • take these lines to search the strings within a folder and its subfolders

My solution looks like this:

while read line 
do echo $line
    grep -irl $line /home 2>/dev/null
done < test.txt

If I replace $line with aaaa it finds the files where aaaa is written in, with $line it doesn't work...?

Maybe you have an idea?

  • 1
    Could it be that test.txt has Windows/DOS line endings? Does it work after you run dos2unix test.txt?
    – Freddy
    Commented Nov 6, 2019 at 9:04
  • Have you try to quote $line in grep? Commented Nov 6, 2019 at 9:13
  • I already tried the quotes and I wrote the file on UNIX. But I found now out that it could be a problem with the interpretation of /home after $line - because if I change to /home and locate the files in there anything works fine...?! So it seems a bit that if I use $line it does not interprete the path after as a path...
    – Jochen
    Commented Nov 6, 2019 at 9:29
  • 1
    I would still think $line contains something you don't expect, but maybe I'm wrong. You could do an echo "$line" | xxd and compare the output.
    – Freddy
    Commented Nov 6, 2019 at 10:10
  • 1
    This is what the -f option is for: grep -irl -f test.txt dir Commented Nov 6, 2019 at 14:48

1 Answer 1


The grep utility has a -f option whose argument is the name of a file. -f tells grep to read patterns from the specified file and then search for all of them, just as if those patterns had been individually specified on the command line. It combines with other options like -r.

-f is specified by POSIX.

GNU Grep allows the file to be specified as -, so the patterns are read from standard input; POSIX says that only file arguments may be specified as - for standard input; no such thing is required for pattern_file arguments.

If the lines of the search file are meant to be strings to look for in the input, as opposed to regular expressions, add the -F option (for fixed-string).

  • ... and if you want to match complete lines (not partial lines), also add -x.
    – Kusalananda
    Commented Aug 20, 2021 at 7:39
  • The edits and comment above are superfluous to my original remark that "[i]t combines with other options". The patterns coming from -f behave like patterns from the command line.
    – Kaz
    Commented Aug 20, 2021 at 12:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .