1 .\" $NetBSD: file.1,v 1.3 2009/05/08 17:34:34 christos Exp $
3 .\" $File: file.man,v 1.79 2008/11/06 22:49:08 rrt Exp $
9 .Nd determine file type
15 .Op Fl m Ar magicfiles
16 .Op Fl Fl mime-encoding
25 This manual page documents version 5.03 of the
30 tests each argument in an attempt to classify it.
31 There are three sets of tests, performed in this order:
32 filesystem tests, magic tests, and language tests.
35 test that succeeds causes the file type to be printed.
37 The type printed will usually contain one of the words
39 (the file contains only
40 printing characters and a few common control
41 characters and is probably safe to read on an
45 (the file contains the result of compiling a program
46 in a form understandable to some
51 meaning anything else (data is usually
54 Exceptions are well-known file formats (core files, tar archives)
55 that are known to contain binary data.
56 When modifying magic files or the program itself, make sure to
57 .Em "preserve these keywords" .
58 Users depend on knowing that all the readable files in a directory
62 Don't do as Berkeley did and change
63 .Dq shell commands text
67 The filesystem tests are based on examining the return from a
70 The program checks to see if the file is empty,
71 or if it's some sort of special file.
72 Any known file types appropriate to the system you are running on
73 (sockets, symbolic links, or named pipes (FIFOs) on those systems that
75 are intuited if they are defined in the system header file
78 The magic tests are used to check for files with data in
79 particular fixed formats.
80 The canonical example of this is a binary executable (compiled program)
82 file, whose format is defined in
87 in the standard include directory.
90 stored in a particular place
91 near the beginning of the file that tells the
94 that the file is a binary executable, and which of several types thereof.
97 has been applied by extension to data files.
98 Any file with some invariant identifier at a small fixed
99 offset into the file can usually be described in this way.
100 The information identifying these files is read from the compiled
102 .Pa /usr/share/misc/magic.mgc ,
103 or the files in the directory
104 .Pa /usr/share/misc/magic
105 if the compiled file does not exist.
110 exists, it will be used in preference to the system magic files.
112 If a file does not match any of the entries in the magic file,
113 it is examined to see if it seems to be a text file.
114 ASCII, ISO-8859-x, non-ISO 8-bit extended-ASCII character sets
115 (such as those used on Macintosh and IBM PC systems),
116 UTF-8-encoded Unicode, UTF-16-encoded Unicode, and EBCDIC
117 character sets can be distinguished by the different
118 ranges and sequences of bytes that constitute printable text
120 If a file passes any of these tests, its character set is reported.
121 ASCII, ISO-8859-x, UTF-8, and extended-ASCII files are identified
124 because they will be mostly readable on nearly any terminal;
125 UTF-16 and EBCDIC are only
128 they contain text, it is text that will require translation
129 before it can be read.
132 will attempt to determine other characteristics of text-type files.
133 If the lines of a file are terminated by CR, CRLF, or NEL, instead
134 of the Unix-standard LF, this will be reported.
135 Files that contain embedded escape sequences or overstriking
136 will also be identified.
140 has determined the character set used in a text-type file,
142 attempt to determine in what language the file is written.
143 The language tests look for particular strings (cf.
145 that can appear anywhere in the first few blocks of a file.
146 For example, the keyword
148 indicates that the file is most likely a
150 input file, just as the keyword
152 indicates a C program.
153 These tests are less reliable than the previous
154 two groups, so they are performed last.
155 The language test routines also test for some miscellany
160 Any file that cannot be identified as having been written
161 in any of the character sets listed above is simply said to be
164 .Bl -tag -width indent
166 Output a null character
168 after the end of the filename.
172 This does not affect the separator which is still printed.
173 .It Fl b , Fl Fl brief
174 Do not prepend filenames to output lines (brief mode).
175 .It Fl c , Fl Fl checking-printout
176 Cause a checking printout of the parsed form of the magic file.
177 This is usually used in conjunction with the
179 flag to debug a new magic file before installing it.
180 .It Fl C , Fl Fl compile
183 output file that contains a pre-parsed version of the magic file or directory.
184 .It Fl e , Fl Fl exclude Ar testname
185 Exclude the test named in
187 from the list of tests made to determine the file type.
188 Valid test names are:
189 .Bl -tag -width compress
192 application type (only on EMX).
194 Various types of text files (this test will try to guess the text
195 encoding, irrespective of the setting of the
199 Different text encodings for soft magic tests.
201 Looks for known tokens inside text files.
203 Prints details of Compound Document Files.
205 Checks for, and looks inside, compressed files.
207 Prints ELF file details.
209 Consults magic files.
213 .It Fl F , Fl Fl separator Ar separator
214 Use the specified string as the separator between the filename and the
215 file result returned.
218 .It Fl f , Fl Fl files-from Ar namefile
219 Read the names of the files to be examined from
222 before the argument list.
225 or at least one filename argument must be present;
226 to test the standard input, use
228 as a filename argument.
229 .It Fl h , Fl Fl no-dereference
230 Do not follow symlinks
231 (on systems that support symbolic links).
232 This is the default if the environment variable
236 Print a help message and exit.
237 .It Fl i , Fl Fl mime
238 Output mime type strings rather than the more
239 traditional human readable ones.
243 .Dq text/plain; charset=us-ascii
246 In order for this option to work,
249 it handles files recognized by the command itself (such as many of the
250 text file types, directories etc), and makes use of an alternative
256 .It Fl Fl mime-type , Fl Fl mime-encoding
259 but print only the specified element(s).
260 .It Fl k , Fl Fl keep-going
261 Don't stop at the first match, keep going.
262 Subsequent matches will have the string
265 (If you want a newline, see the
268 .It Fl L , Fl Fl dereference
269 Follow symlinks, as the like-named option in
271 (on systems that support symbolic links).
272 This is the default if the environment variable
275 .It Fl m , Fl Fl magic-file Ar list
276 Specify an alternate list of files and directories containing magic.
277 This can be a single item, or a colon-separated list.
278 If a compiled magic file is found alongside a file or directory,
279 it will be used instead.
280 .It Fl N , Fl Fl no-pad
281 Don't pad filenames so that they align in the output.
282 .It Fl n , Fl Fl no-buffer
283 Force stdout to be flushed after checking each file.
284 This is only useful if checking a list of files.
285 It is intended to be used by programs that want filetype output from a pipe.
286 .It Fl p , Fl Fl preserve-date
287 On systems that support
291 attempt to preserve the access time of files analyzed, to pretend that
295 Don't translate unprintable characters to \eooo.
298 translates unprintable characters to their octal representation.
299 .It Fl s , Fl Fl special-files
302 only attempts to read and determine the type of argument files which
304 reports are ordinary files.
305 This prevents problems, because reading special files may have peculiar
311 to also read argument files which are block or character special files.
312 This is useful for determining the filesystem types of the data in raw
313 disk partitions, which are block special files.
314 This option also causes
316 to disregard the file size as reported by
318 since on some systems it reports a zero size for raw disk partitions.
319 .It Fl v , Fl Fl version
320 Print the version of the program and exit.
321 .It Fl z , Fl Fl uncompress
322 Try to look inside compressed files.
326 returns 0 on success, and non-zero on error.
328 The environment variable
330 can be used to set the default magic file name.
331 If that variable is set, then
333 will not attempt to open
338 to the value of this variable as appropriate.
339 The environment variable
341 controls (on systems that support symbolic links), whether
343 will attempt to follow symlinks or not.
346 follows symlink, otherwise it does not.
347 This is also controlled by the
353 .Bl -tag -width /usr/share/misc/magic.mgc -compact
354 .It Pa /usr/share/misc/magic.mgc
355 Default compiled list of magic.
356 .It Pa /usr/share/misc/magic
357 Directory containing default magic files.
360 .Bd -literal -offset indent
361 $ file file.c file /dev/{wd0a,hda}
362 file.c: C program text
363 file: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
364 dynamically linked (uses shared libs), stripped
365 /dev/wd0a: block special (0/0)
366 /dev/hda: block special (3/0)
368 $ file -s /dev/wd0{b,d}
370 /dev/wd0d: x86 boot sector
372 $ file -s /dev/hda{,1,2,3,4,5,6,7,8,9,10}
373 /dev/hda: x86 boot sector
374 /dev/hda1: Linux/i386 ext2 filesystem
375 /dev/hda2: x86 boot sector
376 /dev/hda3: x86 boot sector, extended partition table
377 /dev/hda4: Linux/i386 ext2 filesystem
378 /dev/hda5: Linux/i386 swap file
379 /dev/hda6: Linux/i386 swap file
380 /dev/hda7: Linux/i386 swap file
381 /dev/hda8: Linux/i386 swap file
385 $ file -i file.c file /dev/{wd0a,hda}
387 file: application/x-executable
388 /dev/hda: application/x-not-regular-file
389 /dev/wd0a: application/x-not-regular-file
398 This program is believed to exceed the System V Interface Definition
399 of FILE(CMD), as near as one can determine from the vague language
401 Its behavior is mostly compatible with the System V program of the same name.
402 This version knows more magic, however, so it will produce
403 different (albeit more accurate) output in many cases.
404 .\" URL: http://www.opengroup.org/onlinepubs/009695399/utilities/file.html
406 The one significant difference
407 between this version and System V
408 is that this version treats any white space
409 as a delimiter, so that spaces in pattern strings must be escaped.
411 .Bd -literal -offset indent
412 \*[Gt]10 string language impress\ (imPRESS data)
415 in an existing magic file would have to be changed to
416 .Bd -literal -offset indent
417 \*[Gt]10 string language\e impress (imPRESS data)
420 In addition, in this version, if a pattern string contains a backslash,
423 .Bd -literal -offset indent
424 0 string \ebegindata Andrew Toolkit document
427 in an existing magic file would have to be changed to
428 .Bd -literal -offset indent
429 0 string \e\ebegindata Andrew Toolkit document
432 SunOS releases 3.2 and later from Sun Microsystems include a
434 command derived from the System V one, but with some extensions.
435 This version differs from Sun's only in minor ways.
436 It includes the extension of the
440 .Bd -literal -offset indent
441 \*[Gt]16 long\*[Am]0x7fffffff \*[Gt]0 not stripped
444 The magic file entries have been collected from various sources,
445 mainly USENET, and contributed by various authors.
446 Christos Zoulas (address below) will collect additional
447 or corrected magic file entries.
448 A consolidation of magic file entries
449 will be distributed periodically.
451 The order of entries in the magic file is significant.
452 Depending on what system you are using, the order that
453 they are put together may be incorrect.
456 command uses a magic file,
457 keep the old magic file around for comparison purposes
459 .Pa /usr/share/misc/magic.orig ) .
464 .Dv UNIX since at least Research Version 4
465 (man page dated November, 1973).
466 The System V version introduced one significant major change:
467 the external list of magic types.
468 This slowed the program down slightly but made it a lot more flexible.
470 This program, based on the System V version,
471 was written by Ian Darwin
472 .Aq ian@darwinsys.com
473 without looking at anybody else's source code.
475 John Gilmore revised the code extensively, making it better than
477 Geoff Collyer found several inadequacies
478 and provided some magic file entries.
481 operator by Rob McMahon, cudcv@warwick.ac.uk, 1989.
483 Guy Harris, guy@netapp.com, made many changes from 1993 to the present.
485 Primary development and maintenance from 1990 to the present by
487 .Aq christos@astron.com .
489 Altered by Chris Lowth, chris@lowth.com, 2000:
492 option to output mime type strings, using an alternative
493 magic file and internal logic.
495 Altered by Eric Fischer
498 to identify character codes and attempt to identify the languages
501 Altered by Reuben Thomas
503 2007 to 2008, to improve MIME
504 support and merge MIME and non-MIME magic, support directories as well
505 as files of magic, apply many bug fixes and improve the build system.
507 The list of contributors to the
509 directory (magic files)
510 is too long to include here.
511 You know who you are; thank you.
512 Many contributors are listed in the source files.
514 Copyright (c) Ian F. Darwin, Toronto, Canada, 1986-1999.
515 Covered by the standard Berkeley Software Distribution copyright; see the file
516 LEGAL.NOTICE in the source distribution.
522 were written by John Gilmore from his public-domain
524 program, and are not covered by the above license.
526 There must be a better way to automate the construction of the Magic
527 file from all the glop in Magdir.
531 uses several algorithms that favor speed over accuracy,
532 thus it can be misled about the contents of text files.
534 The support for text files (primarily for programming languages)
535 is simplistic, inefficient and requires recompilation to update.
537 The list of keywords in
539 probably belongs in the Magic file.
540 This could be done by using some keyword like
542 for the offset value.
544 Complain about conflicts in the magic file entries.
545 Make a rule that the magic entries sort based on file offset rather
546 than position within the magic file?
548 The program should provide a way to give an estimate of
551 We end up removing guesses (e.g.
553 as first 5 chars of file) because
554 they are not as good as other guesses (e.g.
558 Still, if the others don't pan out, it should be possible to use the
561 This manual page, and particularly this section, is too long.
563 You can obtain the original author's latest version by anonymous FTP
567 .Pa /pub/file/file-X.YZ.tar.gz .