简体   繁体   English

Linux Shell中文件的批量重命名

[英]Batch Renaming of Files in Linux Shell

I am working via ssh on a remote cluster and need to rename some files according to a scheme.我在远程集群上通过 ssh 工作,需要根据方案重命名一些文件。 The rename should work like:重命名应该像这样工作:

M-19-3778_P1-DX5-K47-232-Elev_A1-A1_L001_R1_001.fastq.gz into M-19-3778_P1-DX5-K47-232-Elev_A1-A1_L001_R1_001.fastq.gz
M-19-3778-P1-DX5-K47-232-Elev-A1-A1L001_R1.R.fq.gz

M-19-3778_P1-DX5-K47-232-Elev_A1-A1_L001_R2_001.fastq.gz into M-19-3778_P1-DX5-K47-232-Elev_A1-A1_L001_R2_001.fastq.gz
M-19-3778-P1-DX5-K47-232-Elev-A1-A1L001_R1.F.fq.gz

M-19-3779_P1-F48-F37-86-Cont_A2-A2_L001_R1_001.fastq.gz into M-19-3779_P1-F48-F37-86-Cont_A2-A2_L001_R1_001.fastq.gz进入
M-19-3779-P1-F48-F37-86-Cont-A2-A2L001_R2.R.fq.gz

M-19-3779_P1-F48-F37-86-Cont_A2-A2_L001_R2_001.fastq.gz into M-19-3779_P1-F48-F37-86-Cont_A2-A2_L001_R2_001.fastq.gz进入
M-19-3779-P1-F48-F37-86-Cont-A2-A2L001_R2.F.fq.gz

... ...

M-19-3830_P1-DX5-D12-221-Elev_F9-F9_L001_R1_001.fastq.gz into M-19-3830_P1-DX5-D12-221-Elev_F9-F9_L001_R1_001.fastq.gz
M-19-3830-P1-DX5-D12-221-Elev-F9-F9L001_R53.R.fq.gz

M-19-3830_P1-DX5-D12-221-Elev_F9-F9_L001_R2_001.fastq.gz into M-19-3830_P1-DX5-D12-221-Elev_F9-F9_L001_R2_001.fastq.gz
M-19-3830-P1-DX5-D12-221-Elev-F9-F9L001_R53.F.fq.gz

Iterations are only in the beginning of the names with 3778....迭代仅在名称的开头以 3778....

So far I've tried it with combinations of grep / sed / cat but did not succeed.到目前为止,我已经尝试使用grep / sed / cat的组合,但没有成功。 And I have no idea how to handle the iteration in the end of the name R1 , R2 ,..., my guess would be a combination of a for loop and an if -Statement but I just don't get this running.而且我不知道如何处理名称R1R2 ,...末尾的迭代,我的猜测是for循环和if -Statement 的组合,但我只是没有让它运行。

I would be grateful for any help!如果有任何帮助,我将不胜感激!

i=1
for name in *
do
    newName=${name%_L001_R1_001.fastq.gz}L001_R$i.R.fq.gz
    mv "$name" "$oldname"
    ((i++))
done

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

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