2010-02-09 51 views
72

有没有办法找到一个Linux bash脚本中的文件的MIME类型(或称为“Content-Type”...)?如何找出文件“mime-type(Content-Type?)”?

我之所以需要它是因为ImageShack的似乎需要它来上传文件由于某种原因,检测.png文件作为一个“应用程序/八位字节流” ......

心中已经检查的文件,它确实是一个巴纽

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

这给我的错误:

$ curl -F "[email protected]/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> 

这工作,但我需要指定一个MIME类型。

$ curl -F "[email protected]/1.png;type=image/png" http://www.imageshack.us/upload_api.php 

回答

143

使用file。例子:

> 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 
+0

干杯,“文件--mime-type /1.png”很好用! – Mint 2010-02-09 06:32:19

+0

如果您在Windows下使用Git bash,请不要使用它。 – tivo 2011-09-23 08:25:06

+1

只需**就可以执行mime类型:'file --mime-type FILE_NAME | awk'{print $ 2}'' – 2012-08-23 00:30:40

16

其他工具之一(除了文件),可以使用是xdg-mime

xdg-mime query filetype query filetype <file>

如果你有荫,

yum install xdg-utils.noarch

一个例子比较xdg-mime和Subrip(字幕)文件上的文件

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

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

在上述文件中只显示为纯文本。

+0

xdg-mime查询filetype install.sql; xprop:无法打开显示'' – 2012-10-09 12:42:06

+0

xdg-mime是一个bash脚本,很大程度上依赖于环境变量。其中一些例如如果您未登录会话,则DE未设置。亲自检查一下'$ less $(which xdg-mime)' – ManuelSchneid3r 2014-09-08 23:32:34

1

尝试file命令与-i选项。

-i选项使文件命令输出MIME类型字符串,而不是更传统的可读的字符串。因此可以说text/plain; charset=us-ascii而不是ASCII text

1

file --mime有效,但不是--mime-type。至少对于我的RHEL 5.

+0

这也适用于Linux Mint 12 – 2012-10-09 12:40:52

7

文件版本< 5:文件-i -b /路径/到/文件
文件版本> = 5:文件--mime型-b /路径/到/文件