4

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.

6

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
png
  • 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
0

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:

#!/bin/bash

# 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
MIMETYPE_FILE="/etc/apache2/mime.types"

THIS_SCRIPT=`basename "$0"`
TARGET_FILE="$1"
TARGET_FILE_BASE=$(basename "$TARGET_FILE")
TARGET_FILE_EXTENSION="${TARGET_FILE_BASE##*.}"
if [[ "$TARGET_FILE_BASE" == "$TARGET_FILE_EXTENSION" ]]; then
    # This fixes the case where the target file has no extension
    TARGET_FILE_EXTENSION=''
fi
TARGET_FILE_NAME="${TARGET_FILE_BASE%.*}"


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

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

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

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

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
fi


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

echo Renaming "${TARGET_FILE}" to "${TARGET_FILE}.${EXT}"
mv "${TARGET_FILE}" "${TARGET_FILE}.${EXT}"

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.