LinuxCommandLibrary

file

Determine file type

TLDR

Give a description of the type of the specified file

$ file [path/to/file]
copy

Look inside a zipped file and determine the file type(s) inside
$ file [[-z|--uncompress]] [path/to/file.zip]
copy

Allow file to work with special or device files
$ file [[-s|--special-files]] [path/to/file]
copy

Don't stop at first file type match; keep going until the end of the file
$ file [[-k|--keep-going]] [path/to/file]
copy

Determine the MIME encoding type of a file
$ file [[-i|--mime]] [path/to/file]
copy

SYNOPSIS

file [options] [file ...]
file -C [-m magicfile] [file ...]
file --filesystems

PARAMETERS

-b, --brief
    Do not prepend filenames to output.

-c, --checking-printout
    Print debugging info on magic file checks.

-C, --compile
    Compile text magic files into binary format.

-e testname
    Test only specific magic file subtype.

-f namefile, --files-from=namefile
    Read filenames from namefile instead of args.

-F separator, --separator=separator
    Use custom separator between name and type.

-i, --mime-type
    Output MIME type or application/octet-stream.

-I, --mime-encoding
    Output MIME encoding of file.

-k, --keep-going
    Continue matching after first hit.

-L, --dereference
    Follow symbolic links.

-m magicfile
    Use specified magic file(s).

-N, --no-pad
    Do not pad output fields.

-n, --no-buffer
    Disable line buffering of output.

-p, --preserve-date
    Preserve access time on files.

-r
    Raw output without descriptive text.

-s, --special-handlers
    Defer to special handlers (tar, etc.).

-v, --version
    Print version information.

-z, --uncompress
    Try to decompress gzip/bzip2 files.

--filesystems
    Print known filesystem types.

--help
    Display usage summary.

DESCRIPTION

The file command is a powerful Unix utility that identifies the type of a given file by examining its header bytes, patterns, and content characteristics, rather than trusting the filename extension. It relies on a programmable magic database containing signatures for thousands of file formats, including executables, documents, images, archives, compressed files, and more.

Output typically includes the filename followed by a descriptive type, such as "file example.jpg" yielding "JPEG image data, JFIF standard 1.01". Useful for shell scripting, forensics, malware analysis, and automating workflows based on actual file types.

Key features include MIME type output (-i), brief mode (-b), handling compressed files (-z), and custom magic files (-m). It supports batch processing from lists (-f) and can compile magic databases (-C). Fast and read-only, it works on regular files, directories (listing contents), and devices.

Limitations include potential misclassifications for obfuscated or novel formats, but it's highly reliable for standard files.

CAVEATS

May misidentify obfuscated or rare files; accuracy depends on magic database currency. Does not recurse directories unless specified via -f. Large files are sampled, not fully read.

MAGIC DATABASE

Primary database at /usr/share/misc/magic.mgc (compiled) or magic (source). Customize by editing source and recompiling with file -C.

EXAMPLES

file *: Type all files in dir.
file -bi *: MIME types/encodings.
file -z archive.gz: Type inside gzip.

HISTORY

Originated in 1973 by Ian Darwin for file type identification at University of Calgary. Ported to BSD, enhanced with magic files. Maintained by Christos Zoulas since 1995; now at version 5.45+, integral to most Unix-like systems.

SEE ALSO

magic(5), mimetype(1), xdg-mime(1)

Copied to clipboard