I want to download a file using curl to a temporary directory with the appropriate extension.

Currently I do something like this:

tmp="$(mktemp -t 'curl')"
curl -o "$tmp" http://example.com/generate_image.php?id=44
file -b --mime-type "$tmp"

This will print the mime-type of the downloaded file, but how do I map these to extensions?

As you can see I can't just extract the "extension" of the url as that would give .php instead of .png.

I know there isn't a one-to-one map between mime-types and file extensions but it should handle normal file extensions.


Unlike windows, unix generally has no concept of file extensions. However you can use the /etc/mime.types file to provide those translations:

image/jpeg: jpg
image/gif: gif
image/png: png
image/x-portable-pixmap: ppm
image/tiff: tif

and then match by extension:

$ ext=$(grep "$(file -b --mime-type file.png)" /etc/mime.types | awk '{print $2}')

$ echo $ext
| improve this answer | |
  • I don't have that file, using OS X. – Tyilo Aug 11 '13 at 19:28
  • Actually I think /etc/apache2/mime.types is the same. – Tyilo Aug 11 '13 at 19:31
  • There is a good chance of it (it's how apache serves the mime type in the headers). There is also this answer on the subject: stackoverflow.com/questions/1735659/… – Drav Sloan Aug 11 '13 at 19:31
  • Be aware, that in some English "slangs" there are differences in writing some colors. I just recognized that file wrote image/x-portable-greymap, but in mime.types i found it as image/x-portable-graymap (grey vs. gray). – Marco Nov 15 at 11:24

Bash Script that takes one parameter - a filename, and attempts to rename it based on its mime-type using the file command and the system mime.types file:


# Set the location of your mime-types file here.  On some OS X installations,
# you may find such a file at /etc/apache2/mime.types; On some linux distros, 
# it can be found at /etc/mime.types

THIS_SCRIPT=`basename "$0"`
    # This fixes the case where the target file has no extension

if [ ! -f "$MIMETYPE_FILE" ]; then
    echo Could not find the mime.types file.  Please set the MIMETYPE_FILE variable in this script.
    exit 1

if [ "$TARGET_FILE" == "" ]; then
    echo "No file name given. Usage: ${THIS_SCRIPT} <filename>"
    exit 2

if [ ! -f "$TARGET_FILE" ]; then
    echo "Could not find specified file, $TARGET_FILE"
    exit 3

MIME=`file -b --mime-type $TARGET_FILE`
if [[ "${MIME}" == "" ]]; then
    echo ${THIS_SCRIPT} $TARGET_FILE - Could not find MIME-type.
    exit 4

EXT=$(grep "${MIME}" "${MIMETYPE_FILE}" | sed '/^#/ d' | grep -m 1 "${MIME}" | awk '{print $2}')

if [[ "$EXT" == "" ]]; then
    echo ${THIS_SCRIPT} ${TARGET_FILE} - Could not find extension for MIME-Type ${MIME}
    exit 5

if [ "${TARGET_FILE_EXTENSION}" == "${EXT}" ]; then
    echo ${TARGET_FILE} already has extension appropriate for MIME-Type ${MIME}
    exit 0

echo Renaming "${TARGET_FILE}" to "${TARGET_FILE}.${EXT}"
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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