Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix find command, what are the {} and \; for?

Tags:

find

shell

unix

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

find . -name '*.clj' -exec grep -r resources {} \;
like image 476
Berlin Brown Avatar asked Jan 15 '09 14:01

Berlin Brown


People also ask

What does {} mean in find command?

{} means "the output of find ". As in, "whatever find found". find returns the path of the file you're looking for, right? So {} replaces it; it's a placeholder for each file that the find command locates (taken from here).

What is the command for find in Unix?

$ find. Search for a file by the name abc. txt below the current directory, and prompt the user to delete each match. Note that the “{}” string is substituted by the actual file name while running and that the “\;” string is used to terminate the command to be executed.

Which search options can be used with the find command?

The find Command It supports searching by file, folder, name, creation date, modification date, owner and permissions.


3 Answers

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.

like image 164
Adam Bellaire Avatar answered Oct 04 '22 23:10

Adam Bellaire


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.

like image 27
falstro Avatar answered Oct 04 '22 22:10

falstro


Consider this alternative command which I find easier to understand:

find . -name *.clj | xargs grep -r resources 
like image 2
paxos1977 Avatar answered Oct 04 '22 22:10

paxos1977