0

I am using Manjaro Gnu/Linux. I have a directory named files, and under this directory, I have around 650 sub directories, with names such as: file1, file2, file3,...

Under each sub directory there are varying number of '.tif' images (say, from 2 to 11).

I want to write a command/script to automatically convert all '.tif' images to '.jpeg'/'.jpg' such that they remain in the same sub directory and have the same name as before.

I know that there is the command:

convert source.tif ~/converted.jpg

Can you please help?

Thanks!

1
0

After you cd into the main directory containing the subfolders.

IFS=$'\n'
for i in $(find -type f -name "*tiff"); do 
    echo $i
    convert $i $i.jpg
done

find -type f -name "*tiff" -exec rm {} \;


IFS is the internal field separator

The one problem i am seeing is that i was not able to extract the basename of the file

Dont forget to first try in a small folder before you move to big

0

I would use a script similar to this one:

#!/bin/bash
if [[ $# -gt 0 ]]
then
  if [[ -d $1 ]]
  then
    cd $1
  else
    echo "Given path leads to a file or the directory you are loking does not exist!"
    exit 1
  fi
fi
for dir in */
do
  echo "Changing directory to $dir"
  cd $dir
  for image in *
  do
    if [[ -f "$image" ]]
    then
      filename="${image%.*}"
      extension="${image##*.}"
      if [[ "$extension" =~ ^ti[f]{1,2}$ ]]; then
        echo "Converting $image to ${filename}.jpg"
        convert "$image" -compress JPEG -quality 50 "${filename}.jpg"
      fi
    fi
  done
  cd - > /dev/null
done

Setting up your script:

  1. Save this as batchJPEGConvert.sh
  2. Use chmod 755 batchJPEGConvert.sh command to make this file executable
  3. Use sudo cp batchJPEGConvert.sh /usr/local/bin command to make this script accessible across your system

You can run this script by typing either batchJPEGConvert.sh or batchJPEGConvert.sh path. The first option will run this script in the current working directory. The other option will try to change the working directory to a given path and run the script there. This script will only convert files ending with .tiff or .tif extensions. It will also apply JPEG compression.

Note that this script only works for directories which structure is similar to this one:

test
├── 1
│   ├── polski.jpg
│   └── polski.tiff
└── 2
    ├── polski.jpg
    └── polski.tif

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.