The simplest way of doing this would be with find
executing a short in-line script that converts each found file with the .tif
filename suffix:
find files/ -name '*.tif' -type f -exec sh -c '
for pathname do
convert "$pathname" "${pathname%.tif}.jpg"
done' sh {} +
The in-line sh -c
script above will be called by find
with batches of found pathnames to regular files with the .tif
suffix found under the files
directory. The ${pathname%.tif}.jpg
parameter substitution removes the suffix .tif
from the value of $pathname
and appends .jpg
at the end.
Use files/file*/
as the search paths instead of files/
to look only in these ~650 subdirectories of files
that start with the string file
, ignoring any other subdirectory.
This leaves the .tif
files in place, but you can remove them after successful conversion if you add && rm "$pathname"
after the convert
command in the loop.
In the zsh
shell, you could do away with find
and instead rely on the shell's more advanced filename globbing and string manipulation capabilities:
for pathname in files/**/*.tif(D-.); do
convert $pathname $pathname:r.jpg
done
Here, files/**/*.tif(D-.)
will expand to a list of all the regular files that have names ending in .tif
anywhere in or below the files
directory. The (D-.)
at the end of the glob makes the globbing pattern only match regular files (possibly with "hidden" names).
The :r
after $pathname
gives you the "root" of the pathname (the name without the filename extension).
Use files/file*/**/*.tif(D-.)
to restrict the search to only file in the file*
subdirectories.
With the bash
shell, you can do something similar to the above like so:
shopt -s dotglob failglob globstar
for pathname in files/**/*.tif; do
convert "$pathname" "${pathname%.tif}.jpg"
done
The shell options set here enable the globbing of hidden names and the use of the **
globbing operator. It also makes non-matching patterns generate an error, just like by default in the zsh
shell.