Mint
Mint

Reputation: 15887

How can I find out a file's MIME type (Content-Type)?

Is there a way to find out the MIME type (or is it called "Content-Type"?) of a file in a Linux bash script?

The reason I need it is because ImageShack appears to need it to upload a file, as for some reason it detects the .png file as an application/octet-stream file.

I’ve checked the file, and it really is a PNG image:

$ cat /1.png 
?PNG
(with a heap load of random characters)

This gives me the error:

$ curl -F "fileupload=@/1.png" http://www.imageshack.us/upload_api.php
<links>
<error id="wrong_file_type">Wrong file type detected for file 1.png:application/octet-stream</error>
</links>

This works, but I need to specify a MIME-TYPE.

$ curl -F "fileupload=@/1.png;type=image/png" http://www.imageshack.us/upload_api.php

Upvotes: 189

Views: 197434

Answers (7)

robertspierre
robertspierre

Reputation: 4341

Notice that file and xdg-mime may give different results:

➜  file --mime-type paper.html
paper.html: text/html

➜  xdg-mime query filetype paper.html 
application/xhtml+xml

It looks like Dolphin (the KDE file manager) relies on the output of xdg-mime to show which software can open a file in the right context menu, not file.

This is important if you plan to write a custom .desktop file that is supposed to open a certain file type and want the entry to show up in Dolphin.


You can also exploit alternatives to xdg-utils like handlr or handlr-regex:

➜  handlr mime https://duckduckgo.com . README.md
┌─────────────────────────┬────────────────────────┐
│ path                    │ mime                   │
├─────────────────────────┼────────────────────────┤
│ https://duckduckgo.com/ │ x-scheme-handler/https │
│ .                       │ inode/directory        │
│ README.md               │ text/markdown          │
└─────────────────────────┴────────────────────────┘

Upvotes: 0

fanchyna
fanchyna

Reputation: 2713

file --mime works, but not file --mime-type, at least for my RHEL 5.

Upvotes: 2

foo
foo

Reputation: 2111

For detecting MIME-types, use the aptly named "mimetype" command.

It has a number of options for formatting the output, it even has an option for backward compatibility to "file".

But most of all, it accepts input not only as file, but also via stdin/pipe, so you can avoid temporary files when processing streams.

Upvotes: 1

ghostdog74
ghostdog74

Reputation: 342293

one of the other tool (besides file) you can use is xdg-mime

eg xdg-mime query filetype <file>

if you have yum,

yum install xdg-utils.noarch

An example comparison of xdg-mime and file on a Subrip(subtitles) file

$ xdg-mime query filetype subtitles.srt
application/x-subrip

$ file --mime-type subtitles.srt
subtitles.srt: text/plain

in the above file only show it as plain text.

Upvotes: 40

bhups
bhups

Reputation: 14875

Use file. Examples:

> file --mime-type image.png
image.png: image/png

> file -b --mime-type image.png
image/png

> file -i FILE_NAME
image.png: image/png; charset=binary

Upvotes: 371

Andrey
Andrey

Reputation: 317

file version < 5 : file -i -b /path/to/file
file version >=5 : file --mime-type -b /path/to/file

Upvotes: 13

codaddict
codaddict

Reputation: 454950

Try the file command with -i option.

-i option Causes the file command to output mime type strings rather than the more traditional human readable ones. Thus it may say text/plain; charset=us-ascii rather than ASCII text.

Upvotes: 7

Related Questions