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
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?
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