Portecochon
Portecochon

Reputation: 45

Rename multiple files with different extensions on linux

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

Upvotes: 1

Views: 631

Answers (2)

unutbu
unutbu

Reputation: 879103

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.

Upvotes: 2

fedorqui
fedorqui

Reputation: 289495

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.

Upvotes: 2

Related Questions