File (command)

file
Developer(s)AT&T Bell Laboratories
Initial release1973 (1973) as part of Unix Research Version 4; 1986 (1986) open-source reimplementation
Stable release
5.46[1] Edit this on Wikidata / 27 November 2024 (27 November 2024)
Repositorygithub.com/file/file
Written inC
Operating systemUnix, Unix-like, Plan 9, IBM i
PlatformCross-platform
TypeFile type detector
LicenseBSD license, CDDL
Plan 9: MIT License
Websitedarwinsys.com/file/

file is a shell command for reporting the type of data contained in a file. It is commonly supported in Unix and Unix-like operating systems.

As the command uses relatively quick-running heuristics to determine file type, it can report misleading information. The command can be fooled, for example, by including a magic number in the content even if the rest of the content does not match what the magic number indicates. The command report cannot be taken as completely trustworthy.

The Single UNIX Specification (SUS) requires the command to exhibit the following behavior with respect to the file specified via the command-line:

  1. If the file cannot be read, or its Unix file type is undetermined, the command will report that the file was processed but its type was undetermined
  2. The command must be able to determine the types directory, FIFO, socket, block special file, and character special file
  3. A zero-length file is reported as such
  4. An initial part of file is considered and the command is to use position-sensitive tests
  5. The entire file is considered and the command is to use context-sensitive tests
  6. Otherwise, the file is reported as a data file

Position-sensitive tests are normally implemented by matching various locations within the file against a textual database of magic numbers (see the Usage section). This differs from other simpler methods such as file extensions and schemes like MIME.

In the System V implementation, the Ian Darwin implementation, and the OpenBSD implementation, the command uses a database to drive the probing of the lead bytes. That database is stored as a file that is located in /etc/magic, /usr/share/file/magic or similar.

  1. ^ "[File] FIle 5.46 is now available". 27 November 2024. Retrieved 28 November 2024.

© MMXXIII Rich X Search. We shall prevail. All rights reserved. Rich X Search