This question already has an answer here:

I'm hoping someone can explain to me how i can change a file type. I recently uploaded my pictures onto my laptop and half of them are saved as a file and not an image meaning i cannot upload them onto websites for printing.

Im looking for step by step process and also if i can do them all at the same time or have they to be done individually. I have 500+ to change.

marked as duplicate by Eric Carvalho, v2r, Evandro Silva, BuZZ-dEE, Fabby Jan 22 '15 at 1:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

You should be able to use the mimetype utility - from man mimetype:

NAME
       mimetype - Determine file type

SYNOPSIS
       mimetype [options] [-] files

DESCRIPTION
       This script tries to determine the mime type of a file using the Shared
       MIME-info database. It is intended as a kind of file(1) work-alike, but
       uses mimetypes instead of descriptions.

So for example:

$ mimetype somefile
somefile: image/jpeg

However, by default, mimetype will "believe" the extension if present - so:

$ cp somefile somefile.gif
$ mimetype somefile.gif
somefile.gif: image/gif

You can tell it to use only the file's magic bytes to do the determination by adding the -M switch:

$ mimetype -M somefile.gif
somefile.gif: image/jpeg

and you can add -b to give a brief description:

$ mimetype -bM somefile.gif
image/jpeg

If you wanted to script the renaming, I'd suggest something like this:

#!/bin/bash

while read -r -d '' f; do
  mt="$(mimetype -bM "$f")"
  ext="${mt##*/}"
  case "$ext" in
    jpeg|gif|png)
      echo mv -v "$f" "$f.$ext"
      ;;
    *)
    echo "skipping mimetype $mt... "
      ;;
  esac
done < <(find -type f -print0)

NOTES:

  1. it doesn't actually rename anything as written: the echo mv just outputs what it would do - please check and double check that it is behaving as expected on your files before removing the echo
  2. it only operates on jpeg,gif, and png files: if there are other image types, you will need to add those explicitly
  3. it will rename (add an additional extension to) any files it considers to have the wrong extension, based on magic bytes

There may be other - easier - options using tools like exiftool

Not the answer you're looking for? Browse other questions tagged or ask your own question.