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