如何重命名bash中的文件以增加名称? [英] How to rename files in bash to increase number in name?
问题描述
我有几千个文件,命名如下:
I have a few thousand files named as follows:
Cyprinus_carpio_600_nanopore_trim_reads.fasta
Cyprinus_carpio_700_nanopore_trim_reads.fasta
Cyprinus_carpio_800_nanopore_trim_reads.fasta
Cyprinus_carpio_900_nanopore_trim_reads.fasta
Vibrio_cholerae_3900_nanopore_trim_reads.fasta
对于前两个单词的80个变体(80个不同的种类),我想重命名所有这些文件,以使数字增加100-例如:
for 80 variations of the first two words (80 different species), i would like to rename all of these files such that the number is increased by 100 - for example:
Vibrio_cholerae_3900_nanopore_trim_reads.fasta
将成为
Vibrio_cholerae_4000_nanopore_trim_reads.fasta
或
Cyprinus_carpio_300_nanopore_trim_reads.fasta
将成为
Cyprinus_carpio_400_nanopore_trim_reads.fasta
Unfortunately I can't work out how to get to rename them, i've had some luck with following the solutions on https://unix.stackexchange.com/questions/40523/rename-files-by-incrementing-a-number-within-the-filename
但是我无法在名称中使用它,如果有帮助,我可以在Ubuntu 18.04上运行
But i can't get it to work for the inside of the name, i'm running on Ubuntu 18.04 if that helps
推荐答案
如果您可以掌握Perl风格的rename
版本,则很简单,就像这样:
If you can get hold of the Perl-flavoured version of rename
, that is simple like this:
rename -n 's/(\d+)/$1 + 100/e' *fasta
示例输出
'Ciprianus_maximus_11_fred.fasta' would be renamed to 'Ciprianus_maximus_111_fred.fasta'
'Ciprianus_maximus_300_fred.fasta' would be renamed to 'Ciprianus_maximus_400_fred.fasta'
'Ciprianus_maximus_3900_fred.fasta' would be renamed to 'Ciprianus_maximus_4000_fred.fasta'
如果您看不懂Perl,则表示... 执行以下单个替换.无论何时在一行中看到一串彼此相邻的数字(\d+
),请记住它们(因为我将其放在括号中),然后将其替换为该串数字的计算表达式($1
)加100." .
If you can't read Perl, that says... "Do a single substitution as follows. Wherever you see a bunch of digits next to each other in a row (\d+
), remember them (because I put that in parentheses), and then replace them with the evaluated expression of that bunch of digits ($1
) plus 100.".
如果空运行看起来正确,则删除-n
.唯一的棘手部分" 是替换结尾处使用e
,这意味着求值替换中的表达式-或者我称其为 计算得出的替换" .
Remove the -n
if the dry-run looks correct. The only "tricky part" is the use of e
at the end of the substitution which means evaluate the expression in the substitution - or I call it a "calculated replacement".
这篇关于如何重命名bash中的文件以增加名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!