Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

grep

find

ack

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.

like image 732
chim Avatar asked Oct 23 '25 04:10

chim


1 Answers

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
like image 113
Andy Lester Avatar answered Oct 27 '25 04:10

Andy Lester



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!