file

Determine file type (UNIX)

Syntax:

file [-bcEhikLlNnprsvzZ0] [--apple] [--extension]
     [--mime-encoding] [--mime-type]
     [-e testname] [-F separator] [-f namefile] [-m magicfiles]
     [-P param=value] file ...

file -C [-m magicfiles]
file [--help]

Runs on:

QNX Neutrino

Options:

-0
--print0
Terminate filenames with ASCII NUL.
--apple
Output the Apple CREATOR/TYPE.
-b
--brief
Don't echo the name of the file before its type.
-C
--compile
Compile the file specified by the -m option.
-c
--checking-printout
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.
-d
--debug
Print debugging messages.
-e test
--exclude test
exclude test from the list of tests to be performed for the file. Valid tests are:
  • apptype
  • ascii
  • cdf
  • compress
  • elf
  • encoding
  • soft
  • tar
  • text
  • tokens
--extension
Output a slash-separated list of extensions.
-F string
--separator string
Use string instead of a colon (:) as a separator.
-f namefile
--files-from 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.
-h
--no-dereference
Don't follow symlinks (the default)
--help
Display help information, and then exit.
-i
--mime
Output MIME type strings (the same as specifying --mime-type and --mime-encoding).
-k
--keep-going
Don't stop at the first match.
-L
--dereference
Cause symlinks to be followed, as the like-named option in ls.
-l
--list
List the magic strength.
-m list
--magic-file list
Specify a colon-separated list of files of magic numbers. The default file is /usr/share/misc/magic.
--mime-type
Output the MIME type.
--mime-encoding
Output the MIME encoding.
-N
--no-pad
Don't pad the output.
-n
--no-buffer
Don't buffer the output.
-p
--preserve-date
Preserve the access times on files.
-P param=value
--parameter param=value
Set file engine parameter limits:
param Default Description
indir 15 The recursion limit for indirection
name 30 Use limit for name/use magic
elf_notes 256 Maximum number of ELF notes to process
elf_phnum 128 Maximum number of ELF prog sections to process
elf_shnum 32768 Maximum number of ELF sections to process
-r
--raw
Don't translate unprintable chars to \ooo.
-s
--special-files
Treat special (block/char devices) files as ordinary ones.
-v
--version
Print the version of the program and exit.
-Z
--uncompress-noreport
Only print the contents of compressed files.
-z
--uncompress
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.