Running similar analysis [duplicate]

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.

Answer

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.

Attribution
Source : Link , Question Author : Ali Radaideh , Answer Author : muru

Leave a Comment