lundi 4 mars 2019

How to randomly name file when using find exec in a bash script?

When it comes to quickly converting a bunch of files and randomly renaming them I use a pretty simple way to do so with a for loop:

for i in *; do convert [...] $i ../output/$RANDOM.jpg; done

Easy as that. The details what imagemagick does here aren't important here. It works as intended. It's just about how to handle the bash stuff.

Now my current case the folder does not only contain photos, it also does contain subfolders with other photos themself. Expected behavior is again that all photos are randomly renamed and the output files are merged in a single folder.

Since I don't know a way to recursively loop with for, I use a find construct here.

find . \( -iname "*.jpg" -or -iname "*.png" \) -exec convert [...] {} ../output/$RANDOM.jpg  \;

Problem is $RANDOM does only get called once, so it stays the same over the whole process and the images get overwritten again and again. So in fact the output folder does only one image, the one that got processed the last.

So the question is: How do I get the $RANDOM variable to change with each new file?

Kind regards!




Aucun commentaire:

Enregistrer un commentaire