1

I have subfolders in current directory like this:

ls

apples  bananas  oranges  potatoes

Each subfolder includes different number of .jpg images.

cd apples

ls

000000522638.jpg  000000522713.jpg  000000522751.jpg

I have custom python program downloaded from different source which modifies images, and takes input image path and output image path as argument:

python modifyImage.py path/to/input/image.jpg path/to/output/image.jpg

Problem statement:

I want to apply this program for each image in given subdirectories(apples,oranges etc.). There are a lot of folders and images with arbitrary names, so I can not do this by hand. Possible solution would be, firstly finding all *.jpg image paths and send these as argument to python script.

And I made script for updating *.jpg files only in current directory like this

for file in *.jpg
do
  python modifyImage.py /"$file" /"$file"
done

But I need above script to work with subdirectories. Please help.

0

1 Answer 1

2

From Super User they tell us how to loop through sub-directories:

for d in */ ; do
    echo "$d"
done

Using this reference you can use nested for loops:

for d in */ ; do
    for file in "$d"/*.jpg
    do
        python modifyImage.py /"$file" /"$file"
    done
done
1
  • It works, thanks for precise answer and explanation. :)
    – nintendo64
    May 17, 2018 at 1:36

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