简体   繁体   English

使用正则表达式批量重命名文件不起作用

[英]Batch rename files with regex not working

I've got many files on a linux server which have this format text_text_mixturelettersnumbers.file for example Hesperocyparis_goveniana_E00196073A.bam.bai or Hesperocyparis_forbesii_RBGEH19_bwa_out.txt .我在 linux 服务器上有很多文件具有这种格式text_text_mixturelettersnumbers.file例如Hesperocyparis_goveniana_E00196073A.bam.baiHesperocyparis_forbesii_RBGEH19_bwa_out.txt I would like to change the first underscore to a hyphen and leave everything else so it looks like this text-text_mixturelettersnumbers.file .我想将第一个下划线更改为连字符并保留其他所有内容,因此它看起来像这个text-text_mixturelettersnumbers.file

I have tried rename -n 's/(\\w+)_(\\w+_.)/$1-$2/' * and many different versions thereof but nothing is happening.我试过rename -n 's/(\\w+)_(\\w+_.)/$1-$2/' *及其许多不同的版本,但什么也没发生。 Could someone please point out what I've got wrong?有人可以指出我有什么问题吗?

Thanks谢谢

Markus马库斯

The util-linux rename does not have an option to display the results only. util-linux重命名没有仅显示结果的选项。 It is very basic.这是非常基本的。

If you want to list the files that contain two underscores before an extension, use如果要列出扩展名前包含两个下划线的文件,请使用

for f in *_*_*.*; do
  echo "$f => ${f/_/-}";
done

To actually rename, use mv :要实际重命名,请使用mv

for f in *_*_*.*; do
  mv -- "$f" "${f/_/-}";
done

The "${f/_/-}" replaces the first _ with - in variable f . "${f/_/-}"将第一个_替换为-在变量f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM