List the files containing a particular word in their text

  • I would like to list the files recursively and uniquely that contain the given word.

    Example: Checking for word 'check', I normal do is a grep

    $ grep check * -R

    But as there are many occurrence of this word, I get a lot of output. So I just need to list the filenames that contain the given search word. I guess some trick with find and xargs would suffice here, but not sure.

    Any ideas?

  • N.N.

    N.N. Correct answer

    8 years ago

    Use the -l or --files-with-matches option which is documented as follows:

    Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. (-l is specified by POSIX.)

    So, for you example you can use the following:

    $ grep check * -lR

    Works on MSYS too. Great!

    I used `grep -rl "text to find" "/usr/share"` and it worked great!

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM