LinuxCommandLibrary

file

Determine file type

TLDR

Give a description of the type of the specified file. Works fine for files with no file extension

$ file [path/to/file]
copy

Look inside a zipped file and determine the file type(s) inside
$ file [[-z|--uncompress]] [foo.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] name...

PARAMETERS

-b
    Brief mode; do not prepend filenames to output lines.

-c
    Cause file to print the parsed form of the magic file. It is usually used in conjunction with the -m flag to debug a new or modified magic file.

-d
    Print internal debugging information to standard error.

-f namefile
    Read the names of the files to be examined from namefile (one file name per line).

-i
    (or --mime) 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".

-L
    Options to follow symbolic links.

-m file
    Specify a list of colon-separated magic files to use. This can be a single file, or multiple files.

-N
    Don't pad output filenames.

-z
    Try to look inside compressed files.

DESCRIPTION

The file command is a standard Unix utility that tests each argument in an attempt to classify it. These tests include examining the filesystem, performing magic number tests, and language tests. The file command utilizes a database, typically /etc/magic or /usr/share/misc/magic, containing patterns and descriptions to identify file types.

It can differentiate between various text formats, executables, archives, image formats, and more. Its output usually includes a concise description of the identified file type. file is valuable for scripts or users who need to programmatically or quickly understand the format of a file without relying solely on its extension, which can be misleading or absent.

CAVEATS

The accuracy of the file command depends heavily on the completeness and correctness of the magic file it uses. If the magic file is outdated or does not contain patterns for specific file types, the command may misidentify files.

MAGIC FILE FORMAT

The file command relies on "magic numbers" located at the beginning of the file to identify the type. These are specific byte sequences that are indicative of a file format. The magic file contains these byte sequences along with descriptions of their associated file types. Understanding the format of the magic file allows to add your own rules to recognize new file types.

EXIT STATUS

The file command exits with 0 if successful or >0 if an error occurred.

HISTORY

The file command has been a part of Unix systems since early versions. It was originally designed to provide a reliable way to determine file types, as relying solely on file extensions was deemed insufficient. Over time, the magic file format has been refined and extended to support a wider range of file types and formats. The command continues to be maintained and updated to recognize modern file formats.

SEE ALSO

magic(5)

Copied to clipboard