Rename everything. to '_' in the file name, with the exception of the extension

I am trying to create a script that replaces everything with a ".". occurrences in the file name using "_". For example, when I try to replace all "characters", I use this:

rename 'y/ /_/' '{}' file # test 1.2.jpg -> test_1.2.jpg 

This works fine, but when I try to do it with "." The character also adds an extension:

 rename 'y/./_/' '{}' file # test 1.2.jpg -> test 1_2_jpg 

How can I rename a file without changing the extension (if any)?

+6
source share
2 answers

You can use lookahead to replace all points to the very last point:

 rename 's/\.(?=[^.]*\.)/_/g' '{}' 

OR using a negative scan:

 rename 's/\.(?![^.]*$)/_/g' '{}' 
+4
source

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

0
source

All Articles