0

I would like to come up with a bash script that would add a random alphanumeric string to the end of all the files. I would like the string to have a length of 10.

So if I had the filename: hello.jpg It would become: hello_v41e6ebadx_.jpg

Does anyone have any ideas on how to accomplish this?

mr hi
  • 21
  • 1

1 Answers1

3
for file in *.*
do
    name=${file%.*}
    ext=${file##*.}
    random=$(LC_CTYPE=C tr -cd 'a-zA-Z0-9' < /dev/urandom | head -c 10)
    mv "$file" "${name}_${random}_.${ext}"
done

You can also consider mktemp, if no "X"s in your file extensions are likely to trip it up.

that other guy
  • 109,738
  • 11
  • 156
  • 185