Need helping making tiff2pdf command recursive


Recommended Posts

I have hundreds of multipage .tiff files that I would like to convert into .pdf files. I found a useful linux command to do that - tiff2pdf.

The problem is, it doesn't work recursively. Can anyone give me a hand in making a simple script that can recursively convert my tiff files to pdf? I can do bash scripts but I'm not go with "for each do x".

A sample directory structure is

Folder001

--001 truck.tiff

--001 cruse 1.tiff

--001 tank.tiff

Folder002

--002 truck.tiff

--002 banana.tiff

--002 city.tiff

--002 something.tiff

And I know the format of the command needs to be


tiff2pdf -o "FILENAME.PDF" "FILENAME.TIF"
[/CODE]

Any pointers on how I can do this or would anyone be willing to help me?

Link to comment
Share on other sites

Make a PHP or Bash script to output all the filenames and then construct the command list from that.

e.g.

find . -name "*.tif"

Will get you a list of the tif files, then you just need to use Bash commands to change the extension from .tif to .pdf and put 2 arrays through the command

Link to comment
Share on other sites

I found you can use the -exec argument in find, which is really helpful in addition to -name "*.tif", and that works good as input, but it doesn't work for output (needs to be .PDF).

Link to comment
Share on other sites


#!/bin/bash

# This is a simple script to search a directory for TIFF images and convert them to PDF.
# It relies on find, tiff2pdf, and egrep to work its magic.
# Files ending in '.tiff' or '.tif' (in any case) are supported.

if [ $# -ne 1 ]; then
echo "Syntax: $0 DIRECTORY_CONTAINING_TIFFS_TO_CONVERT"
exit 1
fi

dir_to_search=$1
if [ ! -d "$dir_to_search" ]; then
echo "ERROR! Invalid search directory \"$dir_to_search\""
exit 1
fi

tiffs=$(find "$dir_to_search" -type f -regextype 'posix-egrep' -iregex '.*\.tif[f]{0,1}$' -exec echo -n {}';' \;)

OLDIFS="$IFS"
IFS=';'

for tiff in $tiffs; do
echo $tiff | grep -qsiE '\.tif$'
if [ $? -eq 0 ]; then
extension_length=3
else
extension_length=4
fi
pdf="${tiff:0:$((${#tiff}-$extension_length))}pdf"

echo "tiff2pdf -o \"$pdf\" \"$tiff\""
tiff2pdf -o "$pdf" "$tiff"
done

IFS="$OLDIFS"

exit 0
[/CODE]

Link to comment
Share on other sites

This topic is now closed to further replies.