LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-16-2007, 10:02 AM   #1
haydenyoung
LQ Newbie
 
Registered: Nov 2004
Location: Perth, WA, Australia
Distribution: Ubuntu Dapper Drake, CentOS4
Posts: 13

Rep: Reputation: 0
Retrieving mime types in shell script


I currently have a script that uses the file binary (/usr/bin/file) to retrieve the mime type of a particular video file but in many cases I end up with application/octet-stream. However, if I right click on the file in Nautilus and bring up the Properties dialog I always get the correct mime type so I'm guessing Nautilus is using gnomevfs-info to get this information.

Running gnomevfs-info -s /path/to/video yields me the correct results; e.g. application/x-flash-video (note the -s slow mime flag) but I've got to run my script from a server with no desktop manager installed so running gnomevfs-info is not the ideal solution (although I will go with it if I have to).

Is there any way I can use a more generic command like /usr/bin/file but which will give me more accurate results or else can I update /usr/bin/file's mime db to fix the problem?
 
Old 03-16-2007, 12:42 PM   #2
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
gnome uses the gnome-mime-database, kde has it's own mime-types and any decent filer is going to have some sort of system, either based on one of the above or with its' own database or some system for handling different file types using 'file' or otherwise.
You can use shell or awk to compare the files to whichever mime database you like.
 
Old 03-16-2007, 01:07 PM   #3
haydenyoung
LQ Newbie
 
Registered: Nov 2004
Location: Perth, WA, Australia
Distribution: Ubuntu Dapper Drake, CentOS4
Posts: 13

Original Poster
Rep: Reputation: 0
hi gnashley

Thanks for your reply. I admit I'm clueless about the intricacies of mime type detection and so have been fumbling for a solution for a little while now.

I'm eventual solution has been to go with gnomevfs-info as I know what platforms I will be working on and so am pretty sure the correct commmand will be installed. The only issue here is that while ubuntu requires the --slow-mime flag, CentOS' version of gnomevfs-info doesn't recognize the flag at all. No big deal, I just keep trying to detect the mime type until all my options are exhausted.

Using a combination of gnomevfs-info and awk I retrieve the mime type with the following;

Code:
gnomevfs-info -s /tmp/video | awk '{FS=":"} /MIME type/ {gsub(/^[ \t]+|[ \t]+$/, "",$2); print $2}'
Not sure if my awk is completely correct but seems to work okay.

Last edited by haydenyoung; 03-16-2007 at 01:34 PM.
 
Old 03-21-2007, 03:09 AM   #4
haydenyoung
LQ Newbie
 
Registered: Nov 2004
Location: Perth, WA, Australia
Distribution: Ubuntu Dapper Drake, CentOS4
Posts: 13

Original Poster
Rep: Reputation: 0
My content type detection woes continue although I'm interested to know more.

While I've managed to detect the content types of video files perfectly with gnomevfs-info I've started running into issues with office documents such as pdf, odt, etc. When I run gnomevfs-info --slow-mime or file -i on files without an extension (in the case of file -i with an extension) I get the following;

odt = application/x-zip (understandable but if you open the file in vim you'll see mimetypeapplication/vnd.oasis.opendocument.text in the header of the file)
msword = application/x-ole-storage or nothing in the case of file -i

However, if I run file without the -i flag I get the correct file type but in human readable form;
odt = OpenDocument Text
msword = Microsoft Office Document

I've grep-ed for these values but cannot find them so I'm wondering how it's able to determine this information and why I get the correct human readable value but the incorrect mime types (e.g. application/msword, application/vnd.oasis.opendocument.text).
 
Old 03-21-2007, 05:17 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I may be barking up the wrong tree, but
are you aware that Open Office files are zipped up tar files?
 
Old 03-21-2007, 10:39 PM   #6
haydenyoung
LQ Newbie
 
Registered: Nov 2004
Location: Perth, WA, Australia
Distribution: Ubuntu Dapper Drake, CentOS4
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by bigearsbilly
I may be barking up the wrong tree, but
are you aware that Open Office files are zipped up tar files?
Hi bigearsbilly

Yes I am but they are files that are still of type vnd.oasis.opendocument.text.

I probably posted my previous message a little too hastily as upon reading the file man page (I know, I should have done this to start with) I notice that file on it's own uses the /usr/share/file/magic file to (correctly) determine the file type in human readable form (i.e. an odt file is detected as "OpenDocument Text", pdf as "PDF document, version ...", etc). However, when I specify the -i or --mime flag I receive a mime type that does not correspond to the entry in /usr/share/file/magic, that is vnd.oasis.opendocument.text. Rather I get application/x-zip.

I guess what my question should be is;

using the file command why am I getting the incorrect mime type (e.g. OpenDocument Text = application/x-zip) of a correctly determined human readable file type and how do I get the correct mime type (e.g. OpenDocument Text = vnd.oasis.opendocument.text)?

Last edited by haydenyoung; 03-21-2007 at 10:41 PM.
 
  


Reply

Tags
mime, script, shell, types



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cups, mime.types and mr.wobble Linux - Networking 2 05-29-2006 10:44 PM
how to set mime-types? gaah Linux - Newbie 1 10-15-2005 10:23 PM
mime types Nightfrost Linux - General 2 10-01-2004 05:11 AM
Gnome 1.4 and mime types kly546 Debian 0 07-25-2003 02:04 AM
MIME Types? JoeLinux Linux - Software 6 09-14-2002 04:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration