Вопрос

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' '{}'

Другие советы

rename -n 's/ \.(?=.*\.)//g' * : -n no action. To actually make changes remove -n.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top