79

I have begun to combine different commands in the linux terminal. I am wondering why the backslash and semicolon are required for a command such as:

find ./ -name 'blabla' -exec cp {} ./test \;

when a simple cp command is simply:

cp randomfile ./test

without the \;

Are they to clearly indicate the end of a command, or is it simply required in the documentation? What is the underlying principle?

1
  • 14
    A ; on it's own would end the command. By using \;, a literal ; is being passed as a parameter to find.
    – Linuxios
    Commented Jan 3, 2014 at 21:44

2 Answers 2

77

The backslash before the semicolon is used, because ; is one of list operators (or &&, ||) for separating shell commands. In example:

command1; command2

The find utility is using ; or + to terminate the shell commands invoked by -exec.

So to avoid special shell characters from interpretation, they need to be escaped with a backslash to remove any special meaning for the next character read and for line continuation.

Therefore the following example syntax is allowed for find command:

find . -exec echo {} \;
find . -exec echo {} ';'
find . -exec echo {} ";"
find . -exec echo {} \+
find . -exec echo {} +

See also:

0
62

from "man find":

All following arguments to find are taken to be arguments to the command until an argument consisting of ';' is encountered.

find needs to know when the arguments of exec are terminated. It is natural to terminate a shell command with ; because also the shell uses this character. For the very same reason such a character must be escaped when inserted through the shell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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