Remove file extension if part of file name script

KyleEricson
Valued Contributor II

I use OneDriveFB on Macs and somewhere there was a glitch where it added the file extension to the file name on a ton of files. I need help with a script to remove .pdf .jpeg .doc or any other extension and any spaces at the end or beginning of the file name if its part of the file name. Ideas on how to do this?

Read My Blog: https://www.ericsontech.com
10 REPLIES 10

mm2270
Legendary Contributor III

I'm a little confused. Generally speaking, almost all files have some type of file extension on them, even if it's normally hidden in the Finder. Are you sure these are extra extensions on top of what was there, or was it that it just made the extensions visible in Finder?

If I'm misunderstanding the issue here, can you post an example of a file that you think was affected by this that needs to be fixed?

c_archibald
Contributor II

I think you have "Show All Filename Extensions" in Finder Preferences > Advanced checked. Removing File Types .xxx name removes it's association with what program the OS is told to open it.

ryan_ball
Valued Contributor

Do you have a situation where you have files with names like this:
file1.txt.txt
file2.jpg.jpg
file3.png.png

And you want to convert them in masse to this:
file1.txt
file2.jpg
file3.png

????

KyleEricson
Valued Contributor II

@ryan.ball Yes.

Read My Blog: https://www.ericsontech.com

c_archibald
Contributor II

Might be able to make a sh file to check filenames & remove the dupe like

.jpg.jpg = .jpg
.pdf.pdf = .pdf
etc...

But I'm not sure where to start on that. Doing it by hand would be a PitA.

mm2270
Legendary Contributor III

Ok. Hmm. Are these files with duplicate extensions all in one place, like all on the Desktop, or can they be and are they anywhere? Do you need to run this in an automated fashion across a bunch of Macs, or is this isolated to only a small number of devices that you can run a local script on to fix?

KyleEricson
Valued Contributor II

it's anywhere in the users home folder. Yes, it needs to be automated.

Read My Blog: https://www.ericsontech.com

ryan_ball
Valued Contributor

Something like this:

#!/bin/bash

directory="/your/directory/here"

while read -r file; do
    if [[ $file =~ (.+).[^.]+.([^.]+)$ ]]; then
        mv "$file" "${file%.*.*}.${file##*.}"
    fi
done <<< "$(find "$directory" -depth 1)"

exit 0

Obviously try this on a test directory first.

mm2270
Legendary Contributor III

@ryan.ball There's an issue with your script. It's only looking to see if the file has more than one period in it, and assuming that it's a duplicate file extension if there is. That's not a safe assumption to make. If I have a file called "My.Great.File.txt" that isn't a duplicate extension. The OS doesn't stop us from naming files with periods in them, so it's totally possible to run across files like that.

Using your script it would rename my file above to My.Great.txt

ryan_ball
Valued Contributor

That is true. You'd probably want something to not only check to see if a file has more than one extension but also verify that both extensions are the same and write to a log the changes that it would be making.

This is a simple example and could be used as a baseline for something more thorough. This would be a good opportunity for someone to learn how to write a script as well.