# File (command)

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/File_(command)
> Markdown URL: https://mediated.wiki/source/File_(command).md
> Source: https://en.wikipedia.org/wiki/File_(command)
> Source revision: 1355775392
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

Shell command for reporting file type information

"File (software)" redirects here. For the file management program bundled with [iOS 11](/source/IOS_11), see [Files (software)](/source/Files_(software)).

file Example usage of file Developer AT&T Bell Laboratories Release 1973 (1973) as part of Unix Research Version 4; 1986 (1986) open-source reimplementation Stable release 5.48[1] / 7 June 2026; 19 days ago (7 June 2026) Written in C Operating system Unix, Unix-like, Plan 9, IBM i Platform Cross-platform Type File type detector License BSD license, CDDL Plan 9: MIT License Website darwinsys.com/file/ Repository github.com/file/file

**file** is a [shell](/source/Shell_(computing)) [command](/source/Command_(computing)) for reporting the type of data contained in a [file](/source/Computer_file). It is commonly supported in [Unix](/source/Unix) and [Unix-like](/source/Unix-like) [operating systems](/source/Operating_systems).

As the command uses relatively quick-running [heuristics](/source/Heuristic) to determine [file type](/source/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](/source/Single_UNIX_Specification) (SUS) requires the command to exhibit the following behavior with respect to the file specified via the [command-line](/source/Command-line_interface):

1. If the file cannot be read, or its [Unix file type](/source/Unix_file_type) is undetermined, the command will report that the file was processed but its type was undetermined

1. The command must be able to determine the types [directory](/source/Folder_(computing)), [FIFO](/source/Named_pipe), [socket](/source/Unix_domain_socket), block [special file](/source/Device_file), and character special file

1. A zero-length file is reported as such

1. An initial part of file is considered and the command is to use position-sensitive tests

1. The entire file is considered and the command is to use context-sensitive tests

1. 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](/source/Magic_number_(programming)) (see the Usage section). This differs from other simpler methods such as [file extensions](/source/File_extension) and schemes like [MIME](/source/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.

## History

The file command originated in [Unix Research Version 4](/source/Unix_Research_Version_4)[2] in 1973. [System V](/source/System_V) brought a major update with several important changes, most notably moving the file type information into an external text file rather than compiling it into the binary itself.

Most major [BSD](/source/BSD) and [Linux](/source/Linux) distributions include a [free](/source/Free_software), [open-source](/source/Open-source_software) implementation that was written from scratch by Ian Darwin in 1986–87.[3] It keeps file type information in a text file with a format based on that of the System V version. It was expanded by Geoff Collyer in 1989 and since then has had input from many others, including Guy Harris, Chris Lowth and Eric Fischer. From late 1993 onward, its maintenance has been organized by Christos Zoulas. The [OpenBSD](/source/OpenBSD) system has its own subset implementation written from scratch, but still uses the Darwin/Zoulas collection of magic file formatted information.

The file command was ported to the [IBM i](/source/IBM_i) operating system.[4]

As of version 4.00 of the Ian Darwin/Christos Zoulas implementation of file, the functionality of the command is implemented in and exposed by a libmagic [library](/source/Library_(computing)) that is accessible to consuming code via [C](/source/C_(programming_language)) (and compatible) linking.[5][6][7][8]

## Usage

The SUS[9] mandates the following command-line options:

- -M *file*, prevents the default position-sensitive and context-sensitive tests in favor of the tests specified in a specially formatted file

- -m *file*, same as for -M, but with tests in addition to the default

- -d, selects default position-sensitive and context-sensitive tests; this is the default behavior unless -M or -m are specified

- -h, do not dereference [symbolic links](/source/Symbolic_link) that point to an existing file or directory

- -i, do not classify the file further than to report as: nonexistent, a block special file, a character special file, a directory, a [FIFO](/source/Named_pipe), a socket, a symbolic link, or a regular file; the Ian Darwin and OpenBSD versions behave differently with this option and instead output an [Internet media type](/source/Internet_media_type) ("[MIME](/source/MIME) type") identifying the recognized file format

Implementations may add extra options. Ian Darwin's implementation adds -s 'special files', -k 'keep-going' or -r 'raw', among many others.[10]

## Examples

For a [C](/source/C_(programming_language)) [source code](/source/Source_code) file, file main.c reports:

main.c: C program text

For a compiled executable, file program reports information like:

program: [ELF](/source/Executable_and_Linkable_Format) [32-bit](/source/32-bit) [LSB](/source/Least_significant_bit) [executable](/source/Executable), [Intel 80386](/source/Intel_80386), version 1 ([SYSV](/source/SYSV)), [dynamically linked](/source/Dynamically_linked)
    (uses [shared libs](/source/Shared_Library)), [stripped](/source/Strip_(Unix))

For a block device [/dev/hda](/source/Device_file#Naming_conventions), file /dev/hda1 reports:

/dev/hda1: [block special](/source/Device_file#Block_devices) (0/0)

By default, file does not try to read a device file due to potential undesirable effects. But using the non-standard option -s (available in the Ian Darwin branch), which requests to read device files to identify content, file -s /dev/hda1 reports details such as:

/dev/hda1: Linux/[i386](/source/I386) [ext2](/source/Ext2) [filesystem](/source/Filesystem)

Via Ian Darwin's non-standard option -k, the command does not stop after the first hit found, but looks for other matching patterns. The -r option, which is available in some versions, causes the [new line](/source/Newline) character to be displayed in its raw form rather than in its octal representation. On Linux, file -k -r libmagic-dev_5.35-4_armhf.deb reports information like:

libmagic-dev_5.35-4_[armhf](/source/Armhf).[deb](/source/Deb_(file_format)): Debian binary package (format 2.0)
- current [ar](/source/Ar_(Unix)) archive
- data

For a compressed file, file compressed.gz reports information like:

compressed.gz: [gzip](/source/Gzip) [compressed](/source/Data_compression) data, [deflated](/source/DEFLATE), original filename, `compressed', last
    modified: Thu Jan 26 14:08:23 2006, [os](/source/Operating_system): Unix

For a compressed file, file -i compressed.gz reports information like:

compressed.gz: [application/x-gzip](/source/Media_type); [charset](/source/Character_set)=[binary](/source/Binary_file)

For a PPM file, file data.ppm reports:

data.ppm: [Netpbm PPM](/source/Netpbm_format) "rawbits" image data

For a [Mach-O](/source/Mach-O) [universal binary](/source/Universal_binary), file /bin/cat reports like:

/bin/cat: Mach-O universal binary with 2 [architectures](/source/Instruction_set_architecture)
/bin/cat (for architecture [ppc7400](/source/PowerPC)):	Mach-O executable ppc
/bin/cat (for architecture i386):	Mach-O executable i386

For a [symbolic link](/source/Symbolic_link), file /usr/bin/vi reports:

/usr/bin/vi: symbolic link to vim

Identifying a symbolic link is not available on all platforms and will be dereferenced if -L is passed or POSIXLY_CORRECT is set.

## See also

- [List of POSIX commands](/source/List_of_POSIX_commands)

## References

1. **[^](#cite_ref-wikidata-656d4b08ade247ae2b29e402002b0dfb610a6dcb-v20_1-0)** Christos Zoulas (7 June 2026). [https://mailman.astron.com/pipermail/file/2026-June/001678.html](https://mailman.astron.com/pipermail/file/2026-June/001678.html). Retrieved 8 June 2026. {{[cite web](https://en.wikipedia.org/wiki/Template:Cite_web)}}: Missing or empty |title= ([help](https://en.wikipedia.org/wiki/Help:CS1_errors#citation_missing_title))

1. **[^](#cite_ref-2)** ["Source of the UNIX V4 "file" man page"](https://minnie.tuhs.org/cgi-bin/utree.pl?file=V4/man/man1/file.1). [Archived](https://web.archive.org/web/20191210000719/https://minnie.tuhs.org/cgi-bin/utree.pl?file=V4%2Fman%2Fman1%2Ffile.1) from the original on 2019-12-10. Retrieved 2022-03-13.

1. **[^](#cite_ref-3)** The early history of this program is recorded in its private CVS repository; see [\[1\]](https://www.darwinsys.com/file/file.c.log.txt) [Archived](https://web.archive.org/web/20170401080455/https://www.darwinsys.com/file/file.c.log.txt) 2017-04-01 at the [Wayback Machine](/source/Wayback_Machine) the log of the main program

1. **[^](#cite_ref-4)** ["IBM System i Version 7.2 Programming Qshell"](https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc) (PDF). *[IBM](/source/IBM)*. [Archived](https://web.archive.org/web/20210305180649/https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc) (PDF) from the original on 2021-03-05. Retrieved 2020-09-05.

1. **[^](#cite_ref-5)** [libmagic(3)](https://man7.org/linux/man-pages/man3/libmagic.3.html) – [Linux](/source/Linux) Programmer's [Manual](/source/Man_page) – Library Functions

1. **[^](#cite_ref-6)** [libmagic(3)](https://man.netbsd.org/libmagic.3) – [NetBSD](/source/NetBSD) Library Functions [Manual](/source/Man_page)

1. **[^](#cite_ref-7)** Zoulas, Christos (February 27, 2003). ["file-3.41 is now available"](https://mx.gw.com/pipermail/file/2003/000034.html). *File* (Mailing list). [Archived](https://web.archive.org/web/20160304040612/https://mx.gw.com/pipermail/file/2003/000034.html) from the original on March 4, 2016. Retrieved January 1, 2013.

1. **[^](#cite_ref-8)** Zoulas, Christos (March 24, 2003). ["file-4.00 is now available"](https://mx.gw.com/pipermail/file/2003/000043.html). *File* (Mailing list). [Archived](https://web.archive.org/web/20161228123411/https://mx.gw.com/pipermail/file/2003/000043.html) from the original on December 28, 2016. Retrieved January 1, 2013.

1. **[^](#cite_ref-9)** ["The Open Group Base Specifications Issue 8 — *file* command"](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/file.html). [Archived](https://web.archive.org/web/20250514230703/https://pubs.opengroup.org/onlinepubs/9799919799/utilities/file.html) from the original on 2025-05-14. Retrieved 2025-05-05.

1. **[^](#cite_ref-linux_10-0)** [file(1)](https://man7.org/linux/man-pages/man1/file.1.html) – [Linux](/source/Linux) User [Manual](/source/Man_page) – User Commands

## External links

The Wikibook *[Guide to Unix](https://en.wikibooks.org/wiki/Guide_to_Unix)* has a page on the topic of: ***[Commands](https://en.wikibooks.org/wiki/Guide_to_Unix/Commands)***

- [file](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/file.html): determine file type – Shell and Utilities Reference, [The Single UNIX Specification](/source/Single_Unix_Specification), Version 5 from [The Open Group](/source/The_Open_Group)

- [file(1)](https://man7.org/linux/man-pages/man1/file.1.html) – [Linux](/source/Linux) User [Manual](/source/Man_page) – User Commands

- [libmagic(3)](https://man.netbsd.org/libmagic.3) – [NetBSD](/source/NetBSD) Library Functions [Manual](/source/Man_page)

- [libmagic(3)](https://man7.org/linux/man-pages/man3/libmagic.3.html) – [Linux](/source/Linux) Programmer's [Manual](/source/Man_page) – Library Functions

- [file(1)](https://man.openbsd.org/file.1) – [OpenBSD](/source/OpenBSD) General Commands [Manual](/source/Man_page) – a non-Ian Darwin implementation

- [file(1)](https://9p.io/magic/man2html/1/file) – [Plan 9](/source/Plan_9_from_Bell_Labs) Programmer's Manual, Volume 1 – a non-Ian Darwin, non-SUS implementation

- [Fine Free File Command](https://darwinsys.com/file/) – homepage for Ian Darwin's version of file used in major BSD and Linux distributions. - [mailing list](https://mailman.astron.com/mailman/listinfo/file) - [releases](https://ftp.astron.com/pub/file)

- [binwalk](https://github.com/ReFirmLabs/binwalk), a firmware analysis tool that carves files based on libmagic signatures

- [TrID](https://mark0.net/soft-trid-e.html), an alternative providing ranked answers (instead of just one) based on statistics.

- [Magika](https://google.github.io/magika/), an ML-based tool, by Google Research

v t e Unix command-line utilities and shell builtins File system cat chattr chmod chown chgrp cksum cmp cp dd du df file fuser ln ls mkdir mv pax pwd rm rmdir split tee touch type umask Processes at bg crontab fg kill nice ps time User environment env exit logname mesg talk tput uname who write Text processing awk basename comm csplit cut diff dirname ed ex fold head iconv join m4 more nl paste patch printf read sed sort strings tail tr troff uniq vi wc xargs Shell builtins alias cd echo test unset wait Searching find grep Documentation man Software development ar ctags lex make nm strip yacc Miscellaneous bc cal dc expr lp od sleep true and false Categories Standard Unix programs Unix SUS2008 utilities List

v t e Plan 9 command-line interface programs and shell builtins File system chmod chgrp cmp cp dd du file gzip ls mkdir pwd rm split tee touch Processes kill ps User environment passwd who Text processing awk basename comm diff ed eqn join sed sort spell strings tail tr troff uniq wc Shell builtins echo test Networking ip/ipconfig ip/ping netstat Searching grep Software development ar hoc lex nm strip yacc Miscellaneous bc cal fortune sleep Category

---
Adapted from the Wikipedia article [File (command)](https://en.wikipedia.org/wiki/File_(command)) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/File_(command)?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
