问题
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