문제

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