find . -type f -exec <command> '{}' \;
is not equivalent to
find . -type f | xargs <command>
observe:
$ find -type f
./b
./c
./e
./d
./a
$ find -type f -exec echo '{}' \;
./b
./c
./e
./d
./a
$ find -type f | xargs echo
./b ./c ./e ./d ./a
xargs
collects a bunch of arguments up to a given length and then executes the command with all arguments at once.
This is what gets executed from find -type f -exec echo '{}' \;
echo ./b
echo ./c
echo ./e
echo ./d
echo ./a
This is what gets executed from find -type f | xargs echo
echo ./b ./c ./e ./d ./a
This works well for commands that can take multiple arguments like md5sum
or file
. But does not work for commands that only take one argument at a time.
To make xargs
behave more like find -exec
you can add argument -n1
to xargs
:
$ find -type f | xargs -n1 echo
./b
./c
./e
./d
./a
-n1
tells xargs
to execute a command for every 1
arguments.
In your example command:
find . -type f -print0 | sort -Rz | xargs -0 -n1 <command>
Bonus: you can also make find -exec
to behave more like xargs
by closing the -exec
with \+
instead of \;
:
find -type f -exec <command> '{}' \+
find . -type f -print0 | sort -Rz | xargs -0 echo
, does it change if you run it multiple times? – Gilles 'SO- stop being evil' Aug 26 '16 at 22:36echo
command, it works as expected. But actually I want <command> to bemupdf
(I want to display images from folder in random order) - in that case it only displays randomly selected image and exits. Same with executable script with only one commandecho "$1"
in it - only first file name is echoed. When I use my first commandline:find . -type f -exec <command> '{}' \;
, all images are displayed in sequence (when I close one image, next image is displayed), but not in random order. – Maciek Łoziński Aug 29 '16 at 17:01