Is there a way to pass more than one argument from find -exec
to a bash -c
sub-shell?
Given this working invocation which searches all videos matching a glob pattern (eg "*dogs.mp4
) and then executing ffmpeg
in a sub-shell with a check and warning in case the file already exists:
find . -iname "*.mp4" -exec bash -c 'if [[ -f "${1%.*}_540.mp4" ]]; then echo "Already exists: $1"; else ffmpeg -i "$1" -vf "scale=540:-1" "${1%.*}_540.mp4" ; fi ;' _ {} +
In order to avoid hard-coding the size I replaced 540
with $2
and then pass the size as parameter after {}
(to _ {} 540 +
) so that the command becomes:
find . -iname "*.mp4" -exec bash -c 'if [[ -f "${1%.*}_$2.mp4" ]]; then echo "Already exists: $1"; else ffmpeg -i "$1" -vf "scale=%2:-1" "${1%.*}_%2.mp4" ; fi ;' _ {} 540 +
The line above throws an error: find: -exec: no terminating ";" or "+"
:
My understanding is that {}
is the placeholder for the file name and {} +
(or {} \;
) must be at the end of the command.
In this scenario how to pass an additional parameter to bash -c
from the -exec
clause?
{} \;
then there is no need to loop over matches inside the subshell.+
does some kind of speed optimization and in this specific case with+
the subshell is not getting called for each single match – ccpizza Mar 01 '22 at 22:32+
. If you don't want multiple files passed to the command for each invocation you shouldn't be using+
. See the spec (I provided the link) and search for "-exec" and you'll find this. – Wildcard Mar 01 '22 at 22:48find ... -print0 | xargs-0 ...
so that it doesn't break with funny file names – ccpizza Mar 01 '22 at 22:54find ... -exec ... {} +
is completely safe from breakage regardless of special filenames but okay. (If that surprises you see additional background in http://unix.stackexchange.com/q/321697/135943) – Wildcard Mar 01 '22 at 22:58