Results 1 to 4 of 4
exec, a option of find command, takes a odd format as follows
find ... -exec command {} \;
Is there any explaination about why it takes this kind of format, ...
- 02-12-2010 #1Just Joined!
- Join Date
- Jul 2008
- Posts
- 5
[SOLVED] why is find's syntax so strange?
exec, a option of find command, takes a odd format as follows
find ... -exec command {} \;
Is there any explaination about why it takes this kind of format, or
some obscure implication?
- 02-12-2010 #2
What part of it do you find weird?
Basically, the way that -exec works is that it executes the command you specify for each file. So
might call:Code:find . -iname '*.html' -exec echo '{}' \;
On the other hand, I might make a call like this:Code:echo index.html echo contact.html
which would call:Code:find . -iname '*html' -exec my_program -r 3 '{}' \;
I assume that the strange bits are the "{}" and the "\;".Code:my_program -r 3 index.html my_program -r 3 contact.html
The "{}" is a special token that tells find where to put the name of the file that it found. It chose this symbol because it is very unlikely that a program would use this token, so find can safely assume that you want for it to use the {}.
The "\;" is because find uses a semicolon to terminate the command that you want to run. Therefore, note the differences between the following:
The first would print the "-type f" (assuming it to be an argument to echo), while the second treats "-type f" as an argument to find.Code:find . -iname '*.html' -exec echo '{}' -type f \; find . -iname '*.html' -exec echo '{}' \; -type f
The reason for "\;" and not ";" is that Bash treats ";" as a special character. If you do not include the slash, Bash thinks that the ";" is meant for it (";" means to end the current command), and therefore find will not receive it, and will complain that you didn't use a semicolon:
Does this make sense?Code:alex@danu:~$ find . -iname '*.html' -exec echo '{}' ; find: missing argument to `-exec'DISTRO=Arch
Registered Linux User #388732
- 02-13-2010 #3Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,970
What Cabhan said. Also, it is how the tool was originally written, in the deep dark early Unix era. It's been around for over 30 years now...
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 02-13-2010 #4Just Joined!
- Join Date
- Jul 2008
- Posts
- 5
Your explaination is exactly what I need, thank.


