FILE(1POSIX)
๐ธ FILE(1POSIX) ๐ธ
okay bestie, welcome to the POSIX Programmer's Manual โ this is the file page, and she is LONG, so buckle up ๐ (also: we're doing this in parts, so don't go anywhere ๐)
โจ PROLOG โจ
so like, this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ โ so if you're on Linux, go check the corresponding Linux manual page for the deets on Linux behavior ๐ง โ or honestly the interface might just not be implemented on Linux at all. yeah. it's giving "read the fine print" ๐
๐ NAME ๐
file โ determine file type ๐โจ
๐ฆ SYNOPSIS ๐ฆ
file [-dh] [-M file] [-m file] file...
file -i [-h] file...
๐ DESCRIPTION ๐
so the file utility is gonna run a whole series of tests in sequence on each file you hand it, trying to figure out what tf it is. like a vibe check but for files. here's the order, no skipping ๐ฅ
- if
filedoes 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. (translation: "i looked, bestie, i just don't know her" ๐ญ) - if the file is not a regular file, its file type shall be identified. the file types
directory,FIFO,socket,block special, andcharacter specialshall be identified as such. other implementation-defined file types may also be identified. iffileis a symbolic link, by default the link shall be resolved andfileshall test the type of file referenced by the symbolic link. (see the-hand-ioptions below ๐) - if the length of
fileis zero, it shall be identified as an empty file. nothing going on in there. empty. void. she's got nothing to say ๐ซฅ - the
fileutility shall examine an initial segment offileand 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-moptions below โ ๏ธ) - the
fileutility shall examinefileand shall make a guess at identifying its contents based on context-sensitive default system tests. (again, the answer is not guaranteed to be correct ๐) - the
fileshall be identified as adatafile. the catch-all. the "she's just data, idk" ending ๐
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. (yes this is repeated. the manpage said it twice and so do i. periodt.)
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. ๐
๐ OPTIONS ๐
the file utility shall conform to the Base Definitions volume of POSIX.1โ2017, Section 12.2, Utility Syntax Guidelines โ EXCEPT that the order of the -m, -d, and -M options shall be significant. yes, order matters here. no cap. that's the whole plot twist ๐ข
the following options shall be supported by the implementation:
-dโ apply any position-sensitive default system tests and context-sensitive default system tests to the file. this is the default if no-Mor-moption is specified. she's the baseline, the standard, the no-notes choice โจ-hโ when a symbolic link is encountered, identify the file as asymbolic link. and listen: if-his not specified andfileis a symbolic link that refers to a nonexistent file,fileshall identify the file as a symbolic link, as if-hhad been specified. so the broken-link case behaves like-hanyway, iykyk ๐-iโ 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. she's stopping early. no further questions ๐-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-doption is also specified. big "custom rules only" energy ๐ง-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).
okay so here's the combo logic, and it's giving โจchoreographyโจ:
- if the
-moption is specified without specifying the-doption or the-Moption, position-sensitive default system tests shall be applied after the position-sensitive tests specified by the-moption. - if the
-Moption is specified with the-doption, the-moption, or both, or the-moption is specified with the-doption, the concatenation of the position-sensitive tests specified by these options shall be applied in the order specified by the appearance of these options. (told you the order was significant ๐) - if a
-Mor-mfile option-argument is-, the results are unspecified. ๐จ don't do it, bestie, nobody knows what happens.
๐ OPERANDS ๐
the following operand shall be supported:
fileโ a pathname of a file to be tested. that's her. the one we're investigating ๐
โญ STDIN โญ
the standard input shall be used if a file operand is '-' and the implementation treats the '-' as meaning standard input. otherwise, the standard input shall not be used. simple as that ๐ฏ
๐ง INPUT FILES ๐ง
the file can be any file type. no discrimination here, we test everybody ๐
๐ธ ENVIRONMENT VARIABLES ๐ธ
the following environment variables shall affect the execution of file:
LANGโ provide a default value for the internationalization variables that are unset or null. (see the Base Definitions volume of POSIX.1โ2017, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.)LC_ALLโ if set to a non-empty string value, override the values of all the other internationalization variables. she overrides everything. main character behavior ๐LC_CTYPEโ 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).LC_MESSAGESโ 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.NLSPATHโ determine the location of message catalogs for the processing ofLC_MESSAGES.
๐ ASYNCHRONOUS EVENTS ๐
Default. that's it. that's the whole section. we love a low-effort icon ๐ซถ
โจ STDOUT โจ
in the POSIX locale, the following format shall be 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> shall contain (but is not limited to) the corresponding string, unless the file is identified by a position-sensitive test specified by a -M or -m option. also, each <space> shown in the strings shall be exactly one <space>. one. singular. don't get cute with it ๐จ
๐ Table 4-9: File Utility Output Strings ๐
so here's the tea, translated out of table format into something you can actually read:
- if
fileis Nonexistent โ<type>shall contain the string:cannot open - if
fileis Block special โ<type>shall contain the string:block specialโ Notes: 1 - if
fileis Character special โ<type>shall contain the string:character specialโ Notes: 1 - if
fileis Directory โ<type>shall contain the string:directoryโ Notes: 1 - if
fileis FIFO โ<type>shall contain the string:fifoโ Notes: 1 - if
fileis Socket โ<type>shall contain the string:socketโ Notes: 1 - if
fileis Symbolic link โ<type>shall contain the string:symbolic link toโ Notes: 1 - if
fileis Regular file โ<type>shall contain the string:regular fileโ Notes: 1,2 - if
fileis Empty regular file โ<type>shall contain the string:emptyโ Notes: 3 - if
fileis Regular file that cannot be read โ<type>shall contain the string:cannot openโ Notes: 3 - if
fileis Executable binary โ<type>shall contain the string:executableโ Notes: 3,4,6 - if
fileis ar archive library (see ar) โ<type>shall contain the string:archiveโ Notes: 3,4,6 - if
fileis Extended cpio format (see pax) โ<type>shall contain the string:cpio archiveโ Notes: 3,4,6 - if
fileis Extended tar format (see ustar in pax) โ<type>shall contain the string:tar archiveโ Notes: 3,4,6 - if
fileis Shell script โ<type>shall contain the string:commands textโ Notes: 3,5,6 - if
fileis C-language source โ<type>shall contain the string:c program textโ Notes: 3,5,6 - if
fileis FORTRAN source โ<type>shall contain the string:fortran program textโ Notes: 3,5,6 - if
fileis Regular file whose type cannot be determined โ<type>shall contain the string:dataโ Notes: 3
๐ฆ Notes ๐ฆ
- this is a file type test.
- this test is applied only if the
-ioption is specified. - this test is applied only if the
-ioption 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
-Moption is specified unless the-doption is also specified.
in the POSIX locale, if file is identified as a symbolic link (see the -h option), the following alternative output format shall be used:
"%s: %s %s\n", <file>, <type>, <contents of link>"
โ ๏ธ and this next bit is important, bestie: if the file named by thefileoperand does not exist, cannot be read, or the type of the file named by thefileoperand cannot be determined, this shall not be considered an error that affects the exit status. so she'll be like "couldn't open her ๐คท" and still walk away with a clean exit status. no drama, no error code ๐
๐ STDERR ๐
the standard error shall be used only for diagnostic messages. that's her only job. don't ask her for more ๐ซถ
๐ OUTPUT FILES ๐
None. nothing gets written. she only reads and tells you what she thinks. iconic ๐
๐ฆ EXTENDED DESCRIPTION ๐ฆ
okay so bestie, if you hand a file to -m or -M, that file has to contain one position-sensitive test per line, and each one gets applied to the file you're checking. If a test succeeds, the message field of that line gets printed and then we're done โ no further tests get applied. The one exception? Tests on the lines immediately following, as long as those lines start with a single > character. Those still get applied. iykyk.
Each line is made of these four <tab>-separated fields. (Real talk: implementations may allow any combination of one or more whitespace characters other than <newline> to act as field separators. generous of them fr fr.)
offsetโ an unsigned number (optionally preceded by a single>character) that says the offset, in bytes, of the value in the file that's getting compared against the value field of the line. If the file is shorter than the specified offset, the test fails. ๐
If the offset starts with the character >, the test on that line shall not be applied to the file unless the test on the last line whose offset did not begin with a > was successful. By default the offset is read as an unsigned decimal number. With a leading 0x or 0X, it's read as a hexadecimal number; otherwise, if it leads with 0, it's read as an octal number.
typeโ the type of the value in the file being tested. The type consists of the type specification charactersd,s, andu, meaning signed decimal, string, and unsigned decimal respectively. slay.
The type string gets interpreted as the bytes from the file starting at the specified offset, including the same number of bytes specified by the value field. If not enough bytes remain in the file past the offset to match the value field, the test fails.
The type specification characters d and u can be followed by an optional unsigned decimal integer that says how many bytes the type represents. They can also be followed by an optional C, S, I, or L, meaning 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 corresponds to their respective C-language types, like this: if the system claims conformance to the C-Language Development Utilities option, those specifiers correspond to the default sizes used in the c99 utility. Otherwise, the default sizes are implementation-defined (mysterious, we love that for them).
For the type specifier characters d and u, the default number of bytes corresponds 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 it shall correspond to the order in which a constant of the corresponding type is stored in memory on the system.
Every type specifier except s can be followed by a mask specifier of the form &number. The mask value gets 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 is read as an unsigned decimal number. With a leading 0x or 0X, the mask is read as an unsigned hexadecimal number; otherwise, with a leading 0, the mask is read as an unsigned octal number.
The strings byte, short, long, and string are also supported as type fields, and they're interpreted as dC, dS, dL, and s, respectively. ๐ซถ
valueโ the value to be compared with the value from the file.
If the specifier from the type field is s or string, interpret the value as a string. Otherwise, interpret it as a number. If the value is a string, the test succeeds only when a string value exactly matches the bytes from the file. no wiggle room bestie.
If the value is a string, it can contain the following sequences:
\characterโ the<backslash>-escape sequences as specified in the Base Definitions volume of POSIX.1โ2017, Table 5-1, Escape Sequences and Associated Actions ('\\','\a','\b','\f','\n','\r','\t','\v'). On top of that, 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โ octal sequences, used to represent characters with specific coded values. An octal sequence is a<backslash>followed by the longest sequence of one, two, or three octal-digit characters (01234567).
By default, any value that isn't a string gets interpreted as a signed decimal number. Any such value with a leading 0x or 0X gets interpreted as an unsigned hexadecimal number; otherwise, with a leading zero, the value gets interpreted as an unsigned octal number.
If the value isn't a string, it can be preceded by a character that says which comparison to perform. The permissible characters and the comparisons they specify are:
=โ the test succeeds if the value from the file equals the value field.<โ the test succeeds if the value from the file is less than the value field.>โ the test succeeds if the value from the file is greater than the value field.&โ the test succeeds if all of the set bits in the value field are set in the value from the file.^โ the test succeeds if at least one of the set bits in the value field is not set in the value from the file.xโ the test succeeds if the file is large enough to contain a value of the type specified starting at the offset specified.
messageโ the message to be printed if the test succeeds. The message gets interpreted using the notation for theprintfformatting specification; seeprintf. If the value field was a string, then the value from the file shall be the argument for theprintfformatting specification; otherwise, the value from the file shall be the argument.
โจ EXIT STATUS โจ
these are the exit values you might get back, bestie:
0โ successful completion. ๐>0โ an error occurred. ๐ญ
๐ซ CONSEQUENCES OF ERRORS ๐ซ
Default.
The following sections are informative.
๐ธ APPLICATION USAGE ๐ธ
The file utility can only be required to guess at many of the file types, because only exhaustive testing can determine some types with certainty. Like, for example, binary data on some implementations might match the initial segment of an executable or a tar archive. it's giving "close enough" ๐ญ
Also 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. Note also that on systems that recognize shell script files starting with "#!" as executable files, these may be identified as executable binary files rather than as shell scripts. ๐
๐ EXAMPLES ๐
Determine whether an argument is a binary executable file:
file -- "$1" | grep -q ':.*executable' &&
printf "%s is executable.\n$1"
โญ go off, honestly.
๐ RATIONALE ๐
Okay so first thing's first: the -f option was omitted because the same effect can (and should) be obtained using the xargs utility. Periodt. ๐
Historical versions of the file utility attempt to identify the following types of files: symbolic link, directory, character special, block special, socket, tar archive, cpio archive, SCCS archive, archive library, empty, compress output, pack output, binary data, C source, FORTRAN source, assembler source, nroff/troff/eqn/tbl source troff output, shell script, C shell script, English text, ASCII text, various executables, APL workspace, compiled terminfo entries, and CURSES screen images. Only those types that are reasonably well specified in POSIX or are directly related to POSIX utilities are listed in the table.
Historical systems have used a โmagic fileโ named /etc/magic to help identify file types. Because it is generally useful for users and scripts to be able to identify special file types, the -m flag and a portable format for user-created magic files has been specified. No requirement is made that an implementation of file use this method of identifying files, only that users be permitted to add their own classifying tests.
In addition, three options have been added to historical practice. The -d flag has been added to permit users to cause their tests to follow any default system tests. The -i flag has been added to permit users to test portably for regular files in shell scripts. The -M flag has been added to permit users to ignore any default system tests. Lowkey iconic. โจ
The POSIX.1-2008 description of default system tests and the interaction between the -d, -M, and -m options did not clearly indicate that there were two types of โdefault system testsโ. The โposition-sensitive testsโ determine file types by looking for certain string or binary values at specific offsets in the file being examined. These position-sensitive tests were implemented in historical systems using the magic file described above. Some of these tests are now built into the file utility itself on some implementations so the output can provide more detail than can be provided by magic files. For example, a magic file can easily identify a core file on most implementations, but cannot name the program file that dropped the core. A magic file could produce output such as:
/home/dwc/core: ELF 32-bit MSB core file SPARC Version 1
but by building the test into the file utility, you could get output such as:
/home/dwc/core: ELF 32-bit MSB core file SPARC Version 1, from 'testprog'
These extended built-in tests are still to be treated as position-sensitive default system tests even if they are not listed in /etc/magic or any other magic file.
The context-sensitive default system tests were always built into the file utility. These tests looked for language constructs in text files trying to identify shell scripts, C, FORTRAN, and other computer language source files, and even plain text files. With the addition of the -m and -M options the distinction between position-sensitive and context-sensitive default system tests became important because the order of testing is important. The context-sensitive system default tests should never be applied before any position-sensitive tests even if the -d option is specified before a -m option or -M option due to the high probability that the context-sensitive system default tests will incorrectly identify arbitrary text files as text files before position-sensitive tests specified by the -m or -M option would be applied to give a more accurate identification.
Leaving the meaning of -M - and -m - unspecified allows an existing prototype of these options to continue to work in a backwards-compatible manner. (In that implementation, -M - was roughly equivalent to -d in POSIX.1-2008.)
The historical -c option was omitted as not particularly useful to users or portable shell scripts. In addition, a reasonable implementation of the file utility would report any errors found each time the magic file is read.
The historical format of the magic file was the same as that specified by the Rationale in the ISO POSIX-2:1993 standard for the offset, value, and message fields; however, it used less precise type fields than the format specified by the current normative text. The new type field values are a superset of the historical ones.
The following is an example magic file:
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
The use of a basic integer data type is intended to allow the implementation to choose a word size commonly used by applications on that architecture.
Earlier versions of this standard allowed for implementations with bytes other than eight bits, but this has been modified in this version.
๐ฎ FUTURE DIRECTIONS ๐ฎ
None. Not a single one. The future is a blank canvas bestie. ๐ฆ
๐ SEE ALSO ๐
arlspaxprintf
The Base Definitions volume of POSIX.1-2017, Table 5-1, Escape Sequences and Associated Actions, Chapter 8, Environment Variables, Section 12.2, Utility Syntax Guidelines.
โ๏ธ COPYRIGHT โ๏ธ
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
And that's the whole vibe check! Source/version: IEEE/The Open Group, 2017, FILE(1POSIX). Bye bestie, stay iconic! ๐ โจ
๐ฆ where this page comes from
- debian bookworm: manpages-posix 2017a-2 ยท Other
- debian trixie: manpages-posix 2017a-2 ยท Other
- ubuntu jammy: manpages-posix 2017a-2 ยท Other
- ubuntu noble: manpages-posix 2017a-2 ยท Other
- ubuntu resolute: manpages-posix 2017a-2build1 ยท Other
- ubuntu stonking: manpages-posix 2017a-2.1 ยท Other
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms