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
1 Answer
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.
.mgz?