Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
find . -name "*.html" -exec grep -l somethingtobefound {} \;

I was just wondering what the keywords "-name" "-exec" "-l" "{}" "\" and ";" were supposed to signify.

Also, I commonly see double-dashes "--" being used instead of single-dashes "-" in a lot of cases. I was wondering if they were interchangeable, and what they signify as well. Thank you!

share|improve this question

migrated from programmers.stackexchange.com Jan 5 '12 at 15:11

This question came from our site for professional programmers interested in conceptual questions about software development.

6  
Have you considered reading find man page? –  enzotib Jan 5 '12 at 15:15
4  
This is a fundamental usage question, I would suggest buying a basic unix and shell book for introductory guidance. You can learn about the find command using man find. No, the single dash and double dashes are not interchangable. The single dashes signify the command's arguments. Double dashes were introduced with long arguments in the attempt to make simple args more readable eg: -d might become --device. –  bdowning Jan 5 '12 at 15:20

4 Answers 4

The answers can be found with man find:

`
-name pattern
          Base of  file  name  (the  path  with  the  leading  directories
          removed)  matches  shell  pattern  pattern.   The metacharacters
          ('*', '?', and '[]') match a '.' at the start of the  base  name
          (this is a change in findutils-4.2.2; see section STANDARDS CON‐
          FORMANCE below).  To ignore a directory and the files under  it,
          use  -prune; see an example in the description of -path.  Braces
          are not recognised as being special, despite the fact that  some
          shells  including  Bash  imbue  braces with a special meaning in
          shell patterns.  The filename matching is performed with the use
          of  the  fnmatch(3)  library function.   Don't forget to enclose
          the pattern in quotes in order to protect it from  expansion  by
          the shell.

-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 string '{}'
          is replaced by the current file name being processed  everywhere
          it occurs in the arguments to the command, not just in arguments
          where it is alone, as in some versions of find.  Both  of  these
          constructions might need to be escaped (with a '\') or quoted to
          protect them from expansion by the shell.  See the EXAMPLES sec‐
          tion for examples of the use of the -exec option.  The specified
          command is run once for each matched file.  The command is  exe‐
          cuted  in  the starting directory.   There are unavoidable secu‐
          rity problems surrounding use of the -exec  action;  you  should
          use the -execdir option instead.

And so on

share|improve this answer
    
See no reason to paste a plane manual pages. It was already mentioned here that manuals have the answers for the question. –  Eugene S May 1 '12 at 13:09

In this context, the -name is telling the find command to filter results to those that end with '.html' and the -exec is telling the find command to run grep with the results.

The -l applies to the grep command and indicates that the file name of the first input file to match the pattern (i.e. somethingtobefound) should be printed and scanning halted. As per the man page, the {} makes sure the path name of the current file is passed in by find and the \; terminates the command.

In regards to double-dashes versus single-dashes, this is something that tends to be program specific and shouldn't have any special meaning. That said, generally you tend to see double-dashes before longer parameters (i.e. --show-results) versus single character parameters (i.e. -s).

share|improve this answer
4  
{} \; is the tail-end of the syntax for find's -exec option... The {} represents one of the names found by find, and the \; represents the end of that option (similar to the ; as used in ending a bash command)... find requires the ` \` to escape the ; so that the shell doesn't treat it as a shell end-of-command. -exec also allows for a \+ which means many of the names found by find (but for grep the \; is the appropriate action)... It is all explained in the find's man page (man is an abbreviation of manual).. –  Peter.O Jan 5 '12 at 16:02
1  
@perer Ah, cool, I've updated my answer accordingly. Usually I just pipe stuff together so I don't use the exec too often. –  rob Jan 5 '12 at 16:04
    
Hey, so shouldn't -name and -exec actually be --name and --exec instead since they are long options/arguments instead of single character options/arguments? +1 –  Dark Templar Jan 6 '12 at 0:28
    
@DarkTemplar - In theory yes, but depending upon when a program was written it may or may not follow the convention. –  rob Jan 6 '12 at 1:09
1  
@DarkTemplar - Right, as far as I know it's nothing formal. –  rob Jan 8 '12 at 5:15

The parameters -name and -exec signifies "file or directory name pattern to search for" and "execute something on each of the matched resulting files or directories". The {} is substituted the results matching the -name pattern, so that the -exec statement can use these as arguments to commands executed. The backward slash \; signifies the end of the find command. The double-dashes -- typically means long options e.g. ls --all and the single dash - means short option e.g. ls -a. They both mean exactly the same.

share|improve this answer

This isn't a bash question per se -- everything in that command is a set of arguments to the unix find(1) command, which would behave the same regardless of what shell you invoke it from.

Given this, what you really need to do is look at the documentation for find(1) -- and you can do that by running:

$ man find

or, if your version of find is the Gnu one (as it will be if you are running Linux),

$ info find

for a more book-like document.

For your second question: many commands (particularly those which are part of the Gnu project) use long option flags of the form

$ command --long-argument --other-long-argument

as an alternative to short arguments of the form

$ command -lo

or

$ command -l -o

. Commands which do so will use '--' instead of '-' at the start of such flags, to make clear which type of option flag is coming.

share|improve this answer
    
And some commands, including find, use single dashes with long option names (command -foo -bar). Such commands typically don't permit bundling (making -fb equivalent to -f -b). Perl scripts using the GetOpt::Long module tend to behave this way, for example. –  Keith Thompson Jan 5 '12 at 20:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.