Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
File (command)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Shell command for reporting file type information}} {{Redirects here|File (software)|the file management program bundled with [[iOS 11]]|Files (software)}} {{lowercase title}} {{Infobox software | name = file | logo = <!-- [[File: ]] --> | logo caption = | screenshot = File-example-command-gimp.gif | caption = Example usage of {{code|file}} | collapsible = | author = | developer = [[AT&T Bell Laboratories]] | released = {{Start date|1973|df=yes}} as part of [[Unix Research Version 4]]; {{Start date|1986|df=yes}} open-source reimplementation | discontinued = | latest release version = {{wikidata|property|preferred|references|edit|P348|P548=Q2804309}} | latest release date = {{Start date and age|{{wikidata|qualifier|preferred|single|P348|P548=Q2804309|P577}}|df=yes}} | latest preview version = | latest preview date = <!-- {{Start date and age|YYYY|MM|DD|df=yes/no}} --> | repo = {{URL|https://github.com/file/file}} | programming language = [[C (programming language)|C]] | operating system = [[Unix]], [[Unix-like]], [[Plan 9 from Bell Labs|Plan 9]], [[IBM i]] | platform = [[Cross-platform]] | size = | language = | genre = [[File type]] detector | license = [[BSD license]], [[Common Development and Distribution License|CDDL]]<br />Plan 9: [[MIT License]] | alexa = | website = {{URL|https://darwinsys.com/file/}} }} '''<code>file</code>''' is a [[Shell (computing)|shell]] [[command (computing)|command]] for reporting the type of data contained in a [[computer file|file]]. It is commonly supported in [[Unix]] and [[Unix-like]] [[operating systems]]. As the command uses relatively quick-running [[heuristic]]s 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 interface|command-line]]: # 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 # The command must be able to determine the types [[folder (computing)|directory]], [[named pipe|FIFO]], [[Unix domain socket|socket]], block [[device file|special file]], and character special file # A zero-length file is reported as such # An initial part of file is considered and the command is to use position-sensitive tests # The entire file is considered and the command is to use context-sensitive tests # 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 number (programming)|magic number]]s (see the Usage section). This differs from other simpler methods such as [[file extension]]s 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 <code>/etc/magic</code>, <code>/usr/share/file/magic</code> or similar. ==History== The <code>file</code> command originated in [[Unix Research Version 4]]<ref>{{cite web|url=https://minnie.tuhs.org/cgi-bin/utree.pl?file=V4/man/man1/file.1|title=Source of the UNIX V4 "file" man page|access-date=2022-03-13|archive-date=2019-12-10|archive-url=https://web.archive.org/web/20191210000719/https://minnie.tuhs.org/cgi-bin/utree.pl?file=V4%2Fman%2Fman1%2Ffile.1|url-status=live}}</ref> in 1973. [[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]] and [[Linux]] distributions include a [[free software|free]], [[open-source software|open-source]] implementation that was written from scratch by Ian Darwin in 1986β87.<ref>The early history of this program is recorded in its private CVS repository; see [https://www.darwinsys.com/file/file.c.log.txt] {{Webarchive|url=https://web.archive.org/web/20170401080455/https://www.darwinsys.com/file/file.c.log.txt |date=2017-04-01 }} the log of the main program</ref> 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]] system has its own subset implementation written from scratch, but still uses the Darwin/Zoulas collection of magic file formatted information. The {{code|file}} command was ported to the [[IBM i]] operating system.<ref>{{cite web |title=IBM System i Version 7.2 Programming Qshell |language=en |website=[[IBM]] |url=https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc |access-date=2020-09-05 |archive-date=2021-03-05 |archive-url=https://web.archive.org/web/20210305180649/https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc |url-status=live }}</ref> As of version 4.00 of the Ian Darwin/Christos Zoulas implementation of <code>file</code>, the functionality of the command is implemented in and exposed by a <code>libmagic</code> [[library (computing)|library]] that is accessible to consuming code via [[C (programming language)|C]] (and compatible) linking.<ref>{{man|3|libmagic|Linux}}</ref><ref>{{man|3|libmagic|NetBSD}}</ref><ref>{{cite mailing list |url=https://mx.gw.com/pipermail/file/2003/000034.html |title=file-3.41 is now available |date=February 27, 2003 |access-date=January 1, 2013 |mailing-list=File |last=Zoulas |first=Christos |archive-date=March 4, 2016 |archive-url=https://web.archive.org/web/20160304040612/https://mx.gw.com/pipermail/file/2003/000034.html |url-status=live }}</ref><ref>{{cite mailing list |url=https://mx.gw.com/pipermail/file/2003/000043.html |title=file-4.00 is now available |date=March 24, 2003 |access-date=January 1, 2013 |mailing-list=File |last=Zoulas |first=Christos |archive-date=December 28, 2016 |archive-url=https://web.archive.org/web/20161228123411/https://mx.gw.com/pipermail/file/2003/000043.html |url-status=live }}</ref> ==Usage== The SUS<ref>{{Cite web |url=https://pubs.opengroup.org/onlinepubs/9799919799/utilities/file.html |title=The Open Group Base Specifications Issue 8 β ''file'' command |access-date=2025-05-05}}</ref> mandates the following command-line options: * <code>-M ''file''</code>, prevents the default position-sensitive and context-sensitive tests in favor of the tests specified in a specially formatted file * <code>-m ''file''</code>, same as for <code>-M</code>, but with tests in addition to the default * <code>-d</code>, selects default position-sensitive and context-sensitive tests; this is the default behavior unless <code>-M</code> or <code>-m</code> are specified * <code>-h</code>, do not dereference [[symbolic link]]s that point to an existing file or directory * <code>-L</code>, dereference the symbolic link that points to an existing file or directory * <code>-i</code>, do not classify the file further than to report as: nonexistent, a block special file, a character special file, a directory, a [[named pipe|FIFO]], 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]] ("[[MIME]] type") identifying the recognized file format Implementations may add extra options. Ian Darwin's implementation adds <code>-s</code> 'special files', <code>-k</code> 'keep-going' or <code>-r</code> 'raw', among many others.<ref name=linux>{{man|1|file|Linux}}</ref> ==Examples== For a [[C (programming language)|C]] [[source code]] file, {{code|file main.c}} reports: main.c: C program text For a compiled executable, {{code|file program}} reports information like: program: [[Executable and Linkable Format|ELF]] [[32-bit]] [[Least significant bit|LSB]] [[executable]], [[Intel 80386]], version 1 ([[SYSV]]), [[dynamically linked]] (uses [[Shared Library|shared libs]]), [[strip (Unix)|stripped]] For a block device [[Device file#Naming conventions|/dev/hda]], {{code|file /dev/hda1}} reports: /dev/hda1: [[Device file#Block devices|block special]] (0/0) By default, <code>file</code> does not try to read a device file due to potential undesirable effects. But using the non-standard option {{code|-s}} (available in the Ian Darwin branch), which requests to read device files to identify content, {{code|file -s /dev/hda1}} reports details such as: /dev/hda1: Linux/[[i386]] [[ext2]] [[filesystem]] Via Ian Darwin's non-standard option <code>-k</code>, the command does not stop after the first hit found, but looks for other matching patterns. The <code>-r</code> option, which is available in some versions, causes the [[Newline|new line]] character to be displayed in its raw form rather than in its octal representation. On Linux, {{code|file -k -r libmagic-dev_5.35-4_armhf.deb}} reports information like: libmagic-dev_5.35-4_[[armhf]].[[deb (file format)|deb]]: Debian binary package (format 2.0) - current [[ar (Unix)|ar]] archive - data For a compressed file, {{code|file compressed.gz}} reports information like: compressed.gz: [[gzip]] [[Data compression|compressed]] data, [[DEFLATE|deflated]], original filename, `compressed', last modified: Thu Jan 26 14:08:23 2006, [[Operating system|os]]: Unix For a compressed file, {{code|file -i compressed.gz}} reports information like: compressed.gz: [[media type|application/x-gzip]]; [[character set|charset]]=[[Binary file|binary]] For a PPM file, {{code|file data.ppm}} reports; data.ppm: [[Netpbm format|Netpbm PPM]] "rawbits" image data For a [[Mach-O]] [[universal binary]], {{code|file /bin/cat}} reports like: /bin/cat: Mach-O universal binary with 2 [[Instruction set architecture|architectures]] /bin/cat (for architecture [[PowerPC|ppc7400]]): Mach-O executable ppc /bin/cat (for architecture i386): Mach-O executable i386 For a [[symbolic link]], {{code|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 <code>-L</code> is passed or <code>POSIXLY_CORRECT</code> is set. == See also == * {{Annotated link|List of POSIX commands}} ==References== {{Reflist}} ==External links== {{Wikibooks|Guide to Unix|Commands}} * {{man|cu|file|SUS|determine file type}} * {{man|1|file|Linux}} * {{man|3|libmagic|NetBSD}} * {{man|3|libmagic|Linux}} * {{man|1|file|OpenBSD}} – a non-Ian Darwin implementation * {{man|1|file|Plan 9}} – a non-Ian Darwin, non-SUS implementation * [https://darwinsys.com/file/ Fine Free File Command] – homepage for Ian Darwin's version of <code>file</code> used in major BSD and Linux distributions. ** [https://mailman.astron.com/mailman/listinfo/file mailing list] ** [https://ftp.astron.com/pub/file releases] * [https://github.com/ReFirmLabs/binwalk binwalk], a firmware analysis tool that carves files based on libmagic signatures * [https://mark0.net/soft-trid-e.html TrID], an alternative providing ranked answers (instead of just one) based on statistics. * [https://google.github.io/magika/ Magika], an ML-based tool, by Google Research {{Unix commands}} {{Plan 9 commands}} {{DEFAULTSORT:File (Command)}} [[Category:Standard Unix programs]] [[Category:Unix SUS2008 utilities]] [[Category:Plan 9 commands]] [[Category:IBM i Qshell commands]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Annotated link
(
edit
)
Template:Cite mailing list
(
edit
)
Template:Cite web
(
edit
)
Template:Code
(
edit
)
Template:Infobox
(
edit
)
Template:Infobox software
(
edit
)
Template:Lowercase title
(
edit
)
Template:Main other
(
edit
)
Template:Man
(
edit
)
Template:Plan 9 commands
(
edit
)
Template:Redirects here
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Sister project
(
edit
)
Template:Template other
(
edit
)
Template:Unix commands
(
edit
)
Template:Webarchive
(
edit
)
Template:Wikibooks
(
edit
)