简体   繁体   中英

Using mv command on a list and renaming each file

I am trying to move some images that I have analyzed from one folder to a new folder and also rename them. Each file (we will call file.nii) is named the same in each individual patient folder and I want them to be renamed corresponding to their patient ID when I move them. I have already created a list (lets call it dti.txt) and understand that it will need to look something like what is posted below:

    for ii in $(cat dti.txt); do cd ${INFINITE_MRI_DATA}/${ii}; 
    mv file.nii destination; done

What I am stuck on is trying to rename every file corresponding to their patient ID so file.nii will become 117.nii, 119.nii, and so on. Their patient ID is in the file path in the list if that helps. This is what the full file path would look like:

    /gandg/infinite/imaging_data/individuals/inf0238/1/dti/dtifit/analysis/

I apologize if this is a silly question or if it is not formatted correctly. I am still new to the world of coding. I appreciate any help!

You have to extract the patient id from the parent directory, this is one example using the path /my/mri/data/117/file.nii (where 117 is the patient id):

patientId=$(readlink -f $ii | dirname | cut -f'/' 4)

And then you can use that as a prefix for your new filename

mv $oldfile ${patientId}_mri.nii 

For example

Create one file in that file put the path which consist of patient id

 i created k.txt file and mentioned the path

 cat k.txt
/my/dir/117/ol
/my/dir/118/ol

for i in `awk -F "/" '{print $4}' k.txt`; do for j in 1.txt 2.txt; do mv $j $i.nii; done;done

Used below command  to rename the old file with patientid.nii

where 1.txt 2.txt is old files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM