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