LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 05-10-2006, 08:48 PM   #1
lefty.crupps
Member
 
Registered: Apr 2005
Location: Minneap USA
Distribution: Debian, Mepis, Sidux
Posts: 470

Rep: Reputation: 32
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)
 
Old 05-10-2006, 09:19 PM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,052

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
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.
 
Old 05-21-2006, 08:32 PM   #3
lefty.crupps
Member
 
Registered: Apr 2005
Location: Minneap USA
Distribution: Debian, Mepis, Sidux
Posts: 470

Original Poster
Rep: Reputation: 32
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
 
Old 05-21-2006, 09:32 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
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
 
Old 05-21-2006, 09:49 PM   #5
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,052

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
Thanks homey, and you are right about the quotes. And the backups of course.

jlinkels
 
Old 05-21-2006, 09:58 PM   #6
lefty.crupps
Member
 
Registered: Apr 2005
Location: Minneap USA
Distribution: Debian, Mepis, Sidux
Posts: 470

Original Poster
Rep: Reputation: 32
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
 
Old 08-06-2009, 10:06 PM   #7
tawan
LQ Newbie
 
Registered: Aug 2009
Posts: 2

Rep: Reputation: 1
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.
 
1 members found this post helpful.
Old 08-06-2009, 11:30 PM   #8
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
No more filename collisions...

for fname in *.jpg;
do
mv "$fname" $(echo "$fname" | sha1sum | cut -f1 -d' ').jpg
done
 
Old 08-06-2009, 11:39 PM   #9
tawan
LQ Newbie
 
Registered: Aug 2009
Posts: 2

Rep: Reputation: 1
Quote:
Originally Posted by Admiral Beotch View Post
No more filename collisions...
indeed! great.
 
Old 06-11-2013, 06:24 PM   #10
nbritton
LQ Newbie
 
Registered: Jun 2013
Location: Dubuque, IA
Distribution: Red Hat Enterprise Linux, Mac OS X, Ubuntu, Fedora, FreeBSD
Posts: 1

Rep: Reputation: Disabled
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.

Last edited by nbritton; 06-11-2013 at 07:34 PM. Reason: Better code
 
Old 06-07-2014, 08:24 AM   #11
ephesus
LQ Newbie
 
Registered: Jun 2014
Posts: 2

Rep: Reputation: Disabled
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.
 
Old 06-07-2014, 09:29 AM   #12
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,052

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
Quote:
Originally Posted by ephesus View Post
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
 
Old 06-07-2014, 11:16 AM   #13
ephesus
LQ Newbie
 
Registered: Jun 2014
Posts: 2

Rep: Reputation: Disabled
I only see 11 posts, am I missing something? Hashes don't guarantee no collisions.
 
Old 06-07-2014, 12:52 PM   #14
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,052

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
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
 
Old 06-08-2014, 08:13 AM   #15
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 1,990

Rep: Reputation: 512Reputation: 512Reputation: 512Reputation: 512Reputation: 512Reputation: 512
Quote:
Originally Posted by JulianTosh View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
Script to Rename Many Files geeman2.0 Programming 3 04-05-2006 01:45 PM
BASH USAGE: mass files rename sirpelidor Linux - General 8 09-20-2005 12:36 AM
BASH script showing random behavior chatterbug89 Programming 12 07-19-2005 10:45 PM
Need script to rename files joe_stevensen Programming 5 12-05-2003 06:12 PM


All times are GMT -5. The time now is 11:32 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration