In the Linux system or Mac os, you can use the grep command to find files containing keywords in a directory.

Let’s see how to use the grep command.

  • grep -i pattern files: Search case-insensitively. It is case sensitive by default,
  • grep -l pattern files: list only matching file names,
  • grep -L pattern files: list mismatched file names,
  • grep -w pattern files: matches only whole words, not part of a string (such as ‘magic’, not ‘magical’),
  • grep -C number pattern files: the matching context displays [number] lines,
  • grep pattern1 | pattern2 files: display lines matching pattern1 or pattern2,
  • grep pattern1 files | grep pattern2: Display lines that match both pattern1 and pattern2.

How to find files containing keywords in a directory?

For example, you want to find which files contain the keyword ‘w3cgeek’ in the /home/user directory.

You can use the following grep command to find these files on Linux or Mac os.

grep -R -w -l 'w3cgeek' /home/user/

If there any files contain the keyword, then the filename will be shown in the result.

So, it’s very easy to find files containing keywords in a directory on Linux or Mac OS.