简体   繁体   English

使用Bash脚本在子文件夹中执行ImageMagick转换

[英]Execute ImageMagick convert in subfolders with Bash script

I have multiple series of jpegs. 我有多个系列的jpeg。 Each series is in its own folder. 每个系列都在自己的文件夹中。 I wish to convert each series into a single pdf. 我希望将每个系列转换为单个pdf。 I would further like the pdf to have the same name as the folder it is in. 我希望pdf与它所在的文件夹具有相同的名称。

To do this on the command line in each folder I can use the following command: 要在每个文件夹的命令行中执行此操作,我可以使用以下命令:

convert *.jpg `echo ${PWD##*/}`.pdf

I would like to do it from the top level folder with a bash script. 我想从顶级文件夹中使用bash脚本执行此操作。

You might want to use find for this: 您可能希望使用find来实现此目的:

find . -type d -exec bash -c 'cd "$0" || exit; shopt -s nullglob; f=( *.jpg ); ((${#f[@]})) && echo convert "${f[@]}" "${PWD##*/}.pdf"' {} \;

This will recurse into each subdirectory and for each execute the Bash commands: 这将递归到每个子目录中,并为每个子目录执行Bash命令:

cd "$0" || exit
shopt -s nullglob
f=( *.jpg )
((${#f[@]})) && echo convert "${f[@]}" "${PWD##*/}.pdf"
  • The $0 will expand to the argument given to Bash, ie, the directory found by find . $0将扩展为给予Bash的参数,即find的目录。 The exit is here as a security in case it's impossible to cd in there: you don't want to have random commands executed in this case. exit在这里作为安全保护,以防无法在那里进行cd :您不希望在这种情况下执行随机命令。
  • shopt -s nullglob : if there are no matches, the globs expand to nothing. shopt -s nullglob :如果没有匹配,则globs扩展为空。 Useful if there are subdirectories with no filenames ending in .jpg . 如果存在没有以.jpg结尾的文件名的子目录,则很有用。
  • build an array f of all the filenames in current directory with a filename ending in .jpg . 构建当前目录中所有文件名的数组f ,文件名以.jpg结尾。 This array is empty if there are no such files (thanks to the nullglob shell option). 如果没有这样的文件,这个数组是空的(感谢nullglob shell选项)。
  • If the array f is non-empty, execute the command: 如果数组f非空,请执行以下命令:

     echo convert "${f[@]}" "${PWD##*/}.pdf" 

    (by the way, your echo ${PWD##*/}.pdf is wrong , see below). (顺便说一下,你的echo ${PWD##*/}.pdf错误的 ,见下文)。

I put an echo in front of convert , so that nothing is actually performed; 我在convert前放了一个echo ,所以实际上什么都没有; the command is shown on standard output, for you to visually check if everything works as you expect. 该命令显示在标准输出上,供您直观地检查一切是否按预期工作。 If this is the case, just remove the echo ! 如果是这种情况,只需删除echo


You asked in a comment: why do you say the following line is wrong? 你在评论中问道:为什么你说下面这行是错的?

convert *.jpg `echo ${PWD##*/}`.pdf

you even say since it worked . 你甚至说它起作用了 To work and to be correct are not equal in computer science. 工作正确在计算机科学中并不相同。 The it works sentence should be understood in the full following form: it works until it fails . 它的作品句子应该用以下完整的形式来理解: 它一直有效直到失败 The most obvious case where this fails is if the directory name contains spaces: imagine a directory name of hello world then convert will see hello as an argument on its own, and world.pdf as the last argument. 失败的最明显的情况是目录名称是否包含空格:想象一下hello world的目录名称然后转换将自己看到hello作为参数,而world.pdf作为最后一个参数。 Very likely not what you want. 很可能不是你想要的。 Besides, the following is correct (and shorter and easier to understand): 此外,以下是正确的(更短更容易理解):

convert *.jpg "${PWD##*/}.pdf"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM