For example, say a given directory has 1000 TIFF files named sample1.TIFF, sample2.TIFF, etc., all the way to sample1000.TIFF. I want to merge each pair of files: sample1.TIFF and sample2.TIFF are merged into sample1_2.TIFF, sample3.TIFF and sample4.TIFF are merged into sample3_4.TIFF, and so on. When finished I will have 500 merged TIFF files remaining. How can I do this with Unix/Perl/AIX scripting?


For merging the TIFFs, you can use tiffcp. For your example, you can write a simple loop in Perl:

for ($i= 1; $i<= 100; $i+=2) { 
    $j = $i + 1;    
    system("tiffcp sample$i.TIFF sample$j.TIFF sample$i\_$j.TIFF");


If there is no sequence on the TIFF files, i.e. in place of sample1.tif, sample2.tif, you have abc.tiff, def.tiff..and so on and you need to merge first 2 tiff in folder as per timestamp, then you can do the following:

Sort the TIFF files by timestamp using ls -lt, and then extract the TIFF filenames from the output. You can either do this using awk (see below) or if you have access to vim, then you can use Ctrl-v to remove the unwanted columns from each line of the output.

ls -lt | awk '$9 ~ "TIFF$" { print $9 } ' > sorted_TIFFs.list

Once you have the list of sorted TIFF files, you can traverse through them and run tiffcp using a simple Perl script. This script takes the sorted_TIFFs.list you generated in the previous step as the command line input.


open FILE, $file || die "Invalid filename";

while ($line=<FILE>) {
    $file1 = $line;
    if ($line = <FILE>)
        chomp ($line);
        $file2 = $line;
        $prefix= $file1;
        $prefix =~ s/\.TIFF//; #to avoid a.TIFF_b.TIFF
        system("tiffcp $file1 $file2 $prefix\_$file2\n");
    } else {
       print "No matching TIFF file, odd number of total files.\n";
  • If there is no sequence on the TIFF files, i mean in place of sample1.tif, sample2.tif, it will like abc.tiff, def.tiff..and so on. If i need to merge first 2 tiff in folder as per timestamp, then how could i achieve this? – Ranjeet Behera Sep 19 '14 at 3:39
  • Updated the solution, let me know if anything is unclear. – Harvinder Sep 19 '14 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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