1.\" $NetBSD: file.1,v 1.14 2014/06/13 02:08:06 christos Exp $ 2.\" 3.\" $File: file.man,v 1.106 2014/03/07 23:11:51 christos Exp $ 4.Dd January 30, 2014 5.Dt FILE 1 6.Os 7.Sh NAME 8.Nm file 9.Nd determine file type 10.Sh SYNOPSIS 11.Nm 12.Bk -words 13.Op Fl bcEhiklLNnprsvz0 14.Op Fl Fl apple 15.Op Fl Fl mime-encoding 16.Op Fl Fl mime-type 17.Op Fl e Ar testname 18.Op Fl F Ar separator 19.Op Fl f Ar namefile 20.Op Fl m Ar magicfiles 21.Ar 22.Ek 23.Nm 24.Fl C 25.Op Fl m Ar magicfiles 26.Nm 27.Op Fl Fl help 28.Sh DESCRIPTION 29This manual page documents version 5.19 of the 30.Nm 31command. 32.Pp 33.Nm 34tests each argument in an attempt to classify it. 35There are three sets of tests, performed in this order: 36filesystem tests, magic tests, and language tests. 37The 38.Em first 39test that succeeds causes the file type to be printed. 40.Pp 41The type printed will usually contain one of the words 42.Em text 43(the file contains only 44printing characters and a few common control 45characters and is probably safe to read on an 46.Dv ASCII 47terminal), 48.Em executable 49(the file contains the result of compiling a program 50in a form understandable to some 51.Tn UNIX 52kernel or another), 53or 54.Em data 55meaning anything else (data is usually 56.Dq binary 57or non-printable). 58Exceptions are well-known file formats (core files, tar archives) 59that are known to contain binary data. 60When modifying magic files or the program itself, make sure to 61.Em "preserve these keywords" . 62Users depend on knowing that all the readable files in a directory 63have the word 64.Dq text 65printed. 66Don't do as Berkeley did and change 67.Dq shell commands text 68to 69.Dq shell script . 70.Pp 71The filesystem tests are based on examining the return from a 72.Xr stat 2 73system call. 74The program checks to see if the file is empty, 75or if it's some sort of special file. 76Any known file types appropriate to the system you are running on 77(sockets, symbolic links, or named pipes (FIFOs) on those systems that 78implement them) 79are intuited if they are defined in the system header file 80.In sys/stat.h . 81.Pp 82The magic tests are used to check for files with data in 83particular fixed formats. 84The canonical example of this is a binary executable (compiled program) 85.Dv a.out 86file, whose format is defined in 87.In elf.h , 88.In a.out.h 89and possibly 90.In exec.h 91in the standard include directory. 92These files have a 93.Dq "magic number" 94stored in a particular place 95near the beginning of the file that tells the 96.Tn UNIX 97operating system 98that the file is a binary executable, and which of several types thereof. 99The concept of a 100.Dq "magic" 101has been applied by extension to data files. 102Any file with some invariant identifier at a small fixed 103offset into the file can usually be described in this way. 104The information identifying these files is read from the compiled 105magic file 106.Pa /usr/share/misc/magic.mgc , 107or the files in the directory 108.Pa /usr/share/misc/magic 109if the compiled file does not exist. 110In addition, if 111.Pa $HOME/.magic.mgc 112or 113.Pa $HOME/.magic 114exists, it will be used in preference to the system magic files. 115.Pp 116If a file does not match any of the entries in the magic file, 117it is examined to see if it seems to be a text file. 118ASCII, ISO-8859-x, non-ISO 8-bit extended-ASCII character sets 119(such as those used on Macintosh and IBM PC systems), 120UTF-8-encoded Unicode, UTF-16-encoded Unicode, and EBCDIC 121character sets can be distinguished by the different 122ranges and sequences of bytes that constitute printable text 123in each set. 124If a file passes any of these tests, its character set is reported. 125ASCII, ISO-8859-x, UTF-8, and extended-ASCII files are identified 126as 127.Dq text 128because they will be mostly readable on nearly any terminal; 129UTF-16 and EBCDIC are only 130.Dq character data 131because, while 132they contain text, it is text that will require translation 133before it can be read. 134In addition, 135.Nm 136will attempt to determine other characteristics of text-type files. 137If the lines of a file are terminated by CR, CRLF, or NEL, instead 138of the Unix-standard LF, this will be reported. 139Files that contain embedded escape sequences or overstriking 140will also be identified. 141.Pp 142Once 143.Nm 144has determined the character set used in a text-type file, 145it will 146attempt to determine in what language the file is written. 147The language tests look for particular strings (cf. 148.In names.h ) 149that can appear anywhere in the first few blocks of a file. 150For example, the keyword 151.Em .br 152indicates that the file is most likely a 153.Xr troff 1 154input file, just as the keyword 155.Em struct 156indicates a C program. 157These tests are less reliable than the previous 158two groups, so they are performed last. 159The language test routines also test for some miscellany 160(such as 161.Xr tar 1 162archives). 163.Pp 164Any file that cannot be identified as having been written 165in any of the character sets listed above is simply said to be 166.Dq data . 167.Sh OPTIONS 168.Bl -tag -width indent 169.It Fl Fl apple 170Causes the file command to output the file type and creator code as 171used by older MacOS versions. The code consists of eight letters, 172the first describing the file type, the latter the creator. 173.It Fl b , Fl Fl brief 174Do not prepend filenames to output lines (brief mode). 175.It Fl C , Fl Fl compile 176Write a 177.Pa magic.mgc 178output file that contains a pre-parsed version of the magic file or directory. 179.It Fl c , Fl Fl checking-printout 180Cause a checking printout of the parsed form of the magic file. 181This is usually used in conjunction with the 182.Fl m 183flag to debug a new magic file before installing it. 184.It Fl E 185On filesystem errors (file not found etc), instead of handling the error 186as regular output as POSIX mandates and keep going, issue an error message 187and exit. 188.It Fl e , Fl Fl exclude Ar testname 189Exclude the test named in 190.Ar testname 191from the list of tests made to determine the file type. 192Valid test names are: 193.Bl -tag -width compress 194.It apptype 195.Dv EMX 196application type (only on EMX). 197.It ascii 198Various types of text files (this test will try to guess the text 199encoding, irrespective of the setting of the 200.Sq encoding 201option). 202.It encoding 203Different text encodings for soft magic tests. 204.It tokens 205Ignored for backwards compatibility. 206.It cdf 207Prints details of Compound Document Files. 208.It compress 209Checks for, and looks inside, compressed files. 210.It elf 211Prints ELF file details. 212.It soft 213Consults magic files. 214.It tar 215Examines tar files. 216.El 217.It Fl F , Fl Fl separator Ar separator 218Use the specified string as the separator between the filename and the 219file result returned. 220Defaults to 221.Sq \&: . 222.It Fl f , Fl Fl files-from Ar namefile 223Read the names of the files to be examined from 224.Ar namefile 225(one per line) 226before the argument list. 227Either 228.Ar namefile 229or at least one filename argument must be present; 230to test the standard input, use 231.Sq - 232as a filename argument. 233Please note that 234.Ar namefile 235is unwrapped and the enclosed filenames are processed when this option is 236encountered and before any further options processing is done. 237This allows one to process multiple lists of files with different command line 238arguments on the same 239.Nm 240invocation. 241Thus if you want to set the delimiter, you need to do it before you specify 242the list of files, like: 243.Dq Fl F Ar @ Fl f Ar namefile , 244instead of: 245.Dq Fl f Ar namefile Fl F Ar @ . 246.It Fl h , Fl Fl no-dereference 247option causes symlinks not to be followed 248(on systems that support symbolic links). 249This is the default if the environment variable 250.Dv POSIXLY_CORRECT 251is not defined. 252.It Fl i , Fl Fl mime 253Causes the file command to output mime type strings rather than the more 254traditional human readable ones. 255Thus it may say 256.Sq text/plain; charset=us-ascii 257rather than 258.Dq ASCII text . 259.It Fl Fl mime-type , Fl Fl mime-encoding 260Like 261.Fl i , 262but print only the specified element(s). 263.It Fl k , Fl Fl keep-going 264Don't stop at the first match, keep going. 265Subsequent matches will be 266have the string 267.Sq "\[rs]012\- " 268prepended. 269(If you want a newline, see the 270.Fl r 271option.) 272The magic pattern with the highest strength (see the 273.Fl l 274option) comes first. 275.It Fl l , Fl Fl list 276Shows a list of patterns and their strength sorted descending by 277.Xr magic 4 278strength 279which is used for the matching (see also the 280.Fl k 281option). 282.It Fl L , Fl Fl dereference 283option causes symlinks to be followed, as the like-named option in 284.Xr ls 1 285(on systems that support symbolic links). 286This is the default if the environment variable 287.Ev POSIXLY_CORRECT 288is defined. 289.It Fl m , Fl Fl magic-file Ar magicfiles 290Specify an alternate list of files and directories containing magic. 291This can be a single item, or a colon-separated list. 292If a compiled magic file is found alongside a file or directory, 293it will be used instead. 294.It Fl N , Fl Fl no-pad 295Don't pad filenames so that they align in the output. 296.It Fl n , Fl Fl no-buffer 297Force stdout to be flushed after checking each file. 298This is only useful if checking a list of files. 299It is intended to be used by programs that want filetype output from a pipe. 300.It Fl p , Fl Fl preserve-date 301On systems that support 302.Xr utime 3 303or 304.Xr utimes 2 , 305attempt to preserve the access time of files analyzed, to pretend that 306.Nm 307never read them. 308.It Fl r , Fl Fl raw 309Don't translate unprintable characters to \eooo. 310Normally 311.Nm 312translates unprintable characters to their octal representation. 313.It Fl s , Fl Fl special-files 314Normally, 315.Nm 316only attempts to read and determine the type of argument files which 317.Xr stat 2 318reports are ordinary files. 319This prevents problems, because reading special files may have peculiar 320consequences. 321Specifying the 322.Fl s 323option causes 324.Nm 325to also read argument files which are block or character special files. 326This is useful for determining the filesystem types of the data in raw 327disk partitions, which are block special files. 328This option also causes 329.Nm 330to disregard the file size as reported by 331.Xr stat 2 332since on some systems it reports a zero size for raw disk partitions. 333.It Fl v , Fl Fl version 334Print the version of the program and exit. 335.It Fl z , Fl Fl uncompress 336Try to look inside compressed files. 337.It Fl 0 , Fl Fl print0 338Output a null character 339.Sq \e0 340after the end of the filename. 341Nice to 342.Xr cut 1 343the output. 344This does not affect the separator, which is still printed. 345.It Fl -help 346Print a help message and exit. 347.El 348.Sh FILES 349.Bl -tag -width /usr/share/misc/magic.mgc -compact 350.It Pa /usr/share/misc/magic.mgc 351Default compiled list of magic. 352.It Pa /usr/share/misc/magic 353Directory containing default magic files. 354.El 355.Sh ENVIRONMENT 356The environment variable 357.Ev MAGIC 358can be used to set the default magic file name. 359If that variable is set, then 360.Nm 361will not attempt to open 362.Pa $HOME/.magic . 363.Nm 364adds 365.Dq Pa .mgc 366to the value of this variable as appropriate. 367However, 368.Pa file 369has to exist in order for 370.Pa file.mime 371to be considered. 372The environment variable 373.Ev POSIXLY_CORRECT 374controls (on systems that support symbolic links), whether 375.Nm 376will attempt to follow symlinks or not. 377If set, then 378.Nm 379follows symlink, otherwise it does not. 380This is also controlled by the 381.Fl L 382and 383.Fl h 384options. 385.Sh SEE ALSO 386.Xr magic 5 , 387.Xr hexdump 1 , 388.Xr od 1 , 389.Xr strings 1 , 390.Sh STANDARDS CONFORMANCE 391This program is believed to exceed the System V Interface Definition 392of FILE(CMD), as near as one can determine from the vague language 393contained therein. 394Its behavior is mostly compatible with the System V program of the same name. 395This version knows more magic, however, so it will produce 396different (albeit more accurate) output in many cases. 397.\" URL: http://www.opengroup.org/onlinepubs/009695399/utilities/file.html 398.Pp 399The one significant difference 400between this version and System V 401is that this version treats any white space 402as a delimiter, so that spaces in pattern strings must be escaped. 403For example, 404.Bd -literal -offset indent 405\*[Gt]10 string language impress\ (imPRESS data) 406.Ed 407.Pp 408in an existing magic file would have to be changed to 409.Bd -literal -offset indent 410\*[Gt]10 string language\e impress (imPRESS data) 411.Ed 412.Pp 413In addition, in this version, if a pattern string contains a backslash, 414it must be escaped. 415For example 416.Bd -literal -offset indent 4170 string \ebegindata Andrew Toolkit document 418.Ed 419.Pp 420in an existing magic file would have to be changed to 421.Bd -literal -offset indent 4220 string \e\ebegindata Andrew Toolkit document 423.Ed 424.Pp 425SunOS releases 3.2 and later from Sun Microsystems include a 426.Nm 427command derived from the System V one, but with some extensions. 428This version differs from Sun's only in minor ways. 429It includes the extension of the 430.Sq \*[Am] 431operator, used as, 432for example, 433.Bd -literal -offset indent 434\*[Gt]16 long\*[Am]0x7fffffff \*[Gt]0 not stripped 435.Ed 436.Sh MAGIC DIRECTORY 437The magic file entries have been collected from various sources, 438mainly USENET, and contributed by various authors. 439Christos Zoulas (address below) will collect additional 440or corrected magic file entries. 441A consolidation of magic file entries 442will be distributed periodically. 443.Pp 444The order of entries in the magic file is significant. 445Depending on what system you are using, the order that 446they are put together may be incorrect. 447If your old 448.Nm 449command uses a magic file, 450keep the old magic file around for comparison purposes 451(rename it to 452.Pa /usr/share/misc/magic.orig ) . 453.Sh EXAMPLES 454.Bd -literal -offset indent 455$ file file.c file /dev/{wd0a,hda} 456file.c: C program text 457file: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), 458 dynamically linked (uses shared libs), stripped 459/dev/wd0a: block special (0/0) 460/dev/hda: block special (3/0) 461 462$ file -s /dev/wd0{b,d} 463/dev/wd0b: data 464/dev/wd0d: x86 boot sector 465 466$ file -s /dev/hda{,1,2,3,4,5,6,7,8,9,10} 467/dev/hda: x86 boot sector 468/dev/hda1: Linux/i386 ext2 filesystem 469/dev/hda2: x86 boot sector 470/dev/hda3: x86 boot sector, extended partition table 471/dev/hda4: Linux/i386 ext2 filesystem 472/dev/hda5: Linux/i386 swap file 473/dev/hda6: Linux/i386 swap file 474/dev/hda7: Linux/i386 swap file 475/dev/hda8: Linux/i386 swap file 476/dev/hda9: empty 477/dev/hda10: empty 478 479$ file -i file.c file /dev/{wd0a,hda} 480file.c: text/x-c 481file: application/x-executable 482/dev/hda: application/x-not-regular-file 483/dev/wd0a: application/x-not-regular-file 484 485.Ed 486.Sh HISTORY 487There has been a 488.Nm 489command in every 490.Dv UNIX since at least Research Version 4 491(man page dated November, 1973). 492The System V version introduced one significant major change: 493the external list of magic types. 494This slowed the program down slightly but made it a lot more flexible. 495.Pp 496This program, based on the System V version, 497was written by Ian Darwin 498.Aq ian@darwinsys.com 499without looking at anybody else's source code. 500.Pp 501John Gilmore revised the code extensively, making it better than 502the first version. 503Geoff Collyer found several inadequacies 504and provided some magic file entries. 505Contributions by the 506.Sq \*[Am] 507operator by Rob McMahon, 508.Aq cudcv@warwick.ac.uk , 5091989. 510.Pp 511Guy Harris, 512.Aq guy@netapp.com , 513made many changes from 1993 to the present. 5141989. 515.Pp 516Primary development and maintenance from 1990 to the present by 517Christos Zoulas 518.Aq christos@astron.com . 519.Pp 520Altered by Chris Lowth 521.Aq chris@lowth.com , 5222000: handle the 523.Fl i 524option to output mime type strings, using an alternative 525magic file and internal logic. 526.Pp 527Altered by Eric Fischer 528.Aq enf@pobox.com , 529July, 2000, 530to identify character codes and attempt to identify the languages 531of non-ASCII files. 532.Pp 533Altered by Reuben Thomas 534.Aq rrt@sc3d.org , 5352007-2011, to improve MIME support, merge MIME and non-MIME magic, 536support directories as well as files of magic, apply many bug fixes, 537update and fix a lot of magic, improve the build system, improve the 538documentation, and rewrite the Python bindings in pure Python. 539.Pp 540The list of contributors to the 541.Sq magic 542directory (magic files) 543is too long to include here. 544You know who you are; thank you. 545Many contributors are listed in the source files. 546.Sh LEGAL NOTICE 547Copyright (c) Ian F. Darwin, Toronto, Canada, 1986-1999. 548Covered by the standard Berkeley Software Distribution copyright; see the file 549COPYING in the source distribution. 550.Pp 551The files 552.Pa tar.h 553and 554.Pa is_tar.c 555were written by John Gilmore from his public-domain 556.Xr tar 1 557program, and are not covered by the above license. 558.Sh RETURN CODE 559.Nm 560returns 0 on success, and non-zero on error. 561.Sh BUGS 562.Pp 563Please report bugs and send patches to the bug tracker at 564.Pa http://bugs.gw.com/ 565or the mailing list at 566.Aq file@mx.gw.com 567(visit 568.Pa http://mx.gw.com/mailman/listinfo/file 569first to subscribe). 570.Sh TODO 571.Pp 572Fix output so that tests for MIME and APPLE flags are not needed all 573over the place, and actual output is only done in one place. 574This needs a design. 575Suggestion: push possible outputs on to a list, then pick the 576last-pushed (most specific, one hopes) value at the end, or 577use a default if the list is empty. 578This should not slow down evaluation. 579.Pp 580Continue to squash all magic bugs. 581See Debian BTS for a good source. 582.Pp 583Store arbitrarily long strings, for example for %s patterns, so that 584they can be printed out. 585Fixes Debian bug #271672. 586Would require more complex store/load code in apprentice. 587.Pp 588Add syntax for relative offsets after current level (Debian bug #466037). 589.Pp 590Make file -ki work, i.e. give multiple MIME types. 591.Pp 592Add a zip library so we can peek inside Office2007 documents to 593figure out what they are. 594.Pp 595Add an option to print URLs for the sources of the file descriptions. 596.Pp 597Combine script searches and add a way to map executable names to MIME 598types (e.g. have a magic value for !:mime which causes the resulting 599string to be looked up in a table). 600This would avoid adding the same magic repeatedly for each new 601hash-bang interpreter. 602.Pp 603Fix 604.Dq name 605and 606.Dq use 607to check for consistency at compile time (duplicate 608.Dq name , 609.Dq use 610pointing to undefined 611.Dq name 612). 613Make 614.Dq name 615/ 616.Dq use 617more efficient by keeping a sorted list of names. 618Special-case ^ to flip endianness in the parser so that it does not 619have to be escaped, and document it. 620.Sh AVAILABILITY 621You can obtain the original author's latest version by anonymous FTP 622on 623.Pa ftp.astron.com 624in the directory 625.Pa /pub/file/file-X.YZ.tar.gz . 626