Search code examples
bashawkmime-typesgnu

Get mime-type of file extension


I am trying to make a script that given a file extension (such as 'opus'), it prints the corresponding mime-type ('audio/ogg'). This is so that I can find the mime-type of an inexistent file in the given system (for example, a remote file).

I did try using gawk on /etc/mime.types, but a am a beginner at gawk, and trying to hack with it is difficult to me.

But I do know that gawk is the best solution for this job. Or else, I want a GNU/Linux tool that already does this.

This is what I have:

#!/usr/bin/env bash

awk -F' ' '$0 ~ /'${1}'/' /etc/mime.types

It is made to be used as ./extension2mime opus

  1. I am having difficulties setting the field delimiter as tabs (because I believe tabs are used in /etc/mime.types between the mime-type and the file extension part).
  2. I want to match all the columns except the first column. And to print only the first column of the match (precisely the column that I am ignoring). This is done on the /etc/mime.types file, of course.

EDIT:

The result of ./extension2mime opus would be audio/ogg.

Here is an excerpt of /etc/mime.types

audio/mpeg                                      mp3 mpga mp1 mp2
audio/mpeg4-generic
audio/ogg                                       oga ogg opus spx
audio/opus
audio/parityfec
audio/PCMA

Solution

  • A simple approach would be the following:

    #!/bin/sh
    awk -v ext="$1" '            # pass shell variable to awk
    /^#/{next}                   # skip comment lines
    {type=$1;$1=""}              # store and strip first field
    $0~"\\<"ext"\\>"{print type} # print type of matching extensions
    ' /etc/mime.types
    

    Note that this interprets your argument as regular expression, so an input such as . or ^ would print all lines.

    The \<...\> regex is to match only full extensions, so that jpg won't match jpg2 or sjpg (this is a non-POSIX extension of GNU awk).

    Or the imperative approach, comparing all fields with your needle in a loop:

    awk -v ext="$1" '    # pass shell variable to awk
    !/^#/{               # skip comment lines
      for(i=2;i<=NF;++i) # loop fields
        if ($i==ext)     # compare extensions
          print $1       # print type
    }
    ' /etc/mime.types