20

With this set of commands, what are the {} and \; characters for?

find . -name '*.clj' -exec grep -r resources {} \; 
1
  • 2
    Note that "grep -r" is not needed unless those .clj files are folders. find is by default recursive so grep doesn't have to be. (grep will be fed each file.) You might also want to use xargs instead since with your current command you won't get to know which files you get the hits from. Commented Jan 15, 2009 at 15:12

5 Answers 5

34

See man find. (particular the part about -exec)

When using -exec to run a command on each of the files found, the {} is replaced with the name of each file found, and the command is terminated by \;

In your example, all files found under the current directory (.), matching the name *.clj will have the command grep -r resources run on them (to find the string resources if it exists in each of those files).

It's actually somewhat redundant, since -r is for recursively searching subdirectories, and that's what find is already doing.

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't think to look in man find because it doesn't look like it's part of the find command, any more than a shell expansion, pipe, or other redirect operator is part of the command it's found next to. I assumed it was some shell syntax magic which I wouldn't be able to identify without already knowing the name of it.
4

In find, the -exec parameter grabs the rest of the parameters up til the ; (semicolon) which has to be escaped, hence the \;. Within this span, {} is replaced with the filename being inspected.

Comments

2

Consider this alternative command which I find easier to understand:

find . -name *.clj | xargs grep -r resources 

1 Comment

But which has the major flaw of misproperly handling files with embedded spaces and similar.
1

The character string "{}" will be replaced by the current file being processed. The escaped semi-colon terminates the command argument for the -exec option.

Comments

1

The string {} in find is replaced by the pathname of the current file.

The semicolon is used for terminating the shell command invoked by find utility.

It needs to be escaped, or quoted, so it won't be interpreted by the shell, because ; is one of the special characters used by shell (list operators).

See also: Why are the backslash and semicolon required with the find command's -exec option?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.