Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)
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
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
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 08:34 PM.
Reason: Better code
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.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
Quote:
Originally Posted by ephesus
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.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.