简体   繁体   中英

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 . I would like to change the first underscore to a hyphen and leave everything else so it looks like this text-text_mixturelettersnumbers.file .

I have tried rename -n 's/(\\w+)_(\\w+_.)/$1-$2/' * and many different versions thereof but nothing is happening. 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. 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 :

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

The "${f/_/-}" replaces the first _ with - in variable f .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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