[Techtalk] real life kewl text-processing and regexps samples wanted

R. Daneel Olivaw linuxchix at r-daneel.com
Sat Jan 10 11:35:55 EST 2004


Hi there,

> I like this recursive grep that I found somewhere:
> 
> find  . -type f -exec grep "thing-to-find" {} \;
> 
> note that there is no space between the two braces
> and no space between the backslash and the semicolon,
> but there is a space between the second brace and the backslash.
> 
> Can someone explain the spaces and why this works?
> 
>  I'd like to know

Well, first of all, grep -l may be useful, as it prints the filename
out at every match.
The braces are there to replace the file found by "find" (why does this
sentence sound odd ?).
The rest is easy to see :
after the exec option, eveything is what will be executed on the command
line. In your example : grep "thing-to-find" {}
the semi-colon is there to tell when the command line ends. However, as
you run the find command, the semi-colon would be interpreted by your
current shell as "end of command line", just like in 'echo "hello";'.
(you may concatenate commands that way : 'echo "hello"; echo "again"')
Now, we want to transmit the semi-colon to the grep command, so we need
to backslash it so it will not be interpreted by your current shell.
The real command line will be : grep "thing-to-find" {} ;
Therefore you have no space between backslash & semi-colon (backslash
'hides' or 'escapes' the very next caracter)  all other "arguments"
after '-exec' need to be separated by space, as it will run just like a
stand-alone command.

I hope my explanation is not too fuzzy (it's just 9h30 am here, and I
just got up ...). If it is, don't ésitate to write again ;)

bye,

R. Daneel Olivaw,
The Robot Inside.


More information about the Techtalk mailing list