1 .\" $File: file.man,v 1.138 2019/10/15 18:00:40 christos Exp $
7 .Nd determine file type
11 .Op Fl bcdEhiklLNnprsSvzZ0
14 .Op Fl Fl mime-encoding
19 .Op Fl m Ar magicfiles
20 .Op Fl P Ar name=value
25 .Op Fl m Ar magicfiles
29 This manual page documents version __VERSION__ of the
34 tests each argument in an attempt to classify it.
35 There are three sets of tests, performed in this order:
36 filesystem tests, magic tests, and language tests.
39 test that succeeds causes the file type to be printed.
41 The type printed will usually contain one of the words
43 (the file contains only
44 printing characters and a few common control
45 characters and is probably safe to read on an
49 (the file contains the result of compiling a program
50 in a form understandable to some
55 meaning anything else (data is usually
58 Exceptions are well-known file formats (core files, tar archives)
59 that are known to contain binary data.
60 When modifying magic files or the program itself, make sure to
61 .Em "preserve these keywords" .
62 Users depend on knowing that all the readable files in a directory
66 Don't do as Berkeley did and change
67 .Dq shell commands text
71 The filesystem tests are based on examining the return from a
74 The program checks to see if the file is empty,
75 or if it's some sort of special file.
76 Any known file types appropriate to the system you are running on
77 (sockets, symbolic links, or named pipes (FIFOs) on those systems that
79 are intuited if they are defined in the system header file
82 The magic tests are used to check for files with data in
83 particular fixed formats.
84 The canonical example of this is a binary executable (compiled program)
86 file, whose format is defined in
91 in the standard include directory.
94 stored in a particular place
95 near the beginning of the file that tells the
98 that the file is a binary executable, and which of several types thereof.
101 has been applied by extension to data files.
102 Any file with some invariant identifier at a small fixed
103 offset into the file can usually be described in this way.
104 The information identifying these files is read from the compiled
107 or the files in the directory
109 if the compiled file does not exist.
114 exists, it will be used in preference to the system magic files.
116 If a file does not match any of the entries in the magic file,
117 it is examined to see if it seems to be a text file.
118 ASCII, ISO-8859-x, non-ISO 8-bit extended-ASCII character sets
119 (such as those used on Macintosh and IBM PC systems),
120 UTF-8-encoded Unicode, UTF-16-encoded Unicode, and EBCDIC
121 character sets can be distinguished by the different
122 ranges and sequences of bytes that constitute printable text
124 If a file passes any of these tests, its character set is reported.
125 ASCII, ISO-8859-x, UTF-8, and extended-ASCII files are identified
128 because they will be mostly readable on nearly any terminal;
129 UTF-16 and EBCDIC are only
132 they contain text, it is text that will require translation
133 before it can be read.
136 will attempt to determine other characteristics of text-type files.
137 If the lines of a file are terminated by CR, CRLF, or NEL, instead
138 of the Unix-standard LF, this will be reported.
139 Files that contain embedded escape sequences or overstriking
140 will also be identified.
144 has determined the character set used in a text-type file,
146 attempt to determine in what language the file is written.
147 The language tests look for particular strings (cf.
149 that can appear anywhere in the first few blocks of a file.
150 For example, the keyword
152 indicates that the file is most likely a
154 input file, just as the keyword
156 indicates a C program.
157 These tests are less reliable than the previous
158 two groups, so they are performed last.
159 The language test routines also test for some miscellany
162 archives, JSON files).
164 Any file that cannot be identified as having been written
165 in any of the character sets listed above is simply said to be
168 .Bl -tag -width indent
170 Causes the file command to output the file type and creator code as
171 used by older MacOS versions.
172 The code consists of eight letters,
173 the first describing the file type, the latter the creator.
174 This option works properly only for file formats that have the
175 apple-style output defined.
176 .It Fl b , Fl Fl brief
177 Do not prepend filenames to output lines (brief mode).
178 .It Fl C , Fl Fl compile
181 output file that contains a pre-parsed version of the magic file or directory.
182 .It Fl c , Fl Fl checking-printout
183 Cause a checking printout of the parsed form of the magic file.
184 This is usually used in conjunction with the
186 flag to debug a new magic file before installing it.
188 Prints internal debugging information to stderr.
190 On filesystem errors (file not found etc), instead of handling the error
191 as regular output as POSIX mandates and keep going, issue an error message
193 .It Fl e , Fl Fl exclude Ar testname
194 Exclude the test named in
196 from the list of tests made to determine the file type.
197 Valid test names are:
198 .Bl -tag -width compress
201 application type (only on EMX).
203 Various types of text files (this test will try to guess the text
204 encoding, irrespective of the setting of the
208 Different text encodings for soft magic tests.
210 Ignored for backwards compatibility.
212 Prints details of Compound Document Files.
214 Checks for, and looks inside, compressed files.
216 Checks Comma Separated Value files.
218 Prints ELF file details, provided soft magic tests are enabled and the
221 Examines JSON (RFC-7159) files by parsing them for compliance.
223 Consults magic files.
225 Examines tar files by verifying the checksum of the 512 byte tar header.
226 Excluding this test can provide more detailed content description by using
227 the soft magic method.
233 Print a slash-separated list of valid extensions for the file type found.
234 .It Fl F , Fl Fl separator Ar separator
235 Use the specified string as the separator between the filename and the
236 file result returned.
239 .It Fl f , Fl Fl files-from Ar namefile
240 Read the names of the files to be examined from
243 before the argument list.
246 or at least one filename argument must be present;
247 to test the standard input, use
249 as a filename argument.
252 is unwrapped and the enclosed filenames are processed when this option is
253 encountered and before any further options processing is done.
254 This allows one to process multiple lists of files with different command line
255 arguments on the same
258 Thus if you want to set the delimiter, you need to do it before you specify
259 the list of files, like:
260 .Dq Fl F Ar @ Fl f Ar namefile ,
262 .Dq Fl f Ar namefile Fl F Ar @ .
263 .It Fl h , Fl Fl no-dereference
264 option causes symlinks not to be followed
265 (on systems that support symbolic links).
266 This is the default if the environment variable
269 .It Fl i , Fl Fl mime
270 Causes the file command to output mime type strings rather than the more
271 traditional human readable ones.
273 .Sq text/plain; charset=us-ascii
276 .It Fl Fl mime-type , Fl Fl mime-encoding
279 but print only the specified element(s).
280 .It Fl k , Fl Fl keep-going
281 Don't stop at the first match, keep going.
282 Subsequent matches will be
286 (If you want a newline, see the
289 The magic pattern with the highest strength (see the
292 .It Fl l , Fl Fl list
293 Shows a list of patterns and their strength sorted descending by
294 .Xr magic __FSECTION__
296 which is used for the matching (see also the
299 .It Fl L , Fl Fl dereference
300 option causes symlinks to be followed, as the like-named option in
302 (on systems that support symbolic links).
303 This is the default if the environment variable
306 .It Fl m , Fl Fl magic-file Ar magicfiles
307 Specify an alternate list of files and directories containing magic.
308 This can be a single item, or a colon-separated list.
309 If a compiled magic file is found alongside a file or directory,
310 it will be used instead.
311 .It Fl N , Fl Fl no-pad
312 Don't pad filenames so that they align in the output.
313 .It Fl n , Fl Fl no-buffer
314 Force stdout to be flushed after checking each file.
315 This is only useful if checking a list of files.
316 It is intended to be used by programs that want filetype output from a pipe.
317 .It Fl p , Fl Fl preserve-date
318 On systems that support
322 attempt to preserve the access time of files analyzed, to pretend that
325 .It Fl P , Fl Fl parameter Ar name=value
326 Set 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
338 Don't translate unprintable characters to \eooo.
341 translates unprintable characters to their octal representation.
342 .It Fl s , Fl Fl special-files
345 only attempts to read and determine the type of argument files which
347 reports are ordinary files.
348 This prevents problems, because reading special files may have peculiar
354 to also read argument files which are block or character special files.
355 This is useful for determining the filesystem types of the data in raw
356 disk partitions, which are block special files.
357 This option also causes
359 to disregard the file size as reported by
361 since on some systems it reports a zero size for raw disk partitions.
362 .It Fl S , Fl Fl no-sandbox
363 On systems where libseccomp
364 .Pa ( https://github.com/seccomp/libseccomp )
367 flag disables sandboxing which is enabled by default.
368 This option is needed for file to execute external decompressing programs,
371 flag is specified and the built-in decompressors are not available.
372 On systems where sandboxing is not available, this option has no effect.
373 .It Fl v , Fl Fl version
374 Print the version of the program and exit.
375 .It Fl z , Fl Fl uncompress
376 Try to look inside compressed files.
377 .It Fl Z , Fl Fl uncompress-noreport
378 Try to look inside compressed files, but report information about the contents
379 only not the compression.
380 .It Fl 0 , Fl Fl print0
381 Output a null character
383 after the end of the filename.
387 This does not affect the separator, which is still printed.
389 If this option is repeated more than once, then
391 prints just the filename followed by a NUL followed by the description
392 (or ERROR: text) followed by a second NUL for each entry.
394 Print a help message and exit.
397 The environment variable
399 can be used to set the default magic file name.
400 If that variable is set, then
402 will not attempt to open
407 to the value of this variable as appropriate.
408 The environment variable
410 controls (on systems that support symbolic links), whether
412 will attempt to follow symlinks or not.
415 follows symlink, otherwise it does not.
416 This is also controlled by the
422 .Bl -tag -width __MAGIC__.mgc -compact
424 Default compiled list of magic.
426 Directory containing default magic files.
432 if the operation was successful or
434 if an error was encountered.
435 The following errors cause diagnostic messages, but don't affect the program
436 exit code (as POSIX requires), unless
439 .Bl -bullet -compact -offset indent
441 A file cannot be found
443 There is no permission to read a file
445 The file type cannot be determined
448 .Bd -literal -offset indent
449 $ file file.c file /dev/{wd0a,hda}
450 file.c: C program text
451 file: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
452 dynamically linked (uses shared libs), stripped
453 /dev/wd0a: block special (0/0)
454 /dev/hda: block special (3/0)
456 $ file -s /dev/wd0{b,d}
458 /dev/wd0d: x86 boot sector
460 $ file -s /dev/hda{,1,2,3,4,5,6,7,8,9,10}
461 /dev/hda: x86 boot sector
462 /dev/hda1: Linux/i386 ext2 filesystem
463 /dev/hda2: x86 boot sector
464 /dev/hda3: x86 boot sector, extended partition table
465 /dev/hda4: Linux/i386 ext2 filesystem
466 /dev/hda5: Linux/i386 swap file
467 /dev/hda6: Linux/i386 swap file
468 /dev/hda7: Linux/i386 swap file
469 /dev/hda8: Linux/i386 swap file
473 $ file -i file.c file /dev/{wd0a,hda}
475 file: application/x-executable
476 /dev/hda: application/x-not-regular-file
477 /dev/wd0a: application/x-not-regular-file
484 .Xr magic __FSECTION__ ,
486 .Sh STANDARDS CONFORMANCE
487 This program is believed to exceed the System V Interface Definition
488 of FILE(CMD), as near as one can determine from the vague language
490 Its behavior is mostly compatible with the System V program of the same name.
491 This version knows more magic, however, so it will produce
492 different (albeit more accurate) output in many cases.
493 .\" URL: http://www.opengroup.org/onlinepubs/009695399/utilities/file.html
495 The one significant difference
496 between this version and System V
497 is that this version treats any white space
498 as a delimiter, so that spaces in pattern strings must be escaped.
500 .Bd -literal -offset indent
501 \*[Gt]10 string language impress\ (imPRESS data)
504 in an existing magic file would have to be changed to
505 .Bd -literal -offset indent
506 \*[Gt]10 string language\e impress (imPRESS data)
509 In addition, in this version, if a pattern string contains a backslash,
512 .Bd -literal -offset indent
513 0 string \ebegindata Andrew Toolkit document
516 in an existing magic file would have to be changed to
517 .Bd -literal -offset indent
518 0 string \e\ebegindata Andrew Toolkit document
521 SunOS releases 3.2 and later from Sun Microsystems include a
523 command derived from the System V one, but with some extensions.
524 This version differs from Sun's only in minor ways.
525 It includes the extension of the
529 .Bd -literal -offset indent
530 \*[Gt]16 long\*[Am]0x7fffffff \*[Gt]0 not stripped
533 On systems where libseccomp
534 .Pa ( https://github.com/seccomp/libseccomp )
537 is enforces limiting system calls to only the ones necessary for the
538 operation of the program.
539 This enforcement does not provide any security benefit when
541 is asked to decompress input files running external programs with
545 To enable execution of external decompressors, one needs to disable
550 The magic file entries have been collected from various sources,
551 mainly USENET, and contributed by various authors.
552 Christos Zoulas (address below) will collect additional
553 or corrected magic file entries.
554 A consolidation of magic file entries
555 will be distributed periodically.
557 The order of entries in the magic file is significant.
558 Depending on what system you are using, the order that
559 they are put together may be incorrect.
562 command uses a magic file,
563 keep the old magic file around for comparison purposes
565 .Pa __MAGIC__.orig ) .
570 .Dv UNIX since at least Research Version 4
571 (man page dated November, 1973).
572 The System V version introduced one significant major change:
573 the external list of magic types.
574 This slowed the program down slightly but made it a lot more flexible.
576 This program, based on the System V version,
577 was written by Ian Darwin
578 .Aq ian@darwinsys.com
579 without looking at anybody else's source code.
581 John Gilmore revised the code extensively, making it better than
583 Geoff Collyer found several inadequacies
584 and provided some magic file entries.
587 operator by Rob McMahon,
588 .Aq cudcv@warwick.ac.uk ,
593 made many changes from 1993 to the present.
595 Primary development and maintenance from 1990 to the present by
597 .Aq christos@astron.com .
599 Altered by Chris Lowth
600 .Aq chris@lowth.com ,
603 option to output mime type strings, using an alternative
604 magic file and internal logic.
606 Altered by Eric Fischer
609 to identify character codes and attempt to identify the languages
612 Altered by Reuben Thomas
614 2007-2011, to improve MIME support, merge MIME and non-MIME magic,
615 support directories as well as files of magic, apply many bug fixes,
616 update and fix a lot of magic, improve the build system, improve the
617 documentation, and rewrite the Python bindings in pure Python.
619 The list of contributors to the
621 directory (magic files)
622 is too long to include here.
623 You know who you are; thank you.
624 Many contributors are listed in the source files.
626 Copyright (c) Ian F. Darwin, Toronto, Canada, 1986-1999.
627 Covered by the standard Berkeley Software Distribution copyright; see the file
628 COPYING in the source distribution.
634 were written by John Gilmore from his public-domain
636 program, and are not covered by the above license.
638 Please report bugs and send patches to the bug tracker at
639 .Pa https://bugs.astron.com/
640 or the mailing list at
643 .Pa https://mailman.astron.com/mailman/listinfo/file
646 Fix output so that tests for MIME and APPLE flags are not needed all
647 over the place, and actual output is only done in one place.
649 Suggestion: push possible outputs on to a list, then pick the
650 last-pushed (most specific, one hopes) value at the end, or
651 use a default if the list is empty.
652 This should not slow down evaluation.
656 and printing \e012- between entries is clumsy and complicated; refactor
659 Some of the encoding logic is hard-coded in encoding.c and can be moved
660 to the magic files if we had a !:charset annotation
662 Continue to squash all magic bugs.
663 See Debian BTS for a good source.
665 Store arbitrarily long strings, for example for %s patterns, so that
666 they can be printed out.
667 Fixes Debian bug #271672.
668 This can be done by allocating strings in a string pool, storing the
669 string pool at the end of the magic file and converting all the string
670 pointers to relative offsets from the string pool.
672 Add syntax for relative offsets after current level (Debian bug #466037).
674 Make file -ki work, i.e. give multiple MIME types.
676 Add a zip library so we can peek inside Office2007 documents to
677 print more details about their contents.
679 Add an option to print URLs for the sources of the file descriptions.
681 Combine script searches and add a way to map executable names to MIME
682 types (e.g. have a magic value for !:mime which causes the resulting
683 string to be looked up in a table).
684 This would avoid adding the same magic repeatedly for each new
685 hash-bang interpreter.
687 When a file descriptor is available, we can skip and adjust the buffer
688 instead of the hacky buffer management we do now.
694 to check for consistency at compile time (duplicate
697 pointing to undefined
704 more efficient by keeping a sorted list of names.
705 Special-case ^ to flip endianness in the parser so that it does not
706 have to be escaped, and document it.
708 If the offsets specified internally in the file exceed the buffer size
711 variable in file.h), then we don't seek to that offset, but we give up.
712 It would be better if buffer managements was done when the file descriptor
713 is available so move around the file.
714 One must be careful though because this has performance (and thus security
717 You can obtain the original author's latest version by anonymous FTP
721 .Pa /pub/file/file-X.YZ.tar.gz .