15

I am familiar with rename but I was curious does rename still apply for removing duplicate extensions??

Say I have a few files named:

  • picture2.jpg.jpg
  • picture9.jpg.jpg
  • picture3.jpg.jpg
  • picture6.jpg.jpg

How would you remove the the duplicate extension??

End result:

  • picture2.jpg
  • picture9.jpg
  • picture3.jpg
  • picture6.jpg
DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127

3 Answers3

20

Assuming:

  • You only want to perform this in the current working directory (non-recursively)
  • The double extensions have format precisely as .jpg.jpg:

Then the following script will work:

#!/bin/bash

for file in *.jpg.jpg
do
    mv "${file}" "${file%.jpg}"
done

Explanation:

To use this script:

  • Create a new file called clean_de.sh in that directory
  • Set it to executable by chmod +x clean_de.sh
  • Then run it by ./clean_de.sh

A Note of Warning:

As @gniourf_gniourf have pointed out, use the -n option if your mv supports it.

Otherwise - if you have a.jpg and a.jpg.jpg in the same directory, it will rename a.jpg.jpg to a.jpg and in the process override the already existing a.jpg without warning.

sampson-chen
  • 45,805
  • 12
  • 84
  • 81
5

One line rename command should also suffice (for your case at least):

rename 's/\.jpg\.jpg$/.jpg/' *.jpg.jpg
anishsane
  • 20,270
  • 5
  • 40
  • 73
1

Here is a more general, but still easy solution for this problem:

for oldName in `find . -name "*.*.*"`; do newName=`echo $oldName | rev | cut -f2- -d'.' | rev`; mv $oldName $newName; done

Short explanation:
find . -name "*.*.* - this will find only the files with duplicate extensions recursively

echo $oldName | rev | cut -f2- -d'.' | rev - the trick happens here: the rev command do a reverse on the string, so you now you can see, that you want the whole filename from the first dot. (gpj.gpj.fdsa)

mv $oldName $newName - to actually rename the files

Release Notes: since it is a simple one-line script, you can find unhandled cases. Files with an extra dot in the filename, super-deep directory structures, etc.

Gergely Bacso
  • 14,243
  • 2
  • 44
  • 64
  • This will clobber a similar-named non-double-suffix file. If the directory contains image.jpg.php and image.jpg.jpg, the first file will be deleted. – Lucent May 05 '21 at 17:15