为所有图像添加前缀(递归) [英] Add prefix to all images (recursive)

查看:119
本文介绍了为所有图像添加前缀(递归)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件夹,其中包含超过5000张图像,全部带有JPG扩展名.

I have a folder with more than 5000 images, all with JPG extension.

我想做的是递归地在所有图像上添加"thumb_"前缀.

What i want to do, is to add recursively the "thumb_" prefix to all images.

我发现了一个类似的问题:重命名文件和目录(添加前缀),但我只想将前缀添加到具有JPG扩展名的文件中.

I found a similar question: Rename Files and Directories (Add Prefix) but i only want to add the prefix to files with the JPG extension.

推荐答案

可能的解决方案之一:

find . -name '*.jpg' -printf "'%p' '%h/thumb_%f'\n" | xargs -n2  echo mv

Principe:找到所有需要的文件,并为标准mv命令准备参数.

Principe: find all needed files, and prepare arguments for the standard mv command.

注意:

  • 参数由'包围,以允许在文件名中使用空格.
  • 缺点是:与许多mp3文件一样,它不适用于包含'撇号本身的文件名.如果您需要移动更奇怪的文件名,请在下面检查.
  • 以上命令用于空运行(仅显示带有args的mv命令).对于实际工作,请删除echo假装mv.
  • arguments for the mv are surrounded by ' for allowing spaces in filenames.
  • The drawback is: this will not works with filenames what are containing ' apostrophe itself, like many mp3 files. If you need moving more strange filenames check bellow.
  • the above command is for dry run (only shows the mv commands with args). For real work remove the echo pretending mv.

任何文件名重命名.在外壳中,您需要一个定界符.问题在于,通常文件名(存储在shell变量中)通常可以包含分隔符本身,因此:

ANY filename renaming. In the shell you need a delimiter. The problem is, than the filename (stored in a shell variable) usually can contain the delimiter itself, so:

mv $file $newfile         #will fail, if the filename contains space, TAB or newline
mv "$file" "$newfile"     #will fail, if the any of the filenames contains "

正确的解决方案是:

  • 使用适当的转义符准备文件名
  • 使用容易理解任何文件名的脚本语言

可以使用内部的printf%q格式指令= print quoted bash 中准备正确的转义.但是这种解决方案很长而且很无聊.

Preparing the correct escaping in bash is possible with it's internal printf and %q formatting directive = print quoted. But this solution is long and boring.

恕我直言,最简单的方法是使用perl并使用零填充的print0,如下所示.

IMHO, the easiest way is using perl and zero padded print0, like next.

find . -name \*.jpg -print0 | perl -MFile::Basename -0nle 'rename $_, dirname($_)."/thumb_".basename($_)'

以上使用perl的功能来整理文件名,并最终重命名文件.

The above using perl's power to mungle the filenames and finally renames the files.

这篇关于为所有图像添加前缀(递归)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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