0

I have one folder containing three thousand subjects imaging data. Each subject's folder has many other folders in it. I want to use a loop to extract one file from each subfolder and move them into another directory and rename them according to patient ID. E.g., /imaging/data/1100232/free surfer/MRI/lh_ribbon.mgz Here 1100232 folder Name changes for each patient and there are three thousand patients/folders. I want to extract lh_ribbon.mgz file for each patient and rename this file according to patient iD. I am very new to Linux can someone please help. Thanks

2
  • 1
    Is the ID always the 3rd subfolder and the extension always .mgz?
    – bu5hman
    Jul 7 '19 at 9:16
  • Yes, once I have extracted all ribbon.mgz, I have to change formate to nifti after unzipping them.
    – Nazlee
    Jul 7 '19 at 10:09
0

Since you are new we will build this solution stepwise.

A find to locate all of your files from /imaging/

find /imaging/ -iname *.mgz

Add an -exec \; to execute a command (we will start with echo) once for each file found ( the file name is represented by {})

find /imaging/ -iname *.mgz -exec echo {} \;

Use sh -c to allow more complex scripts in the -exec not just simple commands, passing in the destination directory and the name of the file found as $1 and $2

find /imaging/ -iname *.mgz -exec sh -c 'echo $1 $2' sh {} "/path/to/store/patient/files/" \; 2>/dev/null

finally compose the new file name from the patient ID at the 3rd directory up and do copy from the old location to the new using the new patient ID as a filename

find /imaging/ -iname *.mgz -exec sh -c 'p=${1#/*/*/}; p=${p%%/*}; echo cp $1 $2$p.mgz' sh {} "/path/to/store/patient/files/" \; 2>/dev/null

Note. I have left the cp command as just an echo so that you can run it and see that the output is correct before committing. If all is OK then just delete the echo and then run it.

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.