1

I would like to rename some files. I have a lot of files with this format: ?????.pdb.

The name of this file is composed of 5 characters (numbers and letters) and the new name will be like this ????_biounit?.pdb. The first 4 characters of the old name must be preserved, while the fifth must be inserted after the word "biounits".

Some examples:

  • Original files:

    1b471.pdb 1caz4.pdb 1ca93.pdb
    
  • New files:

    1b47_biounit1.pdb 1caz_biounit4.pdb 1ca9_biounit3.pdb
    

I try to do it with the comand mv but I can't tell the difference between the first four characters and the fifth. Can someone help me please?

Quasímodo
  • 18,865
  • 4
  • 36
  • 73
Tommaso
  • 167
  • 1
  • 9

2 Answers2

4

With a shell loop:

for f in ?????.pdb; do mv "$f" "${f%?.pdb}_biounit${f#????}"; done
  • ${f%?.pdb} removes ?.pdb from the end of the file name.
  • ${f#????} removes ???? from the beginning of the file name.
Quasímodo
  • 18,865
  • 4
  • 36
  • 73
3

Using perl rename tool:

rename -n 's/(....)(.)/$1_biounit$2/' ?????.pdb

Remove -n if you're happy with the output.

pLumo
  • 22,565