LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   bash script? how to rename files in random way? (https://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.

ephesus 06-07-2014 08:24 AM

You should probably note that if all of the pseudo-random numbers happen to be the same, you're going to lose all of your files except for the last one.

jlinkels 06-07-2014 09:29 AM

Quote:

Originally Posted by ephesus (Post 5184045)
You should probably note that if all of the pseudo-random numbers happen to be the same, you're going to lose all of your files except for the last one.

You should read the complete thread before posting.

jlinkels

ephesus 06-07-2014 11:16 AM

I only see 11 posts, am I missing something? Hashes don't guarantee no collisions.

jlinkels 06-07-2014 12:52 PM

Nice try.

First you were talking about all pseudo random numbers be the same so all files would be overwritten with the next. Now it was already a low probability that two random numbers are the same. However it was a significant probability, but for two or a few numbers be the same. Not for all numbers being the same.

Now you have read the thread and you see a hash was proposed as a solution. And you say that the hash might be the same for different pictures. Sure. The probability is like 1.5 x 10E-29 for a md5 sum collision.

Now please state your better solution.

jlinkels

jpollard 06-08-2014 08:13 AM

Quote:

Originally Posted by JulianTosh (Post 3634353)
No more filename collisions...
Code:

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


A simpler fix is:

Code:

for fname in *.jpg
do
  mv $fname ${RANDOM}_${fname}.jpg
done

The only difference is that if the same random number comes up, then the two pictures will be together.

This also has the benefit of still being able to identify the original name for the image.


All times are GMT -5. The time now is 07:48 PM.