1

What is the meaning of \; in find command? What is the reason to use it?

In manual I can find example:

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

Why not: find . -type f -exec file '{}' ?

marked as duplicate by αғsнιη, Mikel, GAD3R, Romeo Ninov, ilkkachu Apr 22 '18 at 17:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

The find command has to have a way of telling where the command that should be executed through -exec ends. Without the ; at the end of the command, find would not know where the command ended and where (possibly) other find flags continued.

A nonsensical example that executes basename on all files and ls on all directories:

find . '(' -type f -exec basename {} ';' ')' -o '(' -type d -exec ls {} ';' ')'

Without the ; above, find would not know whether to execute basename /some/path or basename /some/path ')' -o '(' ... etc.

Quoting or escaping the ; is only done so that the shell will not treat it as a command delimiter.

A less nonsensical example that first copies .txt files to a particular directory and then renames the original files by appending .done to their name (if the copying went ok):

find . -type f -name '*.txt'
    -exec cp {} /some/path/dest ';' \
    -exec mv {} {}.done ';'

(Note, as Eliah Kagan points out in comments, that the construct {}.done is strictly speaking not portable, but implementation defined. An implementation may choose not to expand the {} characters to the current pathname, or it may do. In my experience, all find implementations do expand {} to the current pathname even when concatenated with another string like this.)

Without the ; in the previous example, find would not know whether we would want to execute the cp command on the found file together with /some/path/dest, a file called -exec, mv etc.

  • I'm not sure why this has been downvoted. It addresses the question of why ; can't simply be omitted very well. But it occurs to me that another purpose of ; is to tell find to run the command separately with each path, rather than pass multiple paths to the command as with +. (Also, arguments like {}.done aren't strictly portable; there, "it is implementation-defined whether find replaces those two characters or uses the string without change." I don't regard this as a problem but it may be worth mentioning.) – Eliah Kagan Apr 22 '18 at 16:25

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