Find the answer to your Linux question:
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, ...
  1. #1
    Just Joined!
    Join Date
    Jul 2008
    Posts
    5

    Post [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?

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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
    Code:
    find . -iname '*.html' -exec echo '{}' \;
    might call:
    Code:
    echo index.html
    echo contact.html
    On the other hand, I might make a call like this:
    Code:
    find . -iname '*html' -exec my_program -r 3 '{}' \;
    which would call:
    Code:
    my_program -r 3 index.html
    my_program -r 3 contact.html
    I assume that the strange bits are the "{}" and the "\;".

    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:
    Code:
    find . -iname '*.html' -exec echo '{}' -type f \;
    find . -iname '*.html' -exec echo '{}' \; -type f
    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.

    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:
    Code:
    alex@danu:~$ find . -iname '*.html' -exec echo '{}' ;
    find: missing argument to `-exec'
    Does this make sense?
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Linux Guru Rubberman's Avatar
    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!

  4. #4
    Just Joined!
    Join Date
    Jul 2008
    Posts
    5
    Your explaination is exactly what I need, thank.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...