Home > Software design >  uuidgen and $RANDOM doesn't change in find -exec argument
uuidgen and $RANDOM doesn't change in find -exec argument

Time:02-21

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 \;

CodePudding user response:

find . -iname test.txt -exec bash -c '
    for i do
        cp "$i" "/Volumes/EXT/$RANDOM"
    done' _ {}  

You can use -exec with , to pass multiple files to a bash loop. You can't use command subs (or multiple commands at all) in a single -exec.

CodePudding user response:

This should work:

find ... -exec bash -c 'cp "{}" /Volumes/somewhere/$(uuidgen)'
  • Related