file [−dh] [−M file] [−m file] file...
file −i [−h] file...
- If file does not exist, cannot be read, or its file status could not be determined, the output shall indicate that the file was processed, but that its type could not be determined.
- If the file is not a regular file, its file type shall be identified. The file types directory, FIFO, socket, block special, and character special shall be identified as such. Other implementation-defined file types may also be identified. If file is a symbolic link, by default the link shall be resolved and file shall test the type of file referenced by the symbolic link. (See the −h and −i options below.)
- If the length of file is zero, it shall be identified as an empty file.
- The file utility shall examine an initial segment of file and shall make a guess at identifying its contents based on position-sensitive tests. (The answer is not guaranteed to be correct; see the −d, −M, and −m options below.)
- The file utility shall examine file and make a guess at identifying its contents based on context-sensitive default system tests. (The answer is not guaranteed to be correct.)
- The file shall be identified as a data file.
- Apply any position-sensitive default system tests and context-sensitive default system tests to the file. This is the default if no −M or −m option is specified.
- When a symbolic link is encountered, identify the file as a symbolic link. If −h is not specified and file is a symbolic link that refers to a nonexistent file, file shall identify the file as a symbolic link, as if −h had been specified.
- If a file is a regular file, do not attempt to classify the type of the file further, but identify the file as specified in the STDOUT section.
- −M file
- Specify the name of a file containing position-sensitive tests that shall be applied to a file in order to classify it (see the EXTENDED DESCRIPTION). No position-sensitive default system tests nor context-sensitive default system tests shall be applied unless the −d option is also specified.
- −m file
- Specify the name of a file containing position-sensitive tests that shall be applied to a file in order to classify it (see the EXTENDED DESCRIPTION).
- A pathname of a file to be tested.
- Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1‐2008, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.)
- If set to a non-empty string value, override the values of all the other internationalization variables.
- Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments and input files).
- Determine the location of message catalogs for the processing of LC_MESSAGES.
"%s: %s\n", <file>, <type>
|If file is:||<type> shall contain the string:||Notes|
|Block special||block special||1|
|Character special||character special||1|
|Symbolic link||symbolic link to||1|
|Regular file||regular file||1,2|
|Empty regular file||empty||3|
|Regular file that cannot be read||cannot open||3|
|ar archive library (see ar)||archive||3,4,6|
|Extended cpio format (see pax)||cpio archive||3,4,6|
|Extended tar format (see ustar in pax)||tar archive||3,4,6|
|Shell script||commands text||3,5,6|
|C-language source||c program text||3,5,6|
|FORTRAN source||fortran program text||3,5,6|
|Regular file whose type cannot be determined||data||3|
- This is a file type test.
- This test is applied only if the −i option is specified.
- This test is applied only if the −i option is not specified.
- This is a position-sensitive default system test.
- This is a context-sensitive default system test.
- Position-sensitive default system tests and context-sensitive default system tests are not applied if the −M option is specified unless the −d option is also specified.
"%s: %s %s\n", <file>, <type>, <contents of link>"
- An unsigned number (optionally preceded by a single '>' character) specifying the offset, in bytes, of the value in the file that is to be compared against the value field of the line. If the file is shorter than the specified offset, the test shall fail.
If the offset begins with the character '>', the test contained in the line shall not be applied to the file unless the test on the last line for which the offset did not begin with a '>' was successful. By default, the offset shall be interpreted as an unsigned decimal number. With a leading 0x or 0X, the offset shall be interpreted as a hexadecimal number; otherwise, with a leading 0, the offset shall be interpreted as an octal number.
- The type of the value in the file to be tested. The type shall consist of the type specification characters d, s, and u, specifying signed decimal, string, and unsigned decimal, respectively.
The type string shall be interpreted as the bytes from the file starting at the specified offset and including the same number of bytes specified by the value field. If insufficient bytes remain in the file past the offset to match the value field, the test shall fail. The type specification characters d and u can be followed by an optional unsigned decimal integer that specifies the number of bytes represented by the type. The type specification characters d and u can be followed by an optional C, S, I, or L, indicating that the value is of type char, short, int, or long, respectively. The default number of bytes represented by the type specifiers d, f, and u shall correspond to their respective C-language types as follows. If the system claims conformance to the C-Language Development Utilities option, those specifiers shall correspond to the default sizes used in the c99 utility. Otherwise, the default sizes shall be implementation-defined. For the type specifier characters d and u, the default number of bytes shall correspond to the size of a basic integer type of the implementation. For these specifier characters, the implementation shall support values of the optional number of bytes to be converted corresponding to the number of bytes in the C-language types char, short, int, or long. These numbers can also be specified by an application as the characters C, S, I, and L, respectively. The byte order used when interpreting numeric values is implementation-defined, but shall correspond to the order in which a constant of the corresponding type is stored in memory on the system. All type specifiers, except for s, can be followed by a mask specifier of the form & number. The mask value shall be AND'ed with the value of the input file before the comparison with the value field of the line is made. By default, the mask shall be interpreted as an unsigned decimal number. With a leading 0x or 0X, the mask shall be interpreted as an unsigned hexadecimal number; otherwise, with a leading 0, the mask shall be interpreted as an unsigned octal number. The strings byte, short, long, and string shall also be supported as type fields, being interpreted as dC, dS, dL, and s, respectively.
- The value to be compared with the value from the file.
If the specifier from the type field is s or string, then interpret the value as a string. Otherwise, interpret it as a number. If the value is a string, then the test shall succeed only when a string value exactly matches the bytes from the file. If the value is a string, it can contain the following sequences:
- The <backslash>-escape sequences as specified in the Base Definitions volume of POSIX.1‐2008, Table 5-1, Escape Sequences and Associated Actions ('\\', '\a', '\b', '\f', '\n', '\r', '\t', '\v'). In addition, the escape sequence '\ ' (the <backslash> character followed by a <space> character) shall be recognized to represent a <space> character. The results of using any other character, other than an octal digit, following the <backslash> are unspecified.
- Octal sequences that can be used to represent characters with specific coded values. An octal sequence shall consist of a <backslash> followed by the longest sequence of one, two, or three octal-digit characters (01234567).
- The test shall succeed if the value from the file equals the value field.
- The test shall succeed if the value from the file is less than the value field.
- The test shall succeed if the value from the file is greater than the value field.
- The test shall succeed if all of the set bits in the value field are set in the value from the file.
- The test shall succeed if at least one of the set bits in the value field is not set in the value from the file.
- The test shall succeed if the file is large enough to contain a value of the type specified starting at the offset specified.
- The message to be printed if the test succeeds. The message shall be interpreted using the notation for the printf formatting specification; see printf. If the value field was a string, then the value from the file shall be the argument for the printf formatting specification; otherwise, the value from the file shall be the argument.
- Successful completion.
- An error occurred.
file −− "$1" | grep −q ':.*executable' && printf "%s is executable.\n$1"
/home/dwc/core: ELF 32-bit MSB core file SPARC Version 1
/home/dwc/core: ELF 32-bit MSB core file SPARC Version 1, from 'testprog'
0 short 070707 cpio archive 0 short 0143561 Byte-swapped cpio archive 0 string 070707 ASCII cpio archive 0 long 0177555 Very old archive 0 short 0177545 Old archive 0 short 017437 Old packed data 0 string \037\036 Packed data 0 string \377\037 Compacted data 0 string \037\235 Compressed data >2 byte&0x80 >0 Block compressed >2 byte&0x1f x %d bits 0 string \032\001 Compiled Terminfo Entry 0 short 0433 Curses screen image 0 short 0434 Curses screen image 0 string <ar> System V Release 1 archive 0 string !<arch>\n__.SYMDEF Archive random library 0 string !<arch> Archive 0 string ARF_BEGARF PHIGS clear text archive 0 long 0x137A2950 Scalable OpenFont binary 0 long 0x137A2951 Encrypted scalable OpenFont binary