1 .\" $File: file.man,v 1.94 2011/04/20 19:08:44 christos Exp $
7 .Nd determine file type
11 .Op Fl bchiklLNnprsvz0
18 .Op Fl m Ar magicfiles
23 .Op Fl m Ar magicfiles
27 This manual page documents version __VERSION__ of the
32 tests each argument in an attempt to classify it.
33 There are three sets of tests, performed in this order:
34 filesystem tests, magic tests, and language tests.
37 test that succeeds causes the file type to be printed.
39 The type printed will usually contain one of the words
41 (the file contains only
42 printing characters and a few common control
43 characters and is probably safe to read on an
47 (the file contains the result of compiling a program
48 in a form understandable to some
53 meaning anything else (data is usually
56 Exceptions are well-known file formats (core files, tar archives)
57 that are known to contain binary data.
58 When modifying magic files or the program itself, make sure to
59 .Em "preserve these keywords" .
60 Users depend on knowing that all the readable files in a directory
64 Don't do as Berkeley did and change
65 .Sq shell commands text
69 The filesystem tests are based on examining the return from a
72 The program checks to see if the file is empty,
73 or if it's some sort of special file.
74 Any known file types appropriate to the system you are running on
75 (sockets, symbolic links, or named pipes (FIFOs) on those systems that
77 are intuited if they are defined in
78 the system header file
81 The magic tests are used to check for files with data in
82 particular fixed formats.
83 The canonical example of this is a binary executable (compiled program)
85 file, whose format is defined in
90 in the standard include directory.
93 stored in a particular place
94 near the beginning of the file that tells the
95 .Dv UNIX operating system
96 that the file is a binary executable, and which of several types thereof.
99 has been applied by extension to data files.
100 Any file with some invariant identifier at a small fixed
101 offset into the file can usually be described in this way.
102 The information identifying these files is read from the compiled
105 or the files in the directory
107 if the compiled file does not exist.
112 exists, it will be used in preference to the system magic files.
114 If a file does not match any of the entries in the magic file,
115 it is examined to see if it seems to be a text file.
116 ASCII, ISO-8859-x, non-ISO 8-bit extended-ASCII character sets
117 (such as those used on Macintosh and IBM PC systems),
118 UTF-8-encoded Unicode, UTF-16-encoded Unicode, and EBCDIC
119 character sets can be distinguished by the different
120 ranges and sequences of bytes that constitute printable text
122 If a file passes any of these tests, its character set is reported.
123 ASCII, ISO-8859-x, UTF-8, and extended-ASCII files are identified
126 because they will be mostly readable on nearly any terminal;
127 UTF-16 and EBCDIC are only
130 they contain text, it is text that will require translation
131 before it can be read.
134 will attempt to determine other characteristics of text-type files.
135 If the lines of a file are terminated by CR, CRLF, or NEL, instead
136 of the Unix-standard LF, this will be reported.
137 Files that contain embedded escape sequences or overstriking
138 will also be identified.
142 has determined the character set used in a text-type file,
144 attempt to determine in what language the file is written.
145 The language tests look for particular strings (cf.
147 ) that can appear anywhere in the first few blocks of a file.
148 For example, the keyword
150 indicates that the file is most likely a
152 input file, just as the keyword
154 indicates a C program.
155 These tests are less reliable than the previous
156 two groups, so they are performed last.
157 The language test routines also test for some miscellany
162 Any file that cannot be identified as having been written
163 in any of the character sets listed above is simply said to be
166 .Bl -tag -width indent
168 Do not prepend filenames to output lines (brief mode).
172 output file that contains a pre-parsed version of the magic file or directory.
173 .It Fl c , -checking-printout
174 Cause a checking printout of the parsed form of the magic file.
175 This is usually used in conjunction with the
177 flag to debug a new magic file before installing it.
178 .It Fl e , -exclude Ar testname
179 Exclude the test named in
181 from the list of tests made to determine the file type.
182 Valid test names are:
183 .Bl -tag -width compress
186 application type (only on EMX).
188 Various types of text files (this test will try to guess the text encoding, irrespective of the setting of the
192 Different text encodings for soft magic tests.
194 Looks for known tokens inside text files.
196 Prints details of Compound Document Files.
198 Checks for, and looks inside, compressed files.
200 Prints ELF file details.
202 Consults magic files.
206 .It Fl F , -separator Ar separator
207 Use the specified string as the separator between the filename and the
208 file result returned.
211 .It Fl f , -files-from Ar namefile
212 Read the names of the files to be examined from
215 before the argument list.
218 or at least one filename argument must be present;
219 to test the standard input, use
221 as a filename argument.
222 .It Fl h , -no-dereference
223 option causes symlinks not to be followed
224 (on systems that support symbolic links).
225 This is the default if the environment variable
229 Causes the file command to output mime type strings rather than the more
230 traditional human readable ones.
232 .Sq text/plain; charset=us-ascii
235 In order for this option to work, file changes the way
236 it handles files recognized by the command itself (such as many of the
237 text file types, directories etc), and makes use of an alternative
240 (See the FILES section, below).
241 .It Fl -mime-type , -mime-encoding
244 but print only the specified element(s).
245 .It Fl k , -keep-going
246 Don't stop at the first match, keep going.
247 Subsequent matches will be
251 (If you want a newline, see the
255 Print information about the strength of each magic pattern.
256 .It Fl L , -dereference
257 option causes symlinks to be followed, as the like-named option in
259 (on systems that support symbolic links).
260 This is the default if the environment variable
264 Shows sorted patterns list in the order which is used for the matching.
265 .It Fl m , -magic-file Ar magicfiles
266 Specify an alternate list of files and directories containing magic.
267 This can be a single item, or a colon-separated list.
268 If a compiled magic file is found alongside a file or directory, it will be used instead.
270 Don't pad filenames so that they align in the output.
271 .It Fl n , -no-buffer
272 Force stdout to be flushed after checking each file.
273 This is only useful if checking a list of files.
274 It is intended to be used by programs that want filetype output from a pipe.
275 .It Fl p , -preserve-date
276 On systems that support
280 attempt to preserve the access time of files analyzed, to pretend that
284 Don't translate unprintable characters to \eooo.
287 translates unprintable characters to their octal representation.
288 .It Fl s , -special-files
291 only attempts to read and determine the type of argument files which
293 reports are ordinary files.
294 This prevents problems, because reading special files may have peculiar
300 to also read argument files which are block or character special files.
301 This is useful for determining the filesystem types of the data in raw
302 disk partitions, which are block special files.
303 This option also causes
305 to disregard the file size as reported by
307 since on some systems it reports a zero size for raw disk partitions.
309 Print the version of the program and exit.
310 .It Fl z , -uncompress
311 Try to look inside compressed files.
313 Output a null character
315 after the end of the filename.
319 This does not affect the separator which is still printed.
321 Print a help message and exit.
324 .Bl -tag -width __MAGIC__.mgc -compact
326 Default compiled list of magic.
328 Directory containing default magic files.
331 The environment variable
333 can be used to set the default magic file name.
334 If that variable is set, then
336 will not attempt to open
341 to the value of this variable as appropriate.
344 has to exist in order for
347 The environment variable
349 controls (on systems that support symbolic links), whether
351 will attempt to follow symlinks or not.
354 follows symlink, otherwise it does not.
355 This is also controlled by the
361 .Xr magic __FSECTION__ ,
366 .Sh STANDARDS CONFORMANCE
367 This program is believed to exceed the System V Interface Definition
368 of FILE(CMD), as near as one can determine from the vague language
370 Its behavior is mostly compatible with the System V program of the same name.
371 This version knows more magic, however, so it will produce
372 different (albeit more accurate) output in many cases.
373 .\" URL: http://www.opengroup.org/onlinepubs/009695399/utilities/file.html
375 The one significant difference
376 between this version and System V
377 is that this version treats any white space
378 as a delimiter, so that spaces in pattern strings must be escaped.
380 .Bd -literal -offset indent
381 >10 string language impress\ (imPRESS data)
384 in an existing magic file would have to be changed to
385 .Bd -literal -offset indent
386 >10 string language\e impress (imPRESS data)
389 In addition, in this version, if a pattern string contains a backslash,
392 .Bd -literal -offset indent
393 0 string \ebegindata Andrew Toolkit document
396 in an existing magic file would have to be changed to
397 .Bd -literal -offset indent
398 0 string \e\ebegindata Andrew Toolkit document
401 SunOS releases 3.2 and later from Sun Microsystems include a
403 command derived from the System V one, but with some extensions.
404 My version differs from Sun's only in minor ways.
405 It includes the extension of the
409 .Bd -literal -offset indent
410 >16 long&0x7fffffff >0 not stripped
413 The magic file entries have been collected from various sources,
414 mainly USENET, and contributed by various authors.
415 Christos Zoulas (address below) will collect additional
416 or corrected magic file entries.
417 A consolidation of magic file entries
418 will be distributed periodically.
420 The order of entries in the magic file is significant.
421 Depending on what system you are using, the order that
422 they are put together may be incorrect.
425 command uses a magic file,
426 keep the old magic file around for comparison purposes
428 .Pa __MAGIC__.orig ).
430 .Bd -literal -offset indent
431 $ file file.c file /dev/{wd0a,hda}
432 file.c: C program text
433 file: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
434 dynamically linked (uses shared libs), stripped
435 /dev/wd0a: block special (0/0)
436 /dev/hda: block special (3/0)
438 $ file -s /dev/wd0{b,d}
440 /dev/wd0d: x86 boot sector
442 $ file -s /dev/hda{,1,2,3,4,5,6,7,8,9,10}
443 /dev/hda: x86 boot sector
444 /dev/hda1: Linux/i386 ext2 filesystem
445 /dev/hda2: x86 boot sector
446 /dev/hda3: x86 boot sector, extended partition table
447 /dev/hda4: Linux/i386 ext2 filesystem
448 /dev/hda5: Linux/i386 swap file
449 /dev/hda6: Linux/i386 swap file
450 /dev/hda7: Linux/i386 swap file
451 /dev/hda8: Linux/i386 swap file
455 $ file -i file.c file /dev/{wd0a,hda}
457 file: application/x-executable
458 /dev/hda: application/x-not-regular-file
459 /dev/wd0a: application/x-not-regular-file
466 .Dv UNIX since at least Research Version 4
467 (man page dated November, 1973).
468 The System V version introduced one significant major change:
469 the external list of magic types.
470 This slowed the program down slightly but made it a lot more flexible.
472 This program, based on the System V version,
473 was written by Ian Darwin
474 .Aq ian@darwinsys.com
475 without looking at anybody else's source code.
477 John Gilmore revised the code extensively, making it better than
479 Geoff Collyer found several inadequacies
480 and provided some magic file entries.
481 Contributions by the `&' operator by Rob McMahon
482 .Aq cudcv@warwick.ac.uk ,
487 made many changes from 1993 to the present.
489 Primary development and maintenance from 1990 to the present by
491 .Aq christos@astron.com .
493 Altered by Chris Lowth
494 .Aq chris@lowth.com ,
497 option to output mime type strings, using an alternative
498 magic file and internal logic.
500 Altered by Eric Fischer
502 July, 2000, to identify character codes and attempt to identify the languages
505 Altered by Reuben Thomas
507 2007-2011, to improve MIME support, merge MIME and non-MIME magic,
508 support directories as well as files of magic, apply many bug fixes,
509 update and fix a lot of magic, improve the build system, improve the
510 documentation, and rewrite the Python bindings in pure Python.
512 The list of contributors to the
514 directory (magic files)
515 is too long to include here.
516 You know who you are; thank you.
517 Many contributors are listed in the source files.
519 Copyright (c) Ian F. Darwin, Toronto, Canada, 1986-1999.
520 Covered by the standard Berkeley Software Distribution copyright; see the file
521 COPYING in the source distribution.
527 were written by John Gilmore from his public-domain
529 program, and are not covered by the above license.
532 returns 0 on success, and non-zero on error.
535 Please report bugs and send patches to the bug tracker at
536 .Pa http://bugs.gw.com/
537 or the mailing list at
541 Fix output so that tests for MIME and APPLE flags are not needed all
542 over the place, and actual output is only done in one place. This
543 needs a design. Suggestion: push possible outputs on to a list, then
544 pick the last-pushed (most specific, one hopes) value at the end, or
545 use a default if the list is empty. This should not slow down evaluation.
547 Continue to squash all magic bugs. See Debian BTS for a good source.
549 Store arbitrarily long strings, for example for %s patterns, so that
550 they can be printed out. Fixes Debian bug #271672. Would require more
551 complex store/load code in apprentice.
553 Add syntax for relative offsets after current level (Debian bug #466037).
555 Make file -ki work, i.e. give multiple MIME types.
557 Add a zip library so we can peek inside Office2007 documents to
558 figure out what they are.
560 Don't complain when ~/.magic is not compiled.
562 Add an option to print URLs for the sources of the file descriptions.
564 You can obtain the original author's latest version by anonymous FTP
568 .Dv /pub/file/file-X.YZ.tar.gz