[英]How can I find out a file's MIME type (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
使用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
您可以使用的其他工具之一(除了文件)是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
在上面的文件中只顯示為純文本。
文件版本 < 5:文件 -i -b /path/to/file
文件版本> = 5:文件--mime-type -b /path/to/file
嘗試帶有-i
選項的file
命令。
-i
選項 使文件命令輸出 mime 類型的字符串,而不是更傳統的人類可讀的字符串。 因此它可能會說text/plain; charset=us-ascii
text/plain; charset=us-ascii
而不是ASCII text
。
文件 --mime 有效,但 --mime-type 無效。 至少對於我的 RHEL 5。
要檢測 MIME 類型,請使用恰當命名的“ mimetype ”命令。
它有許多用於格式化輸出的選項,甚至還有一個向后兼容“文件”的選項。
但最重要的是,它不僅接受文件輸入,還接受標准輸入/管道輸入,因此在處理流時可以避免使用臨時文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.