找到并重命名不正确的文件扩展名的所有图片 [英] Find and rename all pictures with incorrect file extension

查看:237
本文介绍了找到并重命名不正确的文件扩展名的所有图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来自动化重命名所有图像文件扩展名错误。到目前为止,我至少发现如何获得所有这些文件的列表:



$ p $ find / media / folder / -name * .jpg -exec file {} \; | grep'PNG \ | GIF'> foob​​ar.txt
find / media / folder / -name * .png -exec file {} \; | grep'JPEG \ | GIF'>> foob​​ar.txt
find / media / folder / -name * .gif -exec file {} \; | grep'JPEG \ | PNG'>> foob​​ar.txt

但是,我也想自动重命名。我尝试了一些东西,比如:
$ b $ pre $ find $ / media / folder / -name * .jpg -exec file {} \; | grep -l PNG |重命名s / .jpg / .png /

但是在这种情况下 grep - l 或者 grep -lH 不要只列出像我想的那样的文件名。

-l <​​/ code>和 -H 标志 grep 在你的例子中没有用。与标准输入一起使用时,这些标志不起作用,就像你的例子来自管道。这些标志只有在你指定了文件(或者用于递归的 -r 标志)时才起作用,例如:

  grep -rl PNG路径/ to / dir1 file2 file3 

在你的例子中, -l <​​/ code>没有任何作用,所以输出是匹配 PNG 的完整行,示例可能看起来像这样:

lockquote

icon.png:PNG图像,512 x 512,8位/颜色RGBA,非隔行


为了只获取文件名,也许你可以像下面这样截断冒号后的所有内容:

  find / media / folder / -name * .jpg -exec file {} \; | grep PNG | sed -e s /:.*// |重命名为s / .jpg / .png / 


I'm looking for a way to automate renaming all images with a wrong filename extension. So far I at least found out how to get the list of all these files:

find /media/folder/ -name *.jpg -exec file {} \; | grep  'PNG\|GIF' > foobar.txt
find /media/folder/ -name *.png -exec file {} \; | grep 'JPEG\|GIF' >> foobar.txt
find /media/folder/ -name *.gif -exec file {} \; | grep 'JPEG\|PNG' >> foobar.txt

However, I would also like to automate the renaming. I tried things like

find /media/folder/ -name *.jpg -exec file {} \; | grep -l PNG | rename s/.jpg/.png/

but in this case grep -l or grep -lH don't list only filenames like I thought they would.

解决方案

The -l and -H flags of grep are not useful in your example. These flags have no effect when used with the standard input, like in your example coming from the pipe. These flags only work if you specify files (or directories and the -r flag for recursion), for example:

grep -rl PNG path/to/dir1 file2 file3

In your example the -l has no effect, so the output is the complete lines that matched PNG, which in your example probably look something like this:

icon.png: PNG image, 512 x 512, 8-bit/color RGBA, non-interlaced

To get only the filename, maybe you can cut off everything after the colon like this:

find /media/folder/ -name *.jpg -exec file {} \; | grep PNG | sed -e s/:.*// | rename s/.jpg/.png/

这篇关于找到并重命名不正确的文件扩展名的所有图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆