我想将名为 的文件重命名db_backup_2019_11_22-12_13_00.gz
为db_dump_2019_11_22-12_13_00.sql.gz
。
有没有办法使用模式匹配来实现这一点?我的意思是像这样:
mv db_backup_*-*.gz db_dump_$1-$2.sql.gz
其中$1
,$2
是匹配的部分。
答案1
这可以通过重命名命令:
rename 's/backup(.+?)(?=\.gz$)/dump$1.sql/' db_backup*
解释:
s/ # substitute
backup # literally "backup"
(.+?) # group 1, 1 or more any character, not greedy
(?=\.gz$) # positive lookahead, make sure we have ".gz" after
/ # WITH
dump # literally "dump"
$1 # content of group 1
.sql # literally ".sql"
/ # end substitute