file

Determine file type (UNIX)

Syntax:

file [-bcLnvz] [-f namefile] [-m magicfile] file ...

Runs on:

Neutrino

Options:

-b
Don't echo the name of the file before its type.
-c
Cause a checking printout of the parsed form of the magic file. This is usually used in conjunction with -m to debug a new magic file before installing it.
-f namefile
Read the names of the files to be examined from namefile (one per line) before the argument list. Either namefile or at least one filename argument must be present; to test the standard input, use - as a filename argument.
-L
Cause symlinks to be followed, as the like-named option in ls.
-m file
Specify an alternate file of magic numbers. The default is /usr/share/misc/magic.
-n
Echo the name of the file before its type (this is done by default).
-v
Print the version of the program and exit.
-z
Try to look inside compressed files.

Description:

The file utility tests each file argument in an attempt to classify it. There are three sets of tests, performed in this order:

  1. Filesystem tests
  2. Magic number tests
  3. Language tests

The first test that succeeds causes the file type to be printed. The type printed usually contains one of these words:

text
The file contains only ASCII characters and is probably safe to read on an ASCII terminal.
executable
The file contains the result of compiling a program in a form understandable to some UNIX kernel or another.
data
Meaning anything else (data is usually “binary” or nonprintable). Exceptions are well-known file formats (core files, tar archives) that are known to contain binary data.

Note: When modifying the file /usr/share/misc/magic or the program itself, preserve these keywords. People depend on knowing that all the readable files in a directory have the word text printed.

The filesystem tests are based on examining the return from a stat() system call. The program checks to see if the file is empty, or if it's some sort of special file. Any known file types appropriate to the system you're running on (sockets, symbolic links, or named pipes (FIFOs) on those systems that implement them) are intuited if they're defined in the system header file /usr/include/sys/stat.h.

The magic number tests are used to check for files with data in particular fixed formats. These files have a “magic number” stored in a particular place near the beginning of the file that tells the UNIX operating system that the file is a binary executable, and which of several types thereof. The concept of “magic number” has been applied by extension to data files. Any file with some invariant identifier at a small fixed offset into the file can usually be described in this way. The information in these files is read from the magic file /usr/share/misc/magic.

If an argument appears to be an ASCII file, file attempts to guess its language. The language tests look for particular strings that can appear anywhere in the first few blocks of a file. For example, the keyword .br indicates that the file is most likely a troff input file, just as the keyword struct indicates a C program. These tests are less reliable than the previous two groups, so they are performed last. The language test routines also test for some miscellany (such as tar archives) and determine whether an unknown file should be labeled as ascii text or data.

Files:

/usr/share/misc/magic
Default list of magic numbers.

Contributing author:

Written by Ian F. Darwin, UUCP address {utzoo|ihnp4}!darwin!ian, Internet address ian@sq.com, postal address: P.O. Box 603, Station F, Toronto, Ontario, CANADA M4Y 2L8.

Altered by Rob McMahon, cudcv@warwick.ac.uk, 1989, to extend the & operator from simple x&y != 0 to x&y op z.

Altered by Guy Harris, guy@auspex.com, 1993, to:

Changes by Ian Darwin and various authors including Christos Zoulas (christos@ee.cornell.edu), 1990-1992.

License:

Copyright © Ian F. Darwin, Toronto, Canada, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993.

This software is not subject to and may not be made subject to any license of the American Telephone and Telegraph Company, Sun Microsystems Inc., Digital Equipment Inc., Lotus Development Inc., the Regents of the University of California, The X Consortium or MIT, or The Free Software Foundation.

This software is not subject to any export provision of the United States Department of Commerce, and may be exported to any country or planet.

Permission is granted to anyone to use this software for any purpose on any computer system, and to alter it and redistribute it freely, subject to the following restrictions:

  1. The author is not responsible for the consequences of use of this software, no matter how awful, even if they arise from flaws in it.
  2. The origin of this software must not be misrepresented, either by explicit claim or by omission. Since few users ever read sources, credits must appear in the documentation.
  3. Altered versions must be plainly marked as such, and must not be misrepresented as being the original software. Since few users ever read sources, credits must appear in the documentation.
  4. This notice may not be removed or altered.

A few support files (getopt(), strtok()) distributed with this package are by Henry Spencer and are subject to the same terms as above. A few simple support files (strtol(), strchr()) distributed with this package are in the public domain; they are so marked.

The files tar.h and is_tar.c were written by John Gilmore from his public-domain tar program, and are not covered by the above restrictions.

Caveats:

The file utility uses several algorithms that favor speed over accuracy, thus it can be misled about the contents of ASCII files. The support for ASCII files (primarily for programming languages) is simplistic, inefficient and requires recompilation to update.

See also:

/usr/share/misc/magic