1

I have a bunch of files with different extensions and i would like to add a suffix at the end of all of their names:

Fe2-K-D4.rac
Fe2-K-D4.plo
Fe2-K-D4_iso.xy
...

to

Fe2-K-D4-4cc8.rac
Fe2-K-D4-4cc8.plo
Fe2-K-D4-4cc8_iso.xy
...

I read a few posts about changing the extension using the rename tool but i don't know how to change the name and keep the same extension (I'm a recent linus user).

Thanks for any help

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Portecochon
  • 45
  • 1
  • 3

2 Answers2

2

Using Extract filename and extension in Bash, I would say:

for file in *
do
  extension="${file##*.}"
  filename="${file%.*}"
  mv "$file" "${filename}-4cc8.${extension}"
done

This loops through all the files, gets its name and extension and then moves it (that is, renames it) to the given name with an extra -4cc8 value before the extension.

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
1

Using rename:

rename 's/[.]([^.]+)$/-4cc8.$1/' *

s/[.]([^.]+)$/-4cc8.$1/ is a perl expression of the form s/PATTERN/REPLACEMENT/ which tells rename to do a global substition.

[.]([^.]+)$ is a regex pattern with the following meaning:

[.]     match a literal period
(       followed by a group
  [     containing a character class
    ^.  composed of anything except a literal period  
  ]+    match 1-or-more characters from the character class
)       end group
$       match the end of the string.

The replacement pattern, -4cc8.$1, tells rename to replace the matched text with a literal -4cc8. followed by text in the first group matched, i.e. whatever followed the literal period.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677