I have a directory called 10K_TIF (main directory). Within this main directory are multiple sub-directories each containing tif images. I want to read each TIF image and extract its path using a bash command. 10K_TIF over 15TB in size.



I managed to echo each sub directory, also managed to echo all tif images but these are in separate commands.

4 Answers 4


You can use find:

find 10K_TIF -iname '*TIF'

To loop over the results and do something with each image, you can use:

find 10K_TIF -iname '*TIF' | while read tif; do echo "Found: $tif"; done

The above assumes sane file names, with no whitespace or strange characters. If your file names can include such characters, use this instead:

find 10K_TIF -iname '*TIF' -print0 | 
    while IFS= read -r -d '' tif; do echo "Found: $tif"; done
find 10K_TIF -iname "*.tif"

And if you want to process each file, like finding the content type of the file

find 10K_TIF -iname "*.tif" -print0 | xargs -0 -n1 file

Or to calculate md5sum

find 10K_TIF -iname "*.tif" -print0 | xargs -0 -n1 md5sum

If you want (only) list all *.tiff files in all subdirectories with bash it is easiest to use ** pattern (globstar):

shopt -s globstar
ls **/*.tiff

You can use find for finding all files and passing them to whatever command you want to use by using

find 10K_TIF -iname "*.tif" -type f -print0 | xargs -0 -n1 <command>

This searches through 10K_TIF and all subdirectories, matching everything that ends with .tif (case-insensitive) and is a regular file (ie. directories, special files and symbolic links ending with .tif are not processed).

If you want to process, for example, symbolic links and regular files, you can use

find 10K_TIF -iname "*.tif" \( -type f -o -type l \) -print0 | xargs -0 -n1 <command>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .