2

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.

Sample:

10K_TIF/4320cc/4320cc_01_rectified.tif
10K_TIF/4320cc/4320cc_02_rectified.tif
10K_TIF/4520cc/4520cc_01_rectified.tif
10K_TIF/4520cc/4520cc_02_rectified.tif

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

2

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
1
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
1

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
0

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>

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.