ohmygoddess
ohmygoddess

Reputation: 657

questions on Linux command "find -exec {}"

I tried to update a file in subversion. I need to add a line in makefile, in order to build up the revised version of code. Here is the command I tried to find the place in make file.

find . -name "*ake*" -exec grep filename {} /dev/null \;

It works. But my questions are: 1. What is the "\;" for? If I change it, there will be error message.

2 The /dev/null didn't change the results. I know this is the device where dispose all the "garbage information". But I still don't quite understand it in this situation.

Thanks in advance!

Upvotes: 2

Views: 628

Answers (1)

andypea
andypea

Reputation: 1401

The \; indicates the end of the command to be executed by find. The \ is required to stop the shell interpreting the ; itself. From man find:

-exec command ;
          Execute  command;  true  if  0 status is returned.  All following 
          arguments to find are taken to be arguments to the command until an 
          argument consisting of ‘;’ is encountered.

The /dev/null is a clever trick that took me a while to figure out. If grep is passed more than one filename it prints the containing filename before each match. /dev/null acts as an empty file containing no matches, but makes grep think it is always passed more then one filename. A much clearer alternative suggested by richard would be to use grep's -H option:

       -H, --with-filename
              Print the filename for each match. This is the default when there 
              is more than one file to search.

Upvotes: 4

Related Questions