mimetype.
Determine file's MIME type
SYNOPSIS
mimetype [OPTIONS] FILE...
PARAMETERS
-b, --brief
Suppresses the filename output, showing only the MIME type.
-n, --no-newline
Prevents a trailing newline character in the output.
-h, --help
Displays a concise help message and exits.
-v, --version
Shows the command's version information and exits.
-o format, --output-format=format
Specifies a custom output format using placeholders like %m (MIME type) and %f (filename).
-c, --check-contents
Forces the command to check file contents for type detection, especially if extension lookup fails.
-a, --all-ext
When checking by extension, lists all possible MIME types associated with the extension, instead of stopping at the first match.
-l, --lookup-filesystem
Attempts to determine the MIME type based on filesystem information (e.g., for block devices, character devices).
-g, --guess-by-contents
Forces the command to guess the MIME type based solely on file contents, ignoring extensions.
-P, --no-pipe
Prevents reading from standard input if it's a pipe.
-r, --recursive
Recursively processes files within directories.
-s, --symlinks
Follows symbolic links when processing files.
-D, --dereference
Dereferences symbolic link arguments, acting on the target file.
-L, --mime-type-only
Outputs only the primary MIME type without any additional description.
DESCRIPTION
The mimetype command is a utility designed to identify the MIME (Multipurpose Internet Mail Extensions) type of a given file. Unlike simply relying on file extensions, mimetype typically analyzes the file's content, often by checking "magic numbers" — specific sequences of bytes at the beginning of a file that uniquely identify its format. This robust content-based identification is crucial for accurately determining a file's nature, especially when extensions are missing, incorrect, or misleading. Common MIME types include text/plain, image/jpeg, application/pdf, and video/mp4.
While many Linux systems primarily use the file command with the --mime-type (or -i) option for this purpose, a dedicated mimetype utility (often provided by packages like libfile-mimeinfo-perl) offers specific options tailored for MIME type detection and output formatting. Its accurate identification is vital for web servers (to send correct Content-Type headers), email clients, and desktop environments (for choosing appropriate applications to open files).
CAVEATS
The availability and specific behavior of the mimetype command can vary significantly across Linux distributions. Many systems use the file command with its -i or --mime-type option as the primary tool for MIME type detection. A standalone mimetype executable, if present, is often provided by a specific package (e.g., libfile-mimeinfo-perl) and may have a distinct set of features and options. Ensure you consult your system's documentation for the exact implementation.
CONTENT VS. EXTENSION-BASED DETECTION
Unlike simple file extension checks (e.g., .txt for text, .jpg for JPEG), mimetype primarily identifies file types by analyzing their internal content, particularly through "magic numbers" and structural patterns. This method is far more reliable as it ensures accurate identification even if a file has an incorrect or missing extension.
MAGIC FILES
The mimetype command, like the file command, relies on a database of "magic numbers" and patterns stored in configuration files (often located in /usr/share/file/magic/). These "magic files" contain rules that map specific byte sequences or file structures to their corresponding MIME types and human-readable descriptions. For accurate detection, these files must be up-to-date.
HISTORY
The concept of identifying file types by content originates from the Unix file command, which dates back to the early days of Unix. As the internet evolved and the need for standardized content negotiation became apparent, MIME (Multipurpose Internet Mail Extensions) types were introduced in the early 1990s, becoming critical for email and later for the World Wide Web. Dedicated mimetype utilities, while sometimes wrappers around file, emerged to provide a more focused interface for MIME type detection, often with more granular control over output and a stronger emphasis on MIME specifications. Their development parallels the increasing importance of structured data exchange and content identification in networked environments.