I'm wondering to know that what difference is the between using +
and ;
at the end of -exec
command when I use in find
command?
find .... -exec ... \;
VS
find .... -exec ... +
-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.
\;
, the executed command would becat 1; cat 2; cat 3
. With+
, the executed command would becat 1 2 3
. – Alaa Ali Dec 10 '14 at 13:46+
is not POSIX, so may not be available on non-Linux systems. Not a concern if you never leave the platform, but good to know if you ever have to use Solaris. :) – Simon Richter Dec 11 '14 at 02:57-exec <command> {} ';'
because its easier on my typing (I can touch type but I don't exactly have the best technique). – hanetzer Dec 11 '14 at 02:58-exec ... {} +
is POSIX. See: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html#tag_20_47_05 – cuonglm Dec 11 '14 at 05:00-exec ... {} +
extension was suggested for inclusion into POSIX specs in 2001 and ratified into POSIX issue 6 in 2004. It's possible the version of Solaris you're used to is older than that POSIX standard (or just targets an earlier version). – Oli Dec 11 '14 at 09:16