file Command
Purpose
Determines the file type.
Syntax
To Classify the File Type
file [ -m MagicFile] [ -d ] [ -h ] [ -i ] [ -M MagicFile ] [ -f FileList] [File...]
To Check the Magic File for Format Errors
Description
The file command reads the files specified by the File parameter or the FileList variable, performs a series of tests on each file, and attempts to classify them by type. The command then writes the file types to standard output. The file can be regular file, directory, FIFO(named pipe), block special, character special, symbolic link or sockets type.
- If it is a regular file and of zero length, it is identified as an empty file.
- If the file is a symbolic link, by default, the link is followed by file the symbolic link refers to.
If a file appears to be in ASCII format, the file command examines the first 1024 bytes and determines the file type. If a file does not appear to be in ASCII format, the file command further attempts to distinguish a binary data file from a text file that contains extended characters.
If the File parameter specifies an executable or object module file and the version number is greater than 0, the file command displays the version stamp. The ld command explains the use of a.out files.
If the language environment is the C programming language, the file command uses the /etc/magic file to identify files that have some sort of a magic number; that is, any file containing a numeric or string constant that indicates type.
However, if the language environment is some language other than the C programming language, the file command uses the /usr/lib/nls/msg/<language_env.>/magic.cat file to identify files with a magic number.
If the file does not exist, cannot be read or its file status could not be determined then, it is not considered as an error that affects the exit status. The output indicates that the file was processed but the type could not be determined.
"%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. Each space shown in the strings shall be exactly one space.If file is a: | type shall contain the string: |
---|---|
Directoy | directory |
FIFO | fifo |
Socket | socket |
Block special | block special |
Character special | character special |
Executable binary | executable |
Empty regular file | empty |
Symbolic link | symbolic link to |
ar archive library | archive |
Extended cpio format | cpio archive |
Extended tar format | tar archive |
Shell script | commands text |
C-language source | c program text |
FORTRAN source | fortran program text |
"%s: %s %s\n", file, type, contents of link"
If the
file named by the file operand does not exist or cannot be
read, the string cannot open
shall be included as
part of the type field, but this shall not be considered an
error that affects the exit status. If the type of the file named
by the file operand cannot be determined, the string data
shall be included as part of the type field,
but this shall not be considered an error that affects the exit status.Flags
Item | Description |
---|---|
-c | Checks the specified magic file (the /etc/magic file, by default) for format errors. This validation is not normally done. File typing is not done under this flag. |
-d | Applies any default system tests to the file. |
-f FileList | Reads the specified file list. The file must list one file per line and must not contain leading or trailing spaces. |
-h | When a symbolic link is encountered, identifies the file as a symbolic link. If the -h flag 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 the -h flag had been specified. |
-i | If a file is a regular file, does not attempt to classify the type of the file further, but identifies the file as specified in Description. |
-m MagicFile | Specifies the file name of the magic file (the /etc/magic file, by default). |
-M MagicFile | Specifies the name of a file containing tests that shall be applied to a file in order to classify it. No default system tests shall be applied. |
Exit Status
This command returns the following exit values:
Item | Description |
---|---|
0 | Successful completion. |
>0 | An error occurred. |
Examples
- To display the type
of information a file contains, enter:
This displays the file type of myfile (such as directory, data, ASCII text, C-program source, and archive).file myfile
- To display the type
of each file named in a list of file names, enter: file -f filenamesThis displays the type of each file named in the filenames list. Each file name must appear alone on a line.
Files
Item | Description |
---|---|
/usr/bin/file | Contains the file command. |
/etc/magic | Contains the file type database. |