0

I have many files on my Linux machine with file names like this:

Veillonella-sp.-AF13-2-AF13-2.Scaf1.faa
Weissella-cibaria-strain-AM27-24-AM27-24.Scaf1.faa
Streptococcus-salivarius-strain-AF24-6AC-AF24-6AC.Scaf1.faa

There is a part of the file name that is repeated. I would like to have only the unique names. I want to edit this file name to have only this:

Veillonella-sp.-AF13-2.faa
Weissella-cibaria-strain-AM27-24.faa
Streptococcus-salivarius-strain-AF24-6AC.faa

Can you please show me a way to edit these file names?

My linux distribution is CentOS

Thanks!

Kusalananda
  • 333,661
Anu
  • 1
  • 1
    Please [edit] your question and tell us i) which Linux distribution you are using (so we can know which rename command you have) and ii) add more example file names so we can understand what changes and what is the same. – terdon Apr 27 '20 at 09:24
  • Thanks. Why do you insist ona for loop? That isn't necessarily the best solution. And what does "the names in the file are repeated" mean? What file? How can names be repeated? – terdon Apr 27 '20 at 09:38
  • Yes, I dont need to do this by for loop. Somehow a part of the file name is repeated. Such as "Veillonella-sp.-AF13-2-AF13-2.Scaf1.faa" has "AF13-2" repeated twice; Weissella-cibaria-strain-AM27-24-AM27-24.Scaf1.faa has "AM27-24" repeated twice . I would like to remove this from all files. – Anu Apr 27 '20 at 09:42

2 Answers2

2

Using a Perl implementation of rename:

rename -v 's/(-\w+?-\d+?)\1/$1/' *.faa

This would delete any repetition of -word-digits in any of the filenames matched by *.faa in the current directory.

It does so by by matching -\w+?-\d+?, i.e. -word-digits, and then that same string again. Such a match would be replaced by just the first instance of the string.

Kusalananda
  • 333,661
  • 1
    instructions for installing perl-rename on CentOS: https://www.linuxquestions.org/questions/centos-111/rename-command-does-not-want-in-cent-os-7-a-4175615551/#post5769407 – terdon Apr 27 '20 at 10:04
0
rename 's/-2-AF13-2.Scaf1.//' *.faa
Ole Tange
  • 35,514