[英]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
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: 对于前两个单词的80个变体(80个不同的种类),我想重命名所有这些文件,以使数字增加100-例如:
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 不幸的是,我不知道如何重命名它们,我对遵循https://unix.stackexchange.com/questions/40523/rename-files-by-incrementing-a-number上的解决方案感到幸运在文件名内
But i can't get it to work for the inside of the name, i'm running on Ubuntu 18.04 if that helps 但是我不能在名称中使用它,如果可以,我正在Ubuntu 18.04上运行
If you can get hold of the Perl-flavoured version of rename
, that is simple like this: 如果你能得到的Perl的口味版本保持
rename
,这是简单的是这样的:
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." 如果您看不懂Perl,则表示... “按以下步骤进行一次替换。无论您在一行中看到一串彼此相邻的数字(
\\d+
),请记住它们(因为我将其放在括号中) ,然后将其替换为该串数字( $1
)加100的求值表达式。” . 。
Remove the -n
if the dry-run looks correct. 如果空运行看起来正确,请除去
-n
。 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" . 唯一的“棘手部分”是在替换末尾使用
e
,这意味着评估替换中的表达式-或我称其为“计算的替换” 。
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 您可以将其放入变量中,然后使用
mv
命令重命名,而不必回显更改的文件名。
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
Mark's perl-rename solution looks great but you should apply it twice with a bump of 50 to avoid name conflict. Mark的perl-rename解决方案看起来不错,但您应套用两次,每次颠倒50,以避免名称冲突。 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.py( https://rene-file-renamer.sourceforge.io ),对其使用命令(也要两次)
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. rene会更容易一些,因为它会自动向您显示更改并询问您是否要进行更改,并且如果您改变主意,则可以撤消操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.