[Shell command] Read search patterns from a file with grep

Created on November 29, 2026

You can use grep -f to read search patterns from a file, one pattern per line. grep prints input lines that match any of the patterns.

Example

Create list.txt with these lines.

Hello
World
Good
Bye

Create patterns.txt with two patterns. The ^ anchor matches the start of a line.

^Hello
^World

Pass the pattern file with -f and specify list.txt as the file to search.

grep -f patterns.txt list.txt
Hello
World
Shell command grep