How to convert all png files to pdf in Bash?

烈酒焚心 提交于 2021-02-05 09:32:10

问题


for f in find *.png; do convert "$f" "$f".pdf; done

This is what I have to find the png files in the directory and convert them to pdf, but I get errors. What is a better way to do this in Bash?

convert: unable to open image `find': No such file or directory @ error/blob.c/OpenBlob/2705.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/504.
convert: no images defined `find.pdf' @ error/convert.c/ConvertImageCommand/3257.

回答1:


The list of filenames you are giving to the for loop literally contains find. I think what you meant to do is give the output of find, searching for all PNG images in or under the current directory, which is

for f in $(find . -iname '*.png'); do convert "$f" "$f".pdf; done

This will not handle spaces well. A better solution is to just run the conversion from within find itself,

find "$PWD" -iname '*.png' -execdir convert '{}' '{}'.pdf \;

Although note that you will wind up with filenames ending with .png.pdf




回答2:


If you prefer to have a file with .pdf instead of .png.pdf you can use :

  find .  -name '*.png' -exec sh -c 'convert $1 ${1%.png}.pdf' sh {} \;


来源:https://stackoverflow.com/questions/41668596/how-to-convert-all-png-files-to-pdf-in-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!