Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

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.

like image 883
John Deverall Avatar asked Jul 24 '26 19:07

John Deverall


2 Answers

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.

like image 63
Charles Duffy Avatar answered Jul 26 '26 23:07

Charles Duffy


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

like image 25
UtsavShah Avatar answered Jul 26 '26 23:07

UtsavShah