2

I have a folder with multiple sub-folders and files. They are all similar in name but the only difference is the patient's number

Example

Analysis/Patient_01/Brain/image_patient_01_merged.nii
Analysis/Patient_02/Brain/image_patient_02_merged.nii
Analysis/Patient_03/Brain/image_patient_03_merged.nii
.
.
.

and so on.

In the main folder (Analysis) I want to make a script to do the same thing for each .nii file in a loop.

example

fslstats image_patient_01_merged.nii -M
fslstats image_patient_02_merged.nii -M
fslstats image_patient_03_merged.nii -M
.
.
.
.

In other words, I need to do the same analysis on the nii files in each subdirectory.

1 Answers1

2

Using bash, in the Analysis folder, use a simple for loop:

for f in */*/*.nii
do
    flstats "${f##*/}" -M
done

*/*/*.nii will expand to Patient_01/Brain/image_patient_01_merged.nii, etc., and ${f##*/} gets just the filename image_patient_01_merged.nii from that.

muru
  • 197,895
  • 55
  • 485
  • 740
  • Thanks, But each subdirectory has many .nii files for each patient

    image_patient_01_merged.nii crop_patient_01_original.nii segmented_patient_01_new.nii . . . and so on. I only need to do the analysis for the image_patient_01_merged.nii file only in each patient directory and not to all .nii files

    – Ali Radaideh Nov 22 '17 at 12:12
  • Is there a pattern to the nii filename? – muru Nov 22 '17 at 12:14
  • Hi Muru, I have added the details in the previous comment – Ali Radaideh Nov 22 '17 at 13:43