The file command takes a list of one or more files and classifies each file on the basis of its contents. The types distinguished by file include directories, special files, FIFO files, archive libraries, C source programs, executable programs, shell scripts, and normal text files.
Caution!
file looks up the magic number in the magic file to identify the target file. If it fails to identify the file by this method, it tries various plausibility checks. Consequently the results are not always reliable.
Syntax
Format 1: | file[ -h][ -m magicfile][ -f ffile] argfile ... |
Format 2: | file[ -h][ -m magicfile] -f ffile[ argfile ...] |
Format 3: | file -c[ -m magicfile] |
Classify a file
(hidden) If argfile is a symbolic link, the link will not be followed and the following error message will be printed: file1
(magic) Causes file to use magicfile instead of the system file /etc/magic to identify the magic numbers of the files being classified.
file interprets the ffile argument as the name of a file which contains the names of all files to be examined. If this option is omitted, you must name at least one file to be classified.
Name of the file to be classified by the file command. If argfile is a symbolic link, file will follow the link and test the original file referenced by the symbolic link. You can name any number of files. If the -f option is omitted, you must name at least one. Check the magic file
(check) The magic file, by default the system file /etc/magic, is checked for format errors.
(magic file) Causes file to check the alternate magic file magicfile for format errors. |
Mode of operation
file performs a series of tests on each input file and attempts to classify it on the basis of its contents. If it appears to be a text file, file examines an initial segment (the first 512 bytes) and tries to guess the language it was created in. The accuracy of the guess cannot be guaranteed, however. If the input file is an executable program, it is identified as such, and further information is provided with respect to its contents. To do this, file searches the file for "magic numbers", i.e. for numeric constants or string constants that give an indication of the type of file. The file /etc/magic contains an explanation of these magic numbers. |
Output
file writes its file classification to standard output. In the following you find the output string and meaning of the most important file types that file classifies: ascii text: ASCII text file block special: Block special file c program text: C source program character special: Character special file commands text: Shell script cpio archive: Archive generated by cpio current ar archive: Archive library (see ar) data: Data file directory: Directory executable: Executable file (e.g. LLMs) empty file: Empty file fifo: FIFO file fortran program text: FORTRAN source program tar archive: Archive generated by tar, pax text: EBCDIC text file |
File
/etc/magic File containing a key to the magic numbers |
Locale
The following environment variables affect the execution of file: LANG Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined. LC_ALL If set to a non-empty string value, override the values of all the other internationalization variables. LC_CTYPE Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other. LC_MESSAGES Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error. NLSPATH Determine the location of message catalogs for the processing of LC_MESSAGES. |
Example
The file list contains the following file names: dir letter lib.a prog.c The following command line will yield information on the contents of each classified file type:
Thus dir is a directory, and letter contains normal ASCII text. lib.a is an archive library; prog.c contains a C program. The same output could also have been obtained with a command line reading:
|
See also
c89 [5] |