Monday, January 25, 2010

Find mime-type for a file

Here is how to determine the mime-type for a given file on the command line.

OS X

file --mime '''filename''

Linux

xdg-mime query filetype '''filename'''
gvfs-info -a standard::content-type '''filename''' | grep content | cut -d: -f4 | cut -d\  -f2-
gnomevfs-info '''filename''' | grep MIME | cut -d\: -f2- | cut -d\  -f2-
file --mime '''filename''' | cut -d\: -f2- | cut -d\  -f2

Everywhere

Update: We can modify the conditional alias to support mime types as well:

conditional_alias2 () {
  UNSET=false
  alias $1 > /dev/null 2>&1 || UNSET=true
  [ $UNSET = true ] && which $2 > /dev/null 2>&1 && alias $1=$3
}
mime_gvfs() {
  gvfs-info -a standard::content-type $1 | grep content | cut -d: -f4 | cut -d\  -f2-
}
mime_xdg() {
 xdg-mime query filetype $1
}
mime_gnomevfs() {
 gnomevfs-info file://$PWD/$1 | grep MIME | cut -d\: -f2- | cut -d\  -f2-
}
conditional_alias2 mime gvfs-info mime_gvfs
conditional_alias2 mime xdg-mime mime_xdg
conditional_alias2 mime gnomevfs-info mime_gnomevfs

1 comment: