Search code examples
imagebashpdfbatch-processingimagemagick-convert

Batch convert PNGs to individual PDFs while maintaining deep folder hierarchy in bash


I've found a solution that claims to do one folder, but I have a deep folder hierarchy of sheet music that I'd like to batch convert from png to pdf. What do my solutions look like?

I will run into a further problem down the line, which may complicate things. Maybe I should write a script? (I'm a total n00b fyi)

The "further problem" is that some of my sheet music spans more than one page, so if the script can parse filenames that include "1of2" and "2of2" to be turned into a single pdf, that'd be neat.

What are my options here?

Thank you so much.


Solution

  • Updated Answer

    As an alternative, the following should be faster (as it does the conversions in parallel) and also able to handle larger numbers of files:

    find . -name \*.png -print0 | parallel -0 convert {} {.}.pdf
    

    It uses GNU Parallel which is readily available on Linux/Unix and which can be simply installed on OSX with homebrew using:

    brew install parallel
    

    Original Answer (as accepted)

    If you have bash version 4 or better, you can use extended globbing to recurse directories and do your job very simply:

    First enable extended globbing with:

    shopt -s globstar
    

    Then recursively convert PNGs to PDFs:

    mogrify -format pdf **/*.png