Rename Files Without Extension

I have an issue where I have recovered from a disk failure and now my image files have no extension. I would like to know how to rename the files with the correct extensions.


the last time I had this issue, I think it was @vikingOSX last time wrote a script... however the script does not run.


can anyone help...


Regards, Paul

Posted on May 21, 2020 11:23 AM

Reply
Question marked as Top-ranking reply

Posted on May 22, 2020 7:33 AM

Hi Paul,


The original post is here — for image validation on files with extensions.


The original script that I wrote last Fall was used to validate files with extensions to their actual image type. It won't work with files without extensions, and neither will the UNIX file command, or even Spotlight's mdls utility that thinks image files without extensions are just public.data.


There are no tools available in macOS that can explore the internal image data, and arrive at the accurate extension information — except the third-party exiftool which you indicated was installed last Fall. Am I correct that your current images without extensions are only camera RAW images that may be RAF, CR2, or DNG image types?


This morning, using the same image data as last Fall, I wrote and tested a very short (8 lines) Zsh script that recursively retrieves all subordinate images of a parent directory and uses exiftool to peek inside them for their FileType. This is also the uppercase extension from the first paragraph that should be assigned to the raw image, and I use that in the renaming. When tested against the same images as I used last Fall for your other solution, the renaming was 100% accurate using known raw files.


You can run this script from the Terminal after making it executable. I have a Test4 directory on my Desktop. Do not use tilde path shorthand, as Zsh doesn't expand tilde without additional help:


cd Desktop
chmod +x addext.zsh
./addext.zsh ./Test4
Script has completed.


Copy and paste the following into BBEdit (which you also installed last Fall), and then save on your Desktop as addext.zsh.


#!/bin/zsh

: <<'COMMENT'

Recursively drill into directory hierarchy for image files with no
extension. Exclude text files. Use exiftool to inspect the image
and extract FileType (extension), then rename with that extension.

Requires third-party exiftool installation.

Usage: addext.zsh /path/to/parent/directory

VikingOSX, 2020-05-22, Apple Support Communities, No warranties of any kind.

COMMENT

STARTDIR="$1:a"
setopt extended_glob
for f in ${STARTDIR}/**/*~*.txt(.N);
do
    # print "${f}"
    EXT=$(/usr/local/bin/exiftool -s3 "-FileType" "${f}")
    mv "${f}" "${f}.${EXT}"
done
echo "Script has completed."


Before:


After:




Similar questions

46 replies

May 23, 2020 11:42 AM in response to Paul1762

Double-check that exiftool is actually in /usr/local/bin. I have no lib, or lib/image underneath /usr/local/bin, and homebrew places exiftool in /usr/local/bin.


I have made some changes in the Zsh script to use a different field (FileTypeExtension) from the exif data, and automatically perform an array lookup to set the extension case depending on RAW or non-raw image type. This should eliminate trailing dots on filenames. It did in my testing this afternoon, where .jpg and .tif were being dropped.


Replace the entire contents of your Run Shell Script with this new script. Verify that the correct path to exiftool is in the script.


#!/bin/zsh

: <<'COMMENT'

Recursively drill into directory hierarchy for image files with no
extension. Exclude text files. Use exiftool to inspect the image
and extract FileType (extension), then rename with that extension.

Requires third-party exiftool installation.

Usage: addext.zsh /path/to/parent/directory

VikingOSX, 2020-05-22, Apple Support Communities, No warranties of any kind.

COMMENT

# replacement strings for exiftool FileTypeExtensions. Deliberate duplicates.
typeset -A extList=(jpeg jpg jpg jpg tiff tif tif tif cr2 CR2 dng DNG raf RAF)

STARTDIR="$1:a"
setopt extended_glob
for f in ${STARTDIR}/**/*~*.txt(.N);
do
    # print "${f}"
    EXT=$(/usr/local/bin/exiftool -s3 "-FileTypeExtension" "${f}")
    # skip those files that already have an extension
    [[ ${f:e} ]] && continue
    # convert 3 or 4 character extensions to appropriate 3 character extension
    # RAW file extensions will be uppercase
    mv "${f}" "${f}.${extList[$EXT]}"
done
echo "Script has completed."





This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Rename Files Without Extension

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.