Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

(macOS terminal) Replacing multiple dots in filename with underscore

I have thousands of jpg files like

juba.cor.top.jpg
juba.jpg
juga.caaa.wadea.dfda.jpg
juga.caaa.wadea.dfda.jpg
juga.caaa.wadea.dfda.afasfs.jpg
juga.caaa.wadea.dfda.aefaf.afdssaf.afdasfs.jpg

I want to find those files which have multiple dots in the name. In the previous list, I want to match all except juba.jpg and replace these dots with underscores.

I would like to convert the previous list into

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

juba_cor_top.jpg
juba.jpg
juga_caaa_wadea_dfda.jpg
juga_caaa_wadea_dfda.jpg
juga_caaa_wadea_dfda.afasfs.jpg
juga_caaa_wadea_dfda_aefaf_afdssaf_afdasfs.jpg

I have discovered that the following regex matches the files I want:

find -E . -regex '(.*\..*){2,}\.jpg'

Now, how do I rename them?

>Solution :

Get the part of the name before .jpg, and replace all the . with _ in it.

for file in *.*.jpg
do
    base=${file%.*} # remove jpg suffix
    base=${base//./_} # replace all . with _
    mv "$file" "$base.jpg"
done
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading