Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are the backslash and semicolon required with the find command's -exec option?

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?

like image 726
wesk Avatar asked Jan 03 '14 21:01

wesk


People also ask

What means the backslash in command line?

A backslash escapes the next character from being interpreted by the shell. If the next character after the backslash is a newline character, then that newline will not be interpreted as the end of the command by the shell. Instead, it effectively allows a command to span multiple lines.

What does command semicolon do?

Command-Semicolon (;): Find misspelled words in the document.

What does backslash mean in Ubuntu?

In Unix systems, and even some programming languages like C, the role of the backslash is to indicate to the system that the next character has a special meaning. Therefore, it works as an escape character. For example, a lowercase n, when used with a backslash, \n, indicates a new line character.

What is backslash in shell script?

The backslash (\) character is used to mark these special characters so that they are not interpreted by the shell, but passed on to the command being run (for example, echo ). So to output the string: (Assuming that the value of $X is 5): A quote is ", backslash is \, backtick is `.


1 Answers

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:

  • Using semicolon (;) vs plus (+) with exec in find
  • Simple unix command, what is the {} and \; for
like image 78
kenorb Avatar answered Sep 17 '22 19:09

kenorb