Determine file type (UNIX)
file [-vczL] [-f namefile] [-m magicfile] file ...
The file utility tests each file argument in an attempt to classify it. There are three sets of tests, performed in this order:
The first test that succeeds causes the file type to be printed. The type printed usually contains one of these words:
When modifying the file /etc/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 /etc/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.
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.
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:
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.
You can obtain the original author's latest version by anonymous FTP on tesla.ee.cornell.edu in the directory /pub/file-X.YY.tar.gz.
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.