Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using find command to search for all files having some text pattern

Tags:

find

command

I use following find command to find and show all files having the input text pattern.

find . -type f -print|xargs grep -n "pattern"

I have many project folders each of which has its own makefile named as 'Makefile'.(no file extension, just 'Makefile')

How do i use above command to search for a certain pattern only in the files named Makefile which are present in all my project folders?

-AD.

like image 663
goldenmean Avatar asked Apr 07 '09 18:04

goldenmean


2 Answers

-print is not required (at least by GNU find implementation). -name argument allows to specify filename pattern. Hence the command would be:

find . -name Makefile | xargs grep pattern

like image 66
Eugene Morozov Avatar answered Sep 28 '22 01:09

Eugene Morozov


If you have spaces or odd characters in your directory paths youll need to use the null-terminated method:

 find . -name Makefile -print0 | xargs -0 grep pattern
like image 30
Tom Ritter Avatar answered Sep 28 '22 01:09

Tom Ritter