Contents of folder /home/User/Desktop/DR


... and so on (increments by 1 up until the number 40)

Contents of folder /home/User/Desktop/rsfMRI


... and so on (these values are random, but they have a RANGE, the 4 digit value can go up to 40 but these values can be repeated (we can have two 0001), and the 2 digits that follow the underscore alternates between 01 and 03).

What I am trying to do is use a match to find the digits "####_##" (4 digits followed by underscore and 2 other digits) from folder rsfMRI and REPLACE and RENAME the "#####" (5 digits in the file name) of DR folder.

So that the output would be


How can I do that?

Edit: There are the same number of files in both folders. I want it to go through the files in each folder in order so that the first file in DR folder matches first file in rsfMRI folder, and so on.

  • So MCU_0001_01 should match dr_subject00001_Z.nii and rename it to dr_subject0001_01.nii, correct? What does MCU_0001_03 match then? – Jesse_b Feb 10 '18 at 23:06
  • @Jesse_b Yes, exactly :) So MCU_0001_03 will match dr_subject00002 and so the renamed file will be dr_subject0001_03. I want them to match each other in the order as they are listed in the folder. – hsayya Feb 10 '18 at 23:09
  • What command are you using to list the folder? – Jesse_b Feb 10 '18 at 23:09
  • @Jesse_b I was thinking of putting them all in a text file (with the directories and files listed in the txt file) in order. But I'm not sure if this is the best way to go about it. – hsayya Feb 10 '18 at 23:17
  • 1
    I'm missing something; how are you matching (4 digits _ 2 digits) to a 5-digit filename? Do the just go in some sort of matched order? – Jeff Schaller Feb 10 '18 at 23:31

You could use a parallel array in bash (assuming you have the same number of files in each folder):



for((i=0; i < ${#drfiles[@]}; i++))
    echo mv -- "${drfiles[i]}" "DR/dr_subject${mrifiles[i]#*_}"

The drfiles array contains the DR files; the mrifiles array contains the files from the rsfMRI directory that match the pattern: (something) (4 digits) underscore (2 digits).

We then loop through the array (keyed to the number of files in the DR folder) and print out a sample mv/rename command. The source of the rename is simply the currently-indexed DR file. The target of the rename operation is the known prefix DR/dr_subject followed by the result of pruning the corresponding MRI file of any leading text until the first underscore. If the MRI files are always named "MCU", you could simplify the rename target to: DR/dr_subject${mrifiles[i]#rsfMRI/MCU_}.

  • Thank you Jeff, but I had a question about this line in particular: "mrifiles=(rsfMRI/*[0-9][0-9][0-9][0-9]_[0-9][0-9])" ... if I wanted the last digit to be either a 1 or 3, how would I do that? Would I write it out to be like this: "mrifiles=(rsfMRI/*[0-9][0-9][0-9][0-9]_[0-9][1,3])"? or does that not make sense? – hsayya Feb 11 '18 at 3:48
  • Just use: [13] — no comma – Jeff Schaller Feb 11 '18 at 4:36
  • When I implemented that command I got: mv -- /home/User/Desktop/DR/dr_subject00000.nii.gz DR/dr_subjectMCU_0001_01 – hsayya Feb 11 '18 at 15:47
  • I want the output to be: "dr_subject0001_01" without the MCU – hsayya Feb 11 '18 at 15:48

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.