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
  • 2
    Yes, I have quite a lot of ideas about this. –  Jan 31 '13 at 19:03
  • possible duplicate of [How do you generate passwords?](http://stackoverflow.com/questions/101362/how-do-you-generate-passwords) covers alphanumeric string generation, the rest if just variable manipulation and file renaming – Ciro Santilli OurBigBook.com Apr 28 '15 at 08:41

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
  • 116,971
  • 11
  • 170
  • 194