LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   bash script? how to rename files in random way? (http://www.linuxquestions.org/questions/linux-software-2/bash-script-how-to-rename-files-in-random-way-443646/)

lefty.crupps 05-10-2006 08:48 PM

bash script? how to rename files in random way?
 
I have a lot of photos that I need to put into a random-order slideshow. Easiest would be to just drag-and-drop the whole lot. Is there any way to scan a directory and batch-rename a group of files into random names, but keeping the extension? (.jpg in this case)

jlinkels 05-10-2006 09:19 PM

Something like:

Code:

for fname in *.jpg
do
  mv fname $RANDOM.jpg
done

If you want something more sophisticated use the rename command.

rename s/oldpart_of_filename.jpg/newname.jpg/ oldfile.jpg

http://www.faqs.org/docs/abs/HTML/

jlinkels

PS. I shouodl not have taken taht asty irish Coffee but I hope I gave somathing ti rename files adn not to delterr them all.

lefty.crupps 05-21-2006 08:32 PM

thanks jlinkels, but how do i use this code? I have created a file called ranren.sh and have tried running
Code:

lefty@1[photo_randomized]$ ./ranren.sh
mv: cannot stat `fname': No such file or directory
mv: cannot stat `fname': No such file or directory
mv: cannot stat `fname': No such file or directory
lefty@1[photo_randomized]$

As you can see, it won't accept "fname" as a file name. What should I change in the script? How would I cycle through each file name (160 total) and give it a random name? (preferably in a subfolder) Many of the files are photo_0001.jpg photo_0002.jpg but others have different names and need to be randomized as well.

thanks for any more help/advice!

p.s. I tried
Code:

ls | mv * $RANDOM.jpg
but it wouldn't move multiple files anywhere except to a directory

homey 05-21-2006 09:32 PM

jlinkels forgot to put the $ before fname like this.... $fname. I also put it inside quotes to help with file names that have spaces in them like ---- my latest pic.jpg ----
Also, it's good to have backups in case your code is goofy :) and wouldn't hurt to put an echo before the mv command to give it a test run. Then, remove the echo when it looks good.

So, the code may look like this
Code:

for fname in *.jpg
do
  echo mv "$fname" $RANDOM.jpg
done


jlinkels 05-21-2006 09:49 PM

Thanks homey, and you are right about the quotes. And the backups of course.

jlinkels

lefty.crupps 05-21-2006 09:58 PM

that worked! (but I had to run the script twice to affect all of the files-- only the first 119 changed the first round??)

Thank you both for the help and quick responses!

lefty.crupps

tawan 08-06-2009 10:06 PM

random rename - files go missing
 
hi, using the above

Code:

for fname in *.jpg
do
  echo mv "$fname" $RANDOM.jpg
done

files go missing as I guess the same random number is generated more than once.

I have no fix.

JulianTosh 08-06-2009 11:30 PM

No more filename collisions...

for fname in *.jpg;
do
mv "$fname" $(echo "$fname" | sha1sum | cut -f1 -d' ').jpg
done

tawan 08-06-2009 11:39 PM

Quote:

Originally Posted by Admiral Beotch (Post 3634353)
No more filename collisions...

indeed! great.

nbritton 06-11-2013 06:24 PM

My two cents...
 
Quote:

for fname in *.jpg; do
mv "$fname" $(echo "$fname" | sha1sum | cut -f1 -d' ').jpg;
done
That doesn't work on Mac OS X, md5 is a better choice due to ubiquity. The bash one liner below should work on most platforms. It's also worth noting that both of our solutions are only pseudo random...

Code:

for i in *.jpg; do mv $i $(echo $i | md5).jpg; done
If you need more randomness or uniqueness, one way is with openssl:
Code:

for i in *.jpg; do mv $i $(openssl rand -rand $i -hex 16).jpg; done
The length of the random filename is controlled by the "-hex #" switch, it can be set to any length. Each image is used as the seed for each rename operation, for all intents and purposes this should guarantee truly random behavior, provided you don't have duplicate images.


All times are GMT -5. The time now is 04:56 AM.