Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

linux - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...