0

I am writing a script in bash on Mac and need to run a command to convert a certain MRI image file in each subdirectory in a given directory.

The subdirectories are named sub_01, sub_02, .. sub_40.

The command is mri_convert input_image output_image

How I can loop over all 40 subdirectories?

1 Answers1

0

Using find:

find . -maxdepth 2 -mindepth 2 -path "./sub_*" -exec ls {} \;

Use mindepth and maxdepth to specify that you want to search the directory structure "one level down" only and then use -path to search for the required directory patterns. Use exec to process the returned entries. In this case, we are simply running ls on the returned paths. Change ls for the command to execute. If you can run the command on multiple entries at once, substitute ; for +

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18