1

I understand that find -tests -execdir <command> '{}' ';' runs command for every matching file against the test(s) specified. The command, when using -execdir, is executed in the same parent directory as the matching file (for every matching file), as {} stands for the basename of the matching file.

Now the question is: how is this done when working with multiple files all at once using + instead of ';'? If I use find -tests -execdir <command> '{}' +, all of the files are supplied as arguments to the command specified (in a manner that doesn't exceed max args). How does find carry out <command> on all of them at once?

1

Let's say your find locates the following files:

./foo/bar
./foo/baz
./foo/quux

If you use -execdir [...]+, the effective resultant command will be:

( cd ./foo; command bar baz quux )

As opposed to (effectively) this, if you use -execdir [...] \;:

( cd ./foo; command bar )
( cd ./foo; command baz )
( cd ./foo; command quux )

The same is true for -exec rather than execdir, but it will specify the path rather than changing the working directory. If you use -exec [...]+, the effective resultant command will be:

command ./foo/bar ./foo/baz ./foo/quux

As opposed to (effectively) this, if you use -exec [...] \;:

command ./foo/bar
command ./foo/baz
command ./foo/quux

Let's see how this behaves with files found in two directories:

$ tree
.
├── bar
│   ├── bletch
│   └── freeble
└── foo
    ├── bar
    ├── baz
    └── quux
$ find . -type f -exec echo {} \;
./foo/baz
./foo/quux
./foo/bar
./bar/bletch
./bar/freeble
$ find . -type f -execdir echo {} \;
./baz
./quux
./bar
./bletch
./freeble
$ find . -type f -exec echo {} +
./foo/baz ./foo/quux ./foo/bar ./bar/bletch ./bar/freeble
$ find . -type f -execdir echo {} +
./baz ./quux ./bar
./bletch ./freeble
  • So you are saying it kinda sorts and uniquifies the directories which it should cd to and performs the command per directory? – joker Oct 17 '18 at 21:30
  • Essentially, yes. – DopeGhoti Oct 17 '18 at 21:44
  • Interesting! Did you figure this out yourself or do have some sort of reference? – joker Oct 17 '18 at 21:54
  • 1
    A combination of tilting my head wile squinting at the output of man find and a healthy amount of experimentation. – DopeGhoti Oct 17 '18 at 21:55
  • 1
    It's not really a bug; there's nothing in the requirements that says all files must be processed with one exec command call. It may easily depend on the order the files and directories are read by readdir() – Stephen Harris Oct 17 '18 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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