[linux] 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

This question is related to linux bash content-type mime mime-types

The answer is


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.


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


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.


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


Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to content-type

Passing headers with axios POST request Spring MVC 4: "application/json" Content Type is not being set correctly What are all the possible values for HTTP "Content-Type" header? New lines (\r\n) are not working in email body HTML Input="file" Accept Attribute File Type (CSV) How do you set the Content-Type header for an HttpClient request? Utility of HTTP header "Content-Type: application/force-download" for mobile? Set Content-Type to application/json in jsp file Cannot set content-type to 'application/json' in jQuery.ajax Difference between application/x-javascript and text/javascript content types

Examples related to mime

Resource blocked due to MIME type mismatch (X-Content-Type-Options: nosniff) Nginx fails to load css files Which MIME type to use for a binary file that's specific to my program? embedding image in html email How to set MimeBodyPart ContentType to "text/html"? Mail multipart/alternative vs multipart/mixed Proper MIME type for OTF fonts How can I find out a file's MIME type (Content-Type)? Detect when browser receives file download Get MIME type from filename extension

Examples related to mime-types

Stylesheet not loaded because of MIME-type Is the MIME type 'image/jpg' the same as 'image/jpeg'? Proper MIME type for .woff2 fonts How to check file MIME type with javascript before upload? Correct MIME Type for favicon.ico? Right mime type for SVG images with fonts embedded Which mime type should I use for mp3 Correct mime type for .mp4 What does "Content-type: application/json; charset=utf-8" really mean? Add MIME mapping in web.config for IIS Express