I am trying to create a script that replaces all the "." occurences in a filename with "_". For example when I try to replace all the " " symbols I use this:
rename 'y/ /_/' '{}' file
# test 1.2.jpg -> test_1.2.jpg
Which works fine, but when I try to do it with the "." symbol the extension also changes:
rename 'y/./_/' '{}' file
# test 1.2.jpg -> test 1_2_jpg
How can I rename the file without changing the extension (when there is one)?
You can use a lookahead to replace all dots before the very last dot:
rename 's/\.(?=[^.]*\.)/_/g' '{}'
OR using negative lookahead:
rename 's/\.(?![^.]*$)/_/g' '{}'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With