What is the difference between using '+' (plus) and ';' (semicolon) in -exec command?
-exec ... \;
will run one item after another. So if you have three files, the exec line will run three times.
-exec ... {} +
is for commands that can take more than one file at a time (eg cat
, stat
, ls
). The files found by find
are chained together like an xargs
command. This means less forking out and for small operations, can mean a substantial speedup.
Here's a performance demo catting 10,000 empty files.
$ mkdir testdir
$ touch testdir/{0000..9999}
$ time find testdir/ -type f -exec cat {} \;
real 0m8.622s
user 0m0.452s
sys 0m8.288s
$ time find testdir/ -type f -exec cat {} +
real 0m0.052s
user 0m0.015s
sys 0m0.037s
Again this only works on commands that can take multiple filenames. You can work out if your command is like that by looking at its manpage. Here's the synopsis from man cat
:
SYNOPSIS
cat [OPTION]... [FILE]...
The ellipsis on [FILE]...
means it can take more than one file.
+
can only be used on single commands and you must have exactly one {}
in the line. \;
can operate with multiple zero-to-many groups.