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

新手上路,请多包涵

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

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

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

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

这给了我错误:

 $ 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>

这可行,但我需要指定一个 MIME-TYPE。

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

原文由 Mint 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 925
2 个回答

使用 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

原文由 bhups 发布,翻译遵循 CC BY-SA 3.0 许可协议

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

例如 xdg-mime query filetype <file>

如果你有好吃的,

yum install xdg-utils.noarch

Subrip(subtitles) 文件上 xdg-mime 和文件的示例比较

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

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

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

原文由 ghostdog74 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题