How to rename files in bash to increase number in

2019-06-13 15:48发布

问题:

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

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 

would become

Vibrio_cholerae_4000_nanopore_trim_reads.fasta

or

Cyprinus_carpio_300_nanopore_trim_reads.fasta

would become

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

But i can't get it to work for the inside of the name, i'm running on Ubuntu 18.04 if that helps

回答1:

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



回答2:

If there is only one number in your string then below two line of code should provide help you resolve your issue

filename="Vibrio_cholerae_3900_nanopore_trim_reads.fasta"
var=$(echo $filename | grep -oP '\d+')
echo ${filename/${var}/$((var+100))}

Instead of echoing the changed file name, you can take it into a variable and use mv command to rename it



回答3:

Considering the filename conflicts in the increasing order, I first thought of reversing the order but there still remains the possibility of conflicts in the alphabetical (standard) sort due to the difference to the numerical sort.
Then how about a two-step solution: in the 1st step, an escape character (or whatever character which does not appear in the filename) is inserted in the filename and it is removed in the 2nd step.

#!/bin/bash

esc=$'\033' # ESC character

# 1st pass: increase the number by 100 and insert a ESC before it
for f in *.fasta; do
    num=${f//[^0-9]/}
    num2=$((num + 100))
    f2=${f/$num/$esc$num2}
    mv "$f" "$f2"
done

# 2nd pass: remove the ESC from the filename
for f in *.fasta; do
    f2=${f/$esc/}
    mv "$f" "$f2"
done


回答4:

Mark's perl-rename solution looks great but you should apply it twice with a bump of 50 to avoid name conflict. If you can't find this flavor of rename you could try my rene.py (https://rene-file-renamer.sourceforge.io) for which the command would be (also applied twice) rene *_*_*_* *_*_?_* B/50. rene would be a little easier because it automatically shows you the changes and asks whether you want to make them and it has an undo if you change your mind.