xargsを使用して画像を一括変換して変換します。出力ファイルの名前を指定するには?

xargsを使用して画像を一括変換して変換します。出力ファイルの名前を指定するには?

私たち全員が知っているように、次のコマンドを使用して画像を単一のPDFに変換できます(画像ファイル名が正しい順序であると仮定します)。

convert *.jpg output.pdf

ただし、何千もの画像が関連している場合、このアプローチはあまりにも多くのRAMを占有する可能性があります。もう1つのアイデアは、画像をPDFに一括変換し、結果のPDFを単一のPDFにリンクすることですpdftk

pdftk output-*.pdf cat output output.pdf

これまで、次のコマンドを使用して画像を一括変換してみましたxargs

ls *.jpg | xargs -d $'\n' -t -n 100 bash -c 'convert "$@" output.pdf'

convert毎回古いPDFを削除することを除いてうまくいきます。

質問:バッチ番号を数えて順次生産する方法はありますか?たとえばoutput-1.pdf...、output-9.pdf

ベストアンサー1

xargsGNUに切り替えることができればparallelする{#}実行中のシリアル番号を含む代替文字列を提供します。

たとえば、与えられた

$ touch File{001..050}.jpg

それから

$ printf '%s\0' *.jpg | parallel --null -n 5 echo convert {} -o output{#}.pdf
convert File001.jpg File002.jpg File003.jpg File004.jpg File005.jpg -o output1.pdf
convert File006.jpg File007.jpg File008.jpg File009.jpg File010.jpg -o output2.pdf
convert File011.jpg File012.jpg File013.jpg File014.jpg File015.jpg -o output3.pdf
convert File016.jpg File017.jpg File018.jpg File019.jpg File020.jpg -o output4.pdf
convert File021.jpg File022.jpg File023.jpg File024.jpg File025.jpg -o output5.pdf
convert File026.jpg File027.jpg File028.jpg File029.jpg File030.jpg -o output6.pdf
convert File031.jpg File032.jpg File033.jpg File034.jpg File035.jpg -o output7.pdf
convert File036.jpg File037.jpg File038.jpg File039.jpg File040.jpg -o output8.pdf
convert File041.jpg File042.jpg File043.jpg File044.jpg File045.jpg -o output9.pdf
convert File046.jpg File047.jpg File048.jpg File049.jpg File050.jpg -o output10.pdf

おすすめ記事