Search for a file with specific content

Posted on March 08, 2007 in Linux

Found a site which describes this quite good (german):

Lets do some comparison and search the whole root directory for the string "PAGER":

Lazy as i am i first started with the shortest possibility:

grep "PAGER" find / -type f -print

Does not find anything for me, zsh gives me an error "argument list too long"

Here some explanation why this is so:

getconf ARG_MAX shows us the limit of 131072 characters allowed passing the command-line at once.,

So lets try something else, this time using xargs, which breaks the command down below 131072 characters:

find / -type f -print | xargs grep "PAGER"

Little bit better now, but, when i come across files with spaces in the filename i get problems, so lets try it again:

find / -type f -print0 | xargs -0 grep "PAGER"

ok, this finally works. Somewhere someone shouted out that the parameters -print0 and -0 are not posix compliant, so for you purists out there another working solution:

find / -type f -exec grep "PAGER" /dev/null {} \;