0

I am running the following code

for d in ./*/ ; do (cd "$d" && dcm2nii -n y -r y -x y -g n ../*/dicom/); done;

in order to execute the dcm2nii program on all subfolders of ./*/ (they are all titled "dicom") and to save the output to those same subfolders. The loop works but it runs twice creating duplicate output files within each "dicom" subfolder. Interestingly, if I specify absolute paths in both places, but with the wildcard * intact to include all folders within the parent directory, the loop runs only once as desired. How can I get the loop to run only once using relative paths?

2

I guess you had 2 subfolders while testing/executing your command.

Your code:

for d in ./*/ ; do (cd "$d" && dcm2nii -n y -r y -x y -g n ../*/dicom/); done;
  • first enters a subfolder $d
  • then does dcm2nii ... on ../*/dicom, which translates into: subfolder "dicom" of all folders of the superfolder
  • then goes to the second subfolder and does the same

Try this:

for d in ./*/ ; do (cd "$d" && dcm2nii -n y -r y -x y -g n dicom/); done;

or (possibly) this:

dcm2nii -n y -r y -x y -g n ../*/dicom/
  • for d in ./*/ ; do (cd "$d" && dcm2nii -n y -r y -x y -g n dicom/); done; did the trick, thanks! – focusbob Aug 12 '16 at 18:18

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.