223

Why is there a difference in output between using

find . -exec ls '{}' \+

and

find . -exec ls '{}' \;

I got:

$ find . -exec ls  \{\} \+
./file1  ./file2

.:
file1  file2  testdir1

./testdir1:
testdir2

./testdir1/testdir2:


$ find . -exec ls  \{\} \;
file1  file2  testdir1
testdir2
./file2
./file1
Braiam
  • 1
  • 11
  • 47
  • 78
Ankur Agarwal
  • 23,692
  • 41
  • 137
  • 208
  • 3
    There is no need to escape the plus (`+`) with a backslash (\\). The semicolon (`;`) needs to be escaped with a backslash (\\) because otherwise the shell will read `;` as the end of the `find` command rather than as the end of the `-exec` attribute, as explained in [this answer](https://stackoverflow.com/a/48511301). – Henke Feb 09 '22 at 12:08

6 Answers6

346

This might be best illustrated with an example. Let's say that find turns up these files:

file1
file2
file3

Using -exec with a semicolon (find . -exec ls '{}' \;), will execute

ls file1
ls file2
ls file3

But if you use a plus sign instead (find . -exec ls '{}' \+), as many filenames as possible are passed as arguments to a single command:

ls file1 file2 file3

The number of filenames is only limited by the system's maximum command line length. If the command exceeds this length, the command will be called multiple times.

Matthew Simoneau
  • 6,199
  • 6
  • 35
  • 46
Martin
  • 37,119
  • 15
  • 73
  • 82
  • 3
    thanks. this is very useful for wanting to sort the resulting files: find -maxdepth 1 -type f -mtime -1 -exec ls -ltr {} \+ – fbas Sep 21 '16 at 15:23
  • 3
    Dumb q: I notice that `+` associated with `-exec` is always escaped, but `+` associated with `-mtime` is not. Do you know the reason? I guess it is habit from escaping `;` associated with `-exec`. – kevinarpe May 10 '18 at 01:55
  • 5
    @kevinarpe indeed, I would chalk it up to habit from `;`. Can't imagine it ever being necessary to escape `+` – Martin Aug 06 '18 at 22:20
54

All of the answers so far are correct. I offer this as a clearer (to me) demonstration of the behaviour that is described using echo rather than ls:

With a semicolon, the command echo is called once per file (or other filesystem object) found:

$ find . -name 'test*' -exec echo {} \;
./test.c
./test.cpp
./test.new
./test.php
./test.py
./test.sh

With a plus, the command echo is called once only. Every file found is passed in as an argument.

$ find . -name 'test*' -exec echo {} \+
./test.c ./test.cpp ./test.new ./test.php ./test.py ./test.sh

If find turns up large numbers of results, you may find that the command being called chokes on the number of arguments.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • 3
    Shouldn't find add the results only to a number that makes safe to pass it to the shell? At least is what `xargs` do... in principle it never chokes for too much arguments. – Rmano Nov 23 '13 at 20:09
  • 3
    @Rmano: I've seen `find` (and `xargs`) on Solaris emit more arguments than could be consumed. The `xargs` (and `find`) in [GNU's findutils`](http://savannah.gnu.org/git/?group=findutils) seem to behave more sensibly, but not everyone uses GNU. – johnsyweb Nov 24 '13 at 01:55
  • 6
    @Johnsyweb, all POSIX `find` would try to avoid reaching the limit on the number of arguments. And that includes Solaris' (10 at least). Where it may fail is if you do something like `find ... -exec ksh -c 'cmd "$@" "$@"' sh {} +` or `find ... -exec ksh -c 'files="$*" cmd "$@"' sh {} +`, but `find` can't really be blamed for that. Note that GNU `find` was one of the last `find` implementations to support `+` (used to be a pain to port script to GNU systems). – Stephane Chazelas Feb 19 '14 at 14:30
43

From man find:

-exec command ;

Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of ';' is encountered. The string '{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a '\') or quoted to protect them from expansion by the shell. See the EXAMPLES sec section for examples of the use of the '-exec' option. The specified command is run once for each matched file. The command is executed in the starting directory. There are unavoidable security problems surrounding use of the -exec option; you should use the -execdir option instead.

-exec command {} +

This variant of the -exec option runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files. The command line is built in much the same way that xargs builds its command lines. Only one instance of '{}' is allowed within the command. The command is executed in the starting directory.

So, the way I understand it, \; executes a separate command for each file found by find, whereas \+ appends the files and executes a single command on all of them. The \ is an escape character, so it's:

ls testdir1; ls testdir2 

vs

ls testdir1 testdir2

Doing the above in my shell mirrored the output in your question.

example of when you would want to use \+

Suppose two files, 1.tmp and 2.tmp:

1.tmp:

1
2
3

2.tmp:

0
2
3

With \;:

 find *.tmp -exec diff {} \;
> diff: missing operand after `1.tmp'
> diff: Try `diff --help' for more information.
> diff: missing operand after `2.tmp'
> diff: Try `diff --help' for more information.

Whereas if you use \+ (to concatenate the results of find):

find *.tmp -exec diff {} \+
1c1,3
< 1
---
> 0
> 2
> 30

So in this case it's the difference between diff 1.tmp; diff 2.tmp and diff 1.tmp 2.tmp

There are cases where \; is appropriate and \+ will be necessary. Using \+ with rm is one such instance, where if you are removing a large number of files performance (speed) will be superior to \;.

user664833
  • 18,397
  • 19
  • 91
  • 140
matchew
  • 19,195
  • 5
  • 44
  • 48
  • I can read the man page too. And I did, but I don't think I understand the difference between using ; vs + – Ankur Agarwal May 22 '11 at 00:01
  • 2
    i dont think the -1 was fair, I explained my understaning of the man. I didn't just copy the man and leave. but I have edited my response to include a better example. – matchew May 22 '11 at 00:05
14

find has special syntax. You use the {} as they are because they have meaning to find as the pathname of the found file and (most) shells don't interpret them otherwise. You need the backslash \; because the semicolon has meaning to the shell, which eats it up before find can get it. So what find wants to see AFTER the shell is done, in the argument list passed to the C program, is

"-exec", "rm", "{}", ";"

but you need \; on the command line to get a semicolon through the shell to the arguments.

You can get away with \{\} because the shell-quoted interpretation of \{\} is just {}. Similarly, you could use '{}'.

What you cannot do is use

 -exec 'rm {} ;'

because the shell interprets that as one argument,

"-exec", "rm {} ;"

and rm {} ; isn't the name of a command. (At least unless someone is really screwing around.)

Update

the difference is between

$ ls file1
$ ls file2

and

$ ls file1 file2

The + is catenating the names onto a command line.

user664833
  • 18,397
  • 19
  • 91
  • 140
Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
  • 1
    I understand what you are saying. I am asking whats the difference between using ; vs + – Ankur Agarwal May 21 '11 at 23:58
  • 1
    sorry , but did you read my question or my comment carefully ? May be I need to rephrase it. Why is there a different o/p when I use semicolon with exec in find versus when I use plus with exec in find ? – Ankur Agarwal May 22 '11 at 00:05
  • 2
    This is an excellent explanation for WHY the command is like this, that the accepted answer doesn't cover. Thanks! – Sherwin Yu Oct 28 '14 at 20:30
6

The difference between ; (semicolon) or + (plus sign) is how the arguments are passed into find's -exec/-execdir parameter. For example:

  • using ; will execute multiple commands (separately for each argument),

    Example:

    $ find /etc/rc* -exec echo Arg: {} ';'
    Arg: /etc/rc.common
    Arg: /etc/rc.common~previous
    Arg: /etc/rc.local
    Arg: /etc/rc.netboot
    

    All following arguments to find are taken to be arguments to the command.

    The string {} is replaced by the current file name being processed.

  • using + will execute the least possible commands (as the arguments are combined together). It's very similar to how xargs command works, so it will use as many arguments per command as possible to avoid exceeding the maximum limit of arguments per line.

    Example:

    $ find /etc/rc* -exec echo Arg: {} '+'
    Arg: /etc/rc.common /etc/rc.common~previous /etc/rc.local /etc/rc.netboot
    

    The command line is built by appending each selected file name at the end.

    Only one instance of {} is allowed within the command.

See also:

kenorb
  • 155,785
  • 88
  • 678
  • 743
-5

we were trying to find file for housekeeping.

find . -exec echo {} \; command ran over night in the end no result.

find . -exec echo {} \ + have results and only took a few hours.

Hope this helps.

Ron
  • 1