문제

Is it possible to use rename to uppercase a file but exclude its extension?

ie: I want to rename the file foo_bar.ext to FOO_BAR.ext

I tried with rename 'y/a-z/A-Z/' foo_bar.ext, but the whole file (including extension) gets uppercased FOO_BAR.EXT

도움이 되었습니까?

해결책

You are asking rename to convert all the instances of [a-z] to [A-Z]. Instead, capture the desired string into a group and modify it:

rename 's/([^.]*)/\U$1/' foo_bar.ext

This would rename the file foo_bar.ext to FOO_BAR.ext.


If you have a file foo_bar.baz.ext that needs to be renamed to FOO_BAR.BAZ.ext, use greedy match and multiple groups. Saying:

rename 's/(.*)(\..*)/\U$1\E$2/' foo_bar.baz.ext

would rename the file foo_bar.baz.ext to FOO_BAR.BAZ.ext.

다른 팁

You can use positive lookahead:

rename 's/.+(?=\.)/\U$&/g' *

Example:

$ ls
input.foo.bar.txt

$ rename 's/.+(?=\.)/\U$&/g' *
$ ls
INPUT.FOO.BAR.txt
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top