Name changing script

I have a problem, and I really need help with this. And I imagine that I will have to learn a new level of scripting to complete this. But I have over 5,000 pictures that are misnamed with the extension .jpg. They're not jpg, they're png, and it's extremely important that they're correctly filed. I need a script that will take all .jpg files, keep the names before the extension, and change the extension to .png. I tried something like

mv *.jpg* .png

but obviously, it didn't work. Does anybody have any information on how I would even go about doing this??

Also, I am using Kali Rolling, with the BASH shell.

With a question like this it ALWAYS helps to know what operating system and shell you're using...

Assuming you're using a shell that provides the basic parameter expansions required by the POSIX standards, you could try something like:

for file in *.jpg
do	mv "$file" "${file%.jpg}.png"
done
1 Like

Thank you for that, but as a new-ish user, my understanding of your answer is limited. Could you explain to me what exactly is happening right after the do command?? The punctuation is unfamiliar. Learning what it means could help me a lot during the future.

---------- Post updated at 09:14 PM ---------- Previous update was at 09:08 PM ----------

@ Don Cragun

Also, I just tested your script, and it works. Thank you so much for your time. But still, if you could explain to me, or point me to a place where I might learn, what exactly was scripted, it would be a tremendous help to me.

The move command:

mv old new

moves the file named old to replace (or, if new did not exist, create) the file named new.
The command:

for file in *.jpg
do	mv "$file" "${file%.jpg}.png"
done

Runs that move command once for every file in the current working directory with a name ending with the string .jpg with the shell variable file set to the name of one of those files. With old being "$file" , the name of the old file is the expansion of that variable protected by double quotes to be a single operand even if the old filename contains field separators (such as <space> or <tab>). With new being the parameter expansion ${file%.jpg} which expands the shell variable named file removing the smallest string matched by the filename matching pattern .jpg from the end of the contents of the variable and appends the string .png to the end of that variable expansion, and, again is enclosed in double quotes to protect agains field separators being present in the filename that is being expanded.

If you look at the man page for your shell, you'll find that there are several variable expansion operators in addition to the % operator that was used in the above expansion that can be used to remove the longest trailing pattern match, the shorted leading pattern match, the longest leading pattern match, return the number of characters in the contents of the variable; and with many shell, lots of other operators are also available.