2

This is a relatively simple command, so if a duplicate exists and someone could refer me, I'm sorry and I'll delete/close this question.

Man page for find

   find . -type f -exec file '{}' \;

   Runs 'file' on every file in or below the current directory.  Notice that the braces are enclosed in single quote marks to protect them from interpretation
   as shell script punctuation.   The semicolon is similarly protected by the use of a backslash, though ';' could have been used in that case also.

I do not understand the notation \;. What in the world is that?

imagineerThat
  • 5,293
  • 7
  • 42
  • 78

1 Answers1

5

In the find command, the action -exec is followed by a command and that command's arguments. Because there can be any number of arguments, find needs some way of knowing when it ends. The semicolon is what tells find that it has reached the end of the command's arguments.

Left to their own devices, most shells would eat the semicolon. We want that semicolon to be passed to the find command. Therefore, we escape it with a backslash. This tells the shell to treat the semicolon as just one of the arguments to the find command.

MORE: Why not, one may ask, just assume that the exec command's argument just go to the end of the line? Why do we need to signal an end to the exec command's arguments at all? The reason is that find has advanced features. Just for example, consider:

find . -name '*.pdf' -exec echo Yes, we have a pdf: {} \; -o -exec echo No, not a pdf: {}  \;
John1024
  • 109,961
  • 14
  • 137
  • 171
  • Why does `find` need to know when it ends? The arguments to the -exec command is a finite list, so shouldn't it know when to end? – imagineerThat Jan 16 '14 at 07:25