用".jpg"重命名一堆PNG图像.扩展到".png" [英] Rename a bunch of PNG images with ".jpg" extension to ".png"
问题描述
所以我有一个包含数千个图像文件的文件夹,所有图像文件另存为.jpg
.
So I have a folder with thousands of image files, all of them saved as .jpg
.
问题在于,其中一些文件实际上是PNG图像文件,因此除非在我手动将其扩展名更改为.png
的情况下,否则它们不会在许多程序中打开.例如,Ubuntu图像查看器会引发以下错误:
The problem is that some of those files are actually PNG image files, so they don't open in a lot of programs, unless I manually change their extension to .png
. For example, the Ubuntu image viewer throws this error:
解释JPEG图像文件时出错(不是JPEG文件:以0x89开头 0x50)"
"Error interpreting JPEG image file (Not a JPEG file: starts with 0x89 0x50)"
我已经运行了其中一些文件的 hexdump 来确认此错误,并已将其检出.
I've already ran a hexdump of some of these files to confirm this error and it checks out.
我正在寻找一种简单的方法来查找其他文件中所有具有错误扩展名的文件并更改其扩展名.例如,我将如何使用bash脚本执行此操作?到目前为止,我还不知道.所有帮助都感激不尽!
I'm looking for a simple way to find all the files with the wrong extension among the other files and change their extension. How would I do this with a bash script for example? So far I have no idea. All help apreciated!
推荐答案
for f in *.jpg ; do
if [[ $(file -b --mime-type "$f") = image/png ]] ; then
mv "$f" "${f/%.jpg/.png}"
fi
done
这将获得一个.jpg
文件的列表,然后针对每个调用它的file
实用程序来获取mime类型.如果是image/png
,则它将使用字符串处理替换来重命名文件.
This gets a list of .jpg
files, then for each calls the file
utility on it to get the mime type. If that's image/png
, then it renames the file using a string manipulation substitution.
这篇关于用".jpg"重命名一堆PNG图像.扩展到".png"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!