如何找出文件的MIME类型(Content-Type)?

Min*_*int 91 linux bash mime content-type mime-types

有没有办法找出Linux bash脚本中的文件的MIME类型(或称为"Content-Type"?)?

我需要它的原因是因为ImageShack似乎需要它来上传文件,因为它将某个.png文件检测为application/octet-stream文件.

我检查了文件,它确实是一个PNG图像:

$ cat /1.png 
?PNG
(with a heap load of random characters)
Run Code Online (Sandbox Code Playgroud)

这给了我错误:

$ 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>
Run Code Online (Sandbox Code Playgroud)

这有效,但我需要指定MIME-TYPE.

$ curl -F "fileupload=@/1.png;type=image/png" http://www.imageshack.us/upload_api.php
Run Code Online (Sandbox Code Playgroud)

bhu*_*ups 191

使用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
Run Code Online (Sandbox Code Playgroud)

  • @JustinJenkins -b省略了文件名,因此`file -b --mime-type FILE_NAME`只返回mime类型 (16认同)
  • 要获取**只是** mime 类型,您可以执行以下操作:`file --mime-type FILE_NAME | awk '{print $2}'` (2认同)

gho*_*g74 21

您可以使用的另一个工具(除了文件)之一 xdg-mime

例如 xdg-mime query filetype <file>

如果你有百胜,

yum install xdg-utils.noarch

在Subrip(字幕)文件上xdg-mime和文件的示例比较

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

$ file --mime-type subtitles.srt
subtitles.srt: text/plain
Run Code Online (Sandbox Code Playgroud)

在上面的文件中只显示为纯文本.


And*_*rey 9

文件版本<5:文件-i -b/path/to/file
file version> = 5: file --mime-type -b/path/to/file

  • @user2867106我认为他的意思是文件命令的版本。 (3认同)
  • 你说的文件版本是什么意思? (2认同)

cod*_*ict 7

尝试file使用带有选项的命令-i

-ioption 使 file 命令输出 mime 类型字符串,而不是更传统的人类可读字符串。因此它可能会说text/plain; charset=us-ascii而不是ASCII text