The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group


file - determine file type


file file ...


The file utility performs a series of tests on each specified file in an attempt to classify it:

  1. If the file is not a regular file, its file type is identified. The file types directory, FIFO, block special and character special are identified as such. Other implementation-dependent file types may also be identified.

  2. If the file is a regular file, and:

    1. The file is zero-length, it is identified as an empty file.

    2. The file is not zero-length, file will examine an initial segment of the file and make a guess at identifying its contents or whether it is an executable binary file. (The answer is not guaranteed to be correct.)

If file does not exist, cannot be read, or its file status could not be determined, the output will indicate that the file was processed, but that its type could not be determined.




The following operand is supported:
A pathname of a file to be tested.


Not used.


The file can be any file type.


The following environment variables affect the execution of file:
Provide a default value for the internationalisation variables that are unset or null. If LANG is unset or null, the corresponding value from the implementation-dependent default locale will be used. If any of the internationalisation variables contains an invalid setting, the utility will behave as if none of the variables had been defined.
If set to a non-empty string value, override the values of all the other internationalisation variables.
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).
Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error and informative messages written to standard output.
Determine the location of message catalogues for the processing of LC_MESSAGES .




In the POSIX locale, the following format is used to identify each operand, file specified:

"%s: %s\n", <file>, <type>

The values for <type> are unspecified, except that in the POSIX locale, if file is identified as one of the types listed in the following table, <type> will contain (but is not limited to) the corresponding string. Each space shown in the strings is exactly one space character.

If file is a: <type> Contains the String:
directory directory
FIFO fifo
block special block special
character special character special
executable binary executable
empty regular file empty
ar archive library (see ar) archive
extended cpio format (see pax) cpio archive
extended tar format (see ustar - see pax) tar archive
shell script commands text
C-language source c program text
FORTRAN source fortran program text
 Table: File Utility Output Strings

If the file named by the file operand does not exist, cannot be read or the status of the file cannot be determined, the string cannot open will be included as part of the <type> field, but this is not considered an error that affects the exit status.


Used only for diagnostic messages.






The following exit values are returned:
Successful completion.
An error occurred.




The file utility can only be required to guess at many of the file types because only exhaustive testing can determine some types with certitude. For example, binary data on some systems might match the initial segment of an executable or a tar archive.

Note that the table indicates that the output contains the stated string. Systems may add text before or after the string. For executables, as an example, the machine architecture and various facts about how the file was link-edited may be included.


Determine if an argument is a binary executable file:

file "$1" | grep -Fq executable &&
    printf "%s is executable.\n" "$1"





UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]