【问题标题】:Using mv command on a list and renaming each file在列表上使用 mv 命令并重命名每个文件
【发布时间】:2018-04-11 15:08:34
【问题描述】:

我正在尝试将我分析过的一些图像从一个文件夹移动到一个新文件夹并重命名它们。每个文件(我们将调用 file.nii)在每个患者文件夹中的名称都相同,我希望在移动它们时根据患者 ID 对它们进行重命名。我已经创建了一个列表(让我们称之为 dti.txt),并且知道它需要看起来像下面发布的内容:

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

我坚持的是尝试重命名与其患者 ID 对应的每个文件,因此 file.nii 将变为 117.nii、119.nii 等。如果有帮助,他们的患者 ID 位于列表中的文件路径中。这是完整文件路径的样子:

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

如果这是一个愚蠢的问题或格式不正确,我深表歉意。我对编码世界还是陌生的。感谢您的帮助!

【问题讨论】:

    标签: linux list rename zsh mv


    【解决方案1】:
    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
    

    【讨论】:

    • 您的答案看起来不错,但请对其进行编辑,以便代码位于代码块中并且解释更清晰。
    【解决方案2】:

    您必须从父目录中提取患者 ID,这是使用路径 /my/mri/da​​ta/117/file.nii(其中 117 是患者 ID)的一个示例:

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

    然后您可以将其用作新文件名的前缀

    mv $oldfile ${patientId}_mri.nii 
    

    例如

    【讨论】:

      猜你喜欢
      • 2012-12-07
      • 2013-08-27
      • 1970-01-01
      • 2011-08-06
      • 2020-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-29
      相关资源
      最近更新 更多