Search code examples
bashshellrandomfindcommand-substitution

uuidgen and $RANDOM doesn't change in find -exec argument


I want to get all the instances of a file in my macosx file system and copy them in a single folder of an external hard disk. I wrote a simple line of code in terminal but when I execute it, there is only a file in the target folder that is replaced at every occurrence it finds. It seems that the $RANDOM or $(uuidgen) used in a single command return only one value used for every occurrence {} of the find command. Is there a way to get a new value for every result of the find command? Thank you.

find . -iname test.txt -exec cp {} /Volumes/EXT/$(uuidgen) \;

or

find . -iname test.txt -exec cp {} /Volumes/EXT/$RANDOM \;

Solution

  • This should work:

    find ... -exec bash -c 'cp "$1" /Volumes/somewhere/$(uuidgen)' _ {} \;
    

    Thanks to dan and pjh for corrections in comments.