Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm surprised I couldn't find an easy script for this online. I have a directory with lots of subdirectories, each filled with TIFFs. How can I batch convert subfolders of multiple images (TIFFs) into single PDFs each named after their parent folders? CLI solution and ability to set output directory preferred

ex.
dir > 1998 > Jan011998 > 1.tif 2.tif ... → Jan011998.pdf (or, ideally, 1998-01.pdf)
dir > 1999 > 02231999 > 1.tif 2.tif ... → Feb231999.pdf (or 1999-02-23.pdf)

share|improve this question
    
irfanview can create the multi-page PDFs, but as far as I know it cannot be done from the command line. I suppose you could create a macro to do the job. AutoHotkey can do this, including sending the correct keystrokes into the application to drive it. –  hdhondt Apr 17 at 0:18
    
superuser.com/questions/81290/… ? –  Jasper Apr 17 at 8:56
    
@Jasper: That's a start, but I'd need some sort of bash scripting or something to iterate through the bottom subdirectories (and I'm not sure how the batching would work either) –  sean Apr 18 at 11:54
    
are you on win or linux or sth. else? –  Jasper Apr 18 at 12:10
    
@Jasper: Mac, so UNIX is fine and I can run Windows through Parallels –  sean Apr 19 at 19:38

1 Answer 1

Here is a little script that should do the trick:

#!/bin/bash

dirs=$(find . -type f -name "*.tiff" | xargs dirname |sort -u)

for d in $dirs; do
    #ls $d/*.tiff  # just debug info, should list all desired tiffs
    #echo $(cut -d/ -f3 <<<$d)  # debug info, should be desired output file name
    convert $d/*.tiff $(cut -d/ -f3 <<<$d).pdf

done

First, find is used to determine all directories that contain TIFF files. Then we use ImageMagick's convert to convert all TIFFs in each directory to a PDF file whose name is created from the directory. You can easily include a different output directory:

    convert $d/*.tiff somewhereElse/$(cut -d/ -f3 <<<$d).pdf
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.