
What should the file be renamed to? Well that is determined by '/dev/random', a random number generator. The next part of the command after the semicolon, tells the FOR statement what to do, it says to rename the file given in the 'i' variable ("$i") to something else: 'mv -n "$i" '. The 'for i in' is a FOR statement, it sets the variable 'i' to each object in succession in the directory listing given by `ls *.jpg`. Again, I doubt that you will ever actually encounter an overwrite when using /dev/random, but just in case the -n or -i options will prevent it from occurring.Įdit: Let's quickly explain how this bash shell command works, shall we? First of all the `ls *.jpg` does a directory listing for all files in the directory ending in '.jpg' (the * is a wild card that matches any pattern). Because no "overwrites" occurred in my testing, I did not test that this command works when overwrites are encountered, but it should work since the "mv -n" works properly. jpg to randomly numbered JPEG files, such as 36582.jpg, etc.
#Mass rename bash code
What should the updated code be in order to no longer overwrite files as they are randomly renamed?įor i in `ls *.jpg` do mv -n "$i" `od -An -N2 -i /dev/random | tr -d " "`.jpg doneīasically, the "-n" option to the rename command, "mv", just tells "mv" not to overwrite a file that already exists, while the "-i" option to "mv" (substitute -i for -n in the above command) will provide an interactive prompt asking for your input if the "mv" ever tries to overwrite a file.Ĭaveat: I just tested this command, and it works properly to rename JPEG files having extensions. For those of us who are not as comfortable creating code, would someone mind re-pasting the exact code that we could paste into the terminal command once the correct (photo) directory is chosen within Terminal? That would be great.īelow is the code I used, which does successfully randomly rename batch files, but DOES NOT prevent the files from being overwritten as they are randomly renamed:įor i in `ls *.jpg` do mv "$i" `od -An -N2 -i /dev/random | tr -d " "`.jpg done The folder no longer had 2330 pictures, but 2250.Īfter I had made this mistake and researched further, I noticed a comment on this post suggested to also include "mv -n" within the code so as to prevent anything from getting overwritten.

I have successfully used the expanded code (pasted below) to successfully do this, but I did notice that an obvious overwrite took place. I too am trying to use Terminal to do a batch (random) rename of 2300 pictures for a digital photo frame. Thank you all for this ongoing post, even if it was years ago. Need updated code please to prevent overwriting files
