Search code examples
grepfindack

How can I use ack (preferably ack) or grep to search for a string in a list of files?


I want to search for a string "my-search-string" in any files called search-this-one.html

In all subdirectories of my current working directory.

I want a list of full paths of the files that contain this string. There are hundreds of files called search-this-one.html, any number of them could contain the string.

Has anyone had any success doing this.

I can get a list of files like this..

find . -type f -name "search-this-one.html"

I've tried a variety of grep and ack switches on this, all the other related answers search only for filenames that match the search string.


Solution

  • If you're running ack 2.x, you can use the -x option to get input from STDIN, much like xargs.

    ack -g search-this-one.html | ack -x pattern
    

    The ack -g says "Find any text files with names that match search-this-one.html", and then that's piped into ack -x which takes its list of input files from STDIN.

    Also works for searching for filenames with regex:

    ack -g '\.*.conf$' | ack -x searchString