John Deverall
John Deverall

Reputation: 6280

What does "find ... {} + || true" do?

I'm working on the deployment script for a web application. The original author of the script wrote the following which I don't quite understand.

find */target -name myapp*.jar -exec mv -t $CIRCLE_ARTIFACTS {} + || true

Googling around has got me so far, but I'm still a little unsure with what

{} + || true

is used for.

Upvotes: 3

Views: 872

Answers (2)

Charles Duffy
Charles Duffy

Reputation: 295472

The {} + suffix is part of the -exec action of find: It substitutes as many arguments as possible (that is, as many files found) on each mv invocation. (There's a limit to "as many as possible" because there's only so much space available in the region the operating system uses to store environment variables and command-line arguments, so if find has many results, multiple mv invocations can be needed even with -exec ... {} +).


Compare to this traditional command:

find */target -name 'myapp*.jar' -exec mv -t "$CIRCLE_ARTIFACTS" {} ';'

This runs mv once per file found. Since /bin/mv is an external executable, this is implicitly expensive.

By contrast,

find */target -name 'myapp*.jar' -exec mv -t "$CIRCLE_ARTIFACTS" {} +

...runs as few mv invocations as possible. (Note that this usage requires the -t extension, which is not specified by POSIX; thus, it's only available on GNU platforms).


Finally, || true is simply a boolean OR operation: If the find fails, then true runs, forcing the command to always result in a truthful result. If you're running with set -e, this prevents the script from being exited in the event of a failure.

This can also be more tersely written as ||:, as : is a synonym for true.

Upvotes: 4

UtsavShah
UtsavShah

Reputation: 877

The {} is replaced the filename found by the find command.

So it finds files with the find command, and executes mv for as many files as possible (this is marked by the +).

The || true is a logical OR to the rest of the command, which basically makes sure the command has a zero exit status (which might be important for the rest of the script

Upvotes: 1

Related Questions