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!

3
  • 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, 2020 at 9: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, 2020 at 9: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, 2020 at 9:42

2 Answers 2

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.

1
0
rename 's/-2-AF13-2.Scaf1.//' *.faa
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .