How to rename files in bash to increase number in name?

后端 未结 4 1070
鱼传尺愫
鱼传尺愫 2021-01-07 02:20

I have a few thousand files named as follows:

Cyprinus_carpio_600_nanopore_trim_reads.fasta                
Cyprinus_carpio_700_nanopore_trim_reads.fasta             


        
4条回答
  •  隐瞒了意图╮
    2021-01-07 03:07

    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
    

    Sample Output

    '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'
    

    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.".

    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".

提交回复
热议问题