bash 将一堆带有“.jpg”扩展名的PNG图像重命名为“.png”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30684796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:08:41  来源:igfitidea点击:

Rename a bunch of PNG images with ".jpg" extension to ".png"

imagebashscriptingpngjpeg

提问by Frank

So I have a folder with thousands of image files, all of them saved as .jpg.

所以我有一个包含数千个图像文件的文件夹,所有这些文件都保存为.jpg.

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:

问题是其中一些文件实际上是 PNG 图像文件,因此它们不会在很多程序中打开,除非我手动将它们的扩展名更改为.png. 例如,Ubuntu 图像查看器抛出此错误:

"Error interpreting JPEG image file (Not a JPEG file: starts with 0x89 0x50)"

“解释 JPEG 图像文件时出错(不是 JPEG 文件:以 0x89 0x50 开头)”

I've already ran a hexdumpof some of these files to confirm this error and it checks out.

我已经运行了其中一些文件的hexdump以确认此错误并检查出来。

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!

我正在寻找一种简单的方法来在其他文件中查找具有错误扩展名的所有文件并更改它们的扩展名。例如,我将如何使用 bash 脚本执行此操作?到目前为止我不知道。所有帮助表示赞赏!

回答by Mat

for f in *.jpg ; do
  if [[ $(file -b --mime-type "$f") = image/png ]] ; then
    mv "$f" "${f/%.jpg/.png}"
  fi
done

This gets a list of .jpgfiles, then for each calls the fileutility on it to get the mime type. If that's image/png, then it renames the file using a string manipulationsubstitution.

这将获得一个.jpg文件列表,然后为每个文件调用file实用程序以获取 mime 类型。如果是image/png,则它使用字符串操作替换重命名文件。

回答by Dan Getz

You want to use fileand parse the output. You can check if it's a JPEG or a PNG by checking the initial part of the output:

您想使用file和解析输出。您可以通过检查输出的初始部分来检查它是 JPEG 还是 PNG:

F="filename.jpg"
TYPE=$(file -b $F)
[[ $TYPE == JPEG\ image\ data* ]] && echo "jpg"
[[ $TYPE == PNG\ image\ data* ]] && echo "png"

You can change the extension by stripping the .jpgwith basename. For example, the following will change the extension of the file $Fwith extension .jpgto .png:

您可以通过剥离.jpgwith来更改扩展名basename。例如,以下内容会将扩展名为 的文件$F的扩展名.jpg更改为.png

mv $F $(dirname $F)/$(basename $F .jpg).png

回答by Sriharsha Kalluru

Firstly get a list of files and make them in a file. Then process the list to a loop so that you can change files one by one automatically.

首先获取文件列表并将它们放在一个文件中。然后将列表处理为一个循环,以便您可以自动一个一个地更改文件。

#!/bin/bash

FILE=/tmp/list ## Assuming the list of files are in this file.
for file in $(cat $FILE); do 
   OLD_FILE=$file
   NEW_FILE=$(echo $file|sed -e 's/jpg$/png/')
   mv $OLD_FILE $NEW_FILE
done

回答by usr2564301

Use file (1)to determine the type of all files. Use grep (1)to filter this list, removing all proper JPEG files. What remains is a list of files that cannot be recognized as a JPEG.

使用file (1)以确定所有的文件类型。使用grep (1)过滤这个列表,删除所有适当的JPEG文件。剩下的是无法识别为 JPEG 的文件列表。

In a single line

在一行中

file *.jpg | grep -v "JPEG image data"

– make sure to verify that JPEG image dataindeed is what filereturns on a correctly identified JPG file.

– 确保验证JPEG image data确实是file正确识别的 JPG 文件返回的内容。

You can use sed (1)to remove the stuff after the colon :and end up with the file name only:

您可以使用sed (1)删除冒号后的内容:并仅以文件名结束:

file *.jpg | grep -v "JPEG image data" | sed 's/:.*//'

.. which is the end of my bash-fu. At this point I'd store the resulting list in a file (add > list) and use GREP in a text editor to rewrite all lines to a proper rename command, then run that.

..这是我的bash-fu的结束。此时我会将结果列表存储在一个文件中 (add > list) 并在文本编辑器中使用 GREP 将所有行重写为正确的重命名命令,然后运行该命令。

回答by Derek Kuether

I am not sure of the appropriate commands for a bash script but you could easily write a C++ file to try to open these files and catch the error. The files that you catch the error will not be the appropriate type (assuming that's your only issue). If you know those incorrect file types should be PNG, you can rename the file. Something like the pseudocode:

我不确定 bash 脚本的适当命令,但您可以轻松编写 C++ 文件来尝试打开这些文件并捕获错误。您捕获错误的文件将不是适当的类型(假设这是您唯一的问题)。如果您知道那些不正确的文件类型应该是 PNG,您可以重命名该文件。类似于伪代码:

For (ALL_FILES)
{
  try 
  {
    open(FILENAME);
  }
  catch
  {
    rename(FILENAME, NEW_FILENAME);
  }
}