I want to execute one command for every file in a folder, but randomly. Something like:find . -type f -exec <command> '{}' \;but every time in another order. This is closest to my needs, but 1) it doesn't work, 2) order is random, but always the same:find . -type f -print0 | sort -Rz | xargs -0 <command>
Executing command on every file in folder in random order
shell;find;sort;random
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 ./axargs 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 ./becho ./cecho ./eecho ./decho ./aThis is what gets executed from find -type f | xargs echoecho ./b ./c ./e ./d ./aThis 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> '{}' \+