1

I'm having trouble setting up a script that copies and converts files. What I basically want is a script that takes a subject e.g.

folder/subjects/subject_name/mri/norm.mgz 

copies this file into

folder/investigation_folder/fs_norms/subject_name/ 

and converts the norm.mgz to norm.nii using mri_convert, and does this with +300 subjects (in the subjects folder).

This should be fairly simple... Can anybody help me out here?

I'm a big newb! This is a example of what i would like to do - with a single subject:

mkdir /mnt/projects/Project_folder/thalamic_subnuclei/fs_norms/gXXX/

cp /mnt/projects/Project_folder/subjects/gXXX/mri/norm.mgz /mnt/projects/Project_folder/thalamic_subnuclei/fs_norms/gXXX/

mri_convert mnt/projects/Project_folder/thalamic_subnuclei/fs_norms/gXXX/norm.mgz mnt/projects/Project_folder/thalamic_subnuclei/fs_norms/gXXX/norm.nii

All my subjects are called g followed by 3-4 numbers. Now I need to set up a loop that does this with the +300 subjects in the /mnt/projects/Project_folder/subjects/ folder.

2
  • 2
    You are probably right about the simplicity. Will you edit your question snow us how far you've gotten and where you're stuck? A script is a simple list of commands. Write down each command when performing the steps on one file. Then put the commands in a loop. If you get stuck, we'll help you to fix your script, but we need to know where you are stuck. Apr 24, 2018 at 11:34
  • See surfer.nmr.mgh.harvard.edu/fswiki/mri_convert for mri_convert's syntax.
    – dessert
    Apr 24, 2018 at 13:28

1 Answer 1

2

Here is an example how the FOR loop can be applied within a bash script to solve this task:

#!/bin/bash

# Execute do-done inner part for each "item" under "folder/subjects/"
for subject in folder/subjects/*
do
    # Check whether the source file exists
    if [[ -f ${subject}/mri/norm.mgz ]]
    then
        # ${subject##*/} will cut the parent path from the value of the variable '$subject'
        echo "Processing: ${subject##*/}"

        # Create the new subject directory
        mkdir -p "folder/investigation_folder/fs_norms/${subject##*/}"

        # Copy the .mgz file into the new directory
        cp "${subject}/mri/norm.mgz" "folder/investigation_folder/fs_norms/${subject##*/}/"

        # Do the conversion to .nii (I'm not sure this is the correct syntax of mri_convert that you are using)
        mri_convert "folder/investigation_folder/fs_norms/${subject##*/}/norm.mgz" "folder/investigation_folder/fs_norms/${subject##*/}/norm.nii"
    fi
done

You must log in to answer this question.

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