LinuxCommandLibrary

mimetype

Determine file's MIME type

TLDR

Print the MIME type of a given file

$ mimetype [path/to/file]
copy

Display only the MIME type, and not the filename
$ mimetype --brief [path/to/file]
copy

Display a description of the MIME type
$ mimetype --describe [path/to/file]
copy

Determine the MIME type of stdin (does not check a filename)
$ [command] | mimetype --stdin
copy

Display debug information about how the MIME type was determined
$ mimetype --debug [path/to/file]
copy

Display all the possible MIME types of a given file in confidence order
$ mimetype --all [path/to/file]
copy

Explicitly specify the 2-letter language code of the output
$ mimetype --language [path/to/file]
copy

SYNOPSIS

mimetype [OPTION...] [FILE...]

PARAMETERS

-b, --brief
    Print only MIME type, omit filename

-d, --debug
    Show detailed detection process

-D, --dump
    Dump XML mime.cache to stdout

-e, --extension
    Print file extension for MIME type (implies -b)

-i, --stdin
    Read single file from stdin

-I, --magic-file FILE
    Use custom magic file

-l, --local
    Use only local (~/.local/share/mime) database

-o, --output-format=FORMAT
    Output in Nfo or XML format

--database=LOCATION
    Specify custom MIME database path

-h, --help
    Display help

-V, --version
    Show version info

DESCRIPTION

The mimetype command identifies the MIME type of files or input data using the shared MIME-info database from the FreeDesktop.org project.

It analyzes file extensions, contents via magic patterns, and XML rules from the database (/usr/share/mime/packages/ and user-local overrides).

By default, it outputs filename: type/subtype. Useful for scripts, desktop environments (GNOME, KDE), and applications needing file type detection without parsing extensions alone.

Handles multiple files, stdin input, and supports custom databases or formats like XML/Nfo. Detection prioritizes content over extension for accuracy.

Ensures cross-desktop consistency; requires shared-mime-info package. Not infallible—relies on maintained magic rules—but highly reliable for common formats like images, documents, archives.

CAVEATS

Detection accuracy depends on updated MIME database; may misidentify ambiguous files. No network access for remote files.

EXAMPLES

mimetype document.pdf → document.pdf: application/pdf
mimetype -b image.png → image/png
mimetype -e application/pdf → .pdf

DATABASE LOCATION

System: /usr/share/mime
User: ~/.local/share/mime
Update with update-mime-database

HISTORY

Part of shared-mime-info project (2003+), created by Havoc Pennington for FreeDesktop.org to standardize MIME handling in Linux desktops like GNOME/KDE.

SEE ALSO

Copied to clipboard