简体   繁体   English

在列表上使用mv命令并重命名每个文件

[英]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. 每个文件(我们将称为file.nii)在每个患者文件夹中都具有相同的名称,并且我希望在移动它们时根据其患者ID对其重命名。 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: 我已经创建了一个列表(将其称为dti.txt),并且了解它需要看起来像下面发布的内容:

    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. 我坚持要尝试重命名每个与患者ID相对应的文件,因此file.nii将变为117.nii,119.nii,依此类推。 Their patient ID is in the file path in the list if that helps. 如果有帮助,他们的患者ID在列表的文件路径中。 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): 您必须从父目录中提取患者ID,这是使用路径/my/mri/da​​ta/117/file.nii(其中117是患者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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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