1 .\" $File: file.man,v 1.66 2007/10/23 19:58:59 christos Exp $
7 .Nd determine file type
15 .Op Fl m Ar magicfiles
21 This manual page documents version __VERSION__ of the
26 tests each argument in an attempt to classify it.
27 There are three sets of tests, performed in this order:
28 filesystem tests, magic number tests, and language tests.
31 test that succeeds causes the file type to be printed.
33 The type printed will usually contain one of the words
35 (the file contains only
36 printing characters and a few common control
37 characters and is probably safe to read on an
41 (the file contains the result of compiling a program
42 in a form understandable to some
47 meaning anything else (data is usually
50 Exceptions are well-known file formats (core files, tar archives)
51 that are known to contain binary data.
52 When modifying the file
54 or the program itself, make sure to
55 .Em "preserve these keywords" .
56 People depend on knowing that all the readable files in a directory
60 Don't do as Berkeley did and change
61 .Dq shell commands text
66 is built mechanically from a large number of small files in
69 in the source distribution of this program.
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
80 the system header file
83 The magic number tests are used to check for files with data in
84 particular fixed formats.
85 The canonical example of this is a binary executable (compiled program)
87 file, whose format is defined in
92 in the standard include directory.
95 stored in a particular place
96 near the beginning of the file that tells the
97 .Dv UNIX operating system
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
109 if the compile file does not exist. In addition
112 .Pa $HOME/.magic.mgc ,
117 If a file does not match any of the entries in the magic file,
118 it is examined to see if it seems to be a text file.
119 ASCII, ISO-8859-x, non-ISO 8-bit extended-ASCII character sets
120 (such as those used on Macintosh and IBM PC systems),
121 UTF-8-encoded Unicode, UTF-16-encoded Unicode, and EBCDIC
122 character sets can be distinguished by the different
123 ranges and sequences of bytes that constitute printable text
125 If a file passes any of these tests, its character set is reported.
126 ASCII, ISO-8859-x, UTF-8, and extended-ASCII files are identified
129 because they will be mostly readable on nearly any terminal;
130 UTF-16 and EBCDIC are only
133 they contain text, it is text that will require translation
134 before it can be read.
137 will attempt to determine other characteristics of text-type files.
138 If the lines of a file are terminated by CR, CRLF, or NEL, instead
139 of the Unix-standard LF, this will be reported.
140 Files that contain embedded escape sequences or overstriking
141 will also be identified.
145 has determined the character set used in a text-type file,
147 attempt to determine in what language the file is written.
148 The language tests look for particular strings (cf
150 that can appear anywhere in the first few blocks of a file.
151 For example, the keyword
153 indicates that the file is most likely a
155 input file, just as the keyword
157 indicates a C program.
158 These tests are less reliable than the previous
159 two groups, so they are performed last.
160 The language test routines also test for some miscellany
165 Any file that cannot be identified as having been written
166 in any of the character sets listed above is simply said to be ``data''.
168 .Bl -tag -width indent
170 Do not prepend filenames to output lines (brief mode).
171 .It Fl c , -checking-printout
172 Cause a checking printout of the parsed form of the magic file.
173 This is usually used in conjunction with the
175 flag to debug a new magic file before installing it.
179 output file that contains a pre-parsed version of the magic file.
180 .It Fl e , -exclude Ar testname
181 Exclude the test named in
183 from the list of tests made to determine the file type. Valid test names
189 application type (only on EMX).
191 Check for various types of ascii files.
193 Don't look for, or inside compressed files.
195 Don't print elf details.
197 Don't look for fortran sequences inside ascii files.
199 Don't consult magic files.
201 Don't examine tar files.
203 Don't look for known tokens inside ascii files.
205 Don't look for troff sequences inside ascii files.
207 .It Fl f , -files-from Ar namefile
208 Read the names of the files to be examined from
211 before the argument list.
214 or at least one filename argument must be present;
215 to test the standard input, use
217 as a filename argument.
218 .It Fl F , -separator Ar separator
219 Use the specified string as the separator between the filename and the
220 file result returned. Defaults to
222 .It Fl h , -no-dereference
223 option causes symlinks not to be followed
224 (on systems that support symbolic links). This is the default if the
229 Causes the file command to output mime type strings rather than the more
230 traditional human readable ones. Thus it may say
231 .Dq text/plain charset=us-ascii
234 In order for this option to work, file changes the way
235 it handles files recognized by the command itself (such as many of the
236 text file types, directories etc), and makes use of an alternative
242 .It Fl -mime-type , -mime-encoding
245 but print only the specified element(s).
246 .It Fl k , -keep-going
247 Don't stop at the first match, keep going.
248 .It Fl L , -dereference
249 option causes symlinks to be followed, as the like-named option in
251 (on systems that support symbolic links).
252 This is the default if the environment variable
255 .It Fl m , -magic-file Ar list
256 Specify an alternate list of files containing magic numbers.
257 This can be a single file, or a colon-separated list of files.
258 If a compiled magic file is found alongside, it will be used instead.
263 option, the program adds
266 .It Fl n , -no-buffer
267 Force stdout to be flushed after checking each file.
268 This is only useful if checking a list of files.
269 It is intended to be used by programs that want filetype output from a pipe.
271 Don't pad filenames so that they align in the output.
272 .It Fl p , -preserve-date
273 On systems that support
277 attempt to preserve the access time of files analyzed, to pretend that
281 Don't translate unprintable characters to \eooo.
284 translates unprintable characters to their octal representation.
285 .It Fl s , -special-files
288 only attempts to read and determine the type of argument files which
290 reports are ordinary files.
291 This prevents problems, because reading special files may have peculiar
297 to also read argument files which are block or character special files.
298 This is useful for determining the filesystem types of the data in raw
299 disk partitions, which are block special files.
300 This option also causes
302 to disregard the file size as reported by
304 since on some systems it reports a zero size for raw disk partitions.
306 Print the version of the program and exit.
307 .It Fl z , -uncompress
308 Try to look inside compressed files.
310 Output a null character
312 after the end of the filename. Nice to
314 the output. This does not affect the separator which is still printed.
316 Print a help message and exit.
319 .Bl -tag -width __MAGIC__.mime.mgc -compact
321 Default compiled list of magic numbers
323 Default list of magic numbers
324 .It Pa __MAGIC__.mime.mgc
325 Default compiled list of magic numbers, used to output mime types when
329 .It Pa __MAGIC__.mime
330 Default list of magic numbers, used to output mime types when the
335 The environment variable
337 can be used to set the default magic number file name.
338 If that variable is set, then
340 will not attempt to open
347 to the value of this variable as appropriate.
348 The environment variable
350 controls (on systems that support symbolic links), if
352 will attempt to follow symlinks or not. If set, then
354 follows symlink, otherwise it does not. This is also controlled
361 .Xr magic __FSECTION__ ,
365 .Sh STANDARDS CONFORMANCE
366 This program is believed to exceed the System V Interface Definition
367 of FILE(CMD), as near as one can determine from the vague language
369 Its behavior is mostly compatible with the System V program of the same name.
370 This version knows more magic, however, so it will produce
371 different (albeit more accurate) output in many cases.
372 .\" URL: http://www.opengroup.org/onlinepubs/009695399/utilities/file.html
374 The one significant difference
375 between this version and System V
376 is that this version treats any white space
377 as a delimiter, so that spaces in pattern strings must be escaped.
379 .Bd -literal -offset indent
380 >10 string language impress\ (imPRESS data)
383 in an existing magic file would have to be changed to
384 .Bd -literal -offset indent
385 >10 string language\e impress (imPRESS data)
388 In addition, in this version, if a pattern string contains a backslash,
391 .Bd -literal -offset indent
392 0 string \ebegindata Andrew Toolkit document
395 in an existing magic file would have to be changed to
396 .Bd -literal -offset indent
397 0 string \e\ebegindata Andrew Toolkit document
400 SunOS releases 3.2 and later from Sun Microsystems include a
402 command derived from the System V one, but with some extensions.
403 My version differs from Sun's only in minor ways.
404 It includes the extension of the
408 .Bd -literal -offset indent
409 >16 long&0x7fffffff >0 not stripped
412 The magic file entries have been collected from various sources,
413 mainly USENET, and contributed by various authors.
414 Christos Zoulas (address below) will collect additional
415 or corrected magic file entries.
416 A consolidation of magic file entries
417 will be distributed periodically.
419 The order of entries in the magic file is significant.
420 Depending on what system you are using, the order that
421 they are put together may be incorrect.
424 command uses a magic file,
425 keep the old magic file around for comparison purposes
427 .Pa __MAGIC__.orig ).
429 .Bd -literal -offset indent
430 $ file file.c file /dev/{wd0a,hda}
431 file.c: C program text
432 file: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
433 dynamically linked (uses shared libs), stripped
434 /dev/wd0a: block special (0/0)
435 /dev/hda: block special (3/0)
437 $ file -s /dev/wd0{b,d}
439 /dev/wd0d: x86 boot sector
441 $ file -s /dev/hda{,1,2,3,4,5,6,7,8,9,10}
442 /dev/hda: x86 boot sector
443 /dev/hda1: Linux/i386 ext2 filesystem
444 /dev/hda2: x86 boot sector
445 /dev/hda3: x86 boot sector, extended partition table
446 /dev/hda4: Linux/i386 ext2 filesystem
447 /dev/hda5: Linux/i386 swap file
448 /dev/hda6: Linux/i386 swap file
449 /dev/hda7: Linux/i386 swap file
450 /dev/hda8: Linux/i386 swap file
454 $ file -i file.c file /dev/{wd0a,hda}
456 file: application/x-executable
457 /dev/hda: application/x-not-regular-file
458 /dev/wd0a: application/x-not-regular-file
465 .Dv UNIX since at least Research Version 4
466 (man page dated November, 1973).
467 The System V version introduced one significant major change:
468 the external list of magic number types.
469 This slowed the program down slightly but made it a lot more flexible.
471 This program, based on the System V version,
472 was written by Ian Darwin <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.
479 Contributions by the `&' operator by Rob McMahon, cudcv@warwick.ac.uk, 1989.
481 Guy Harris, guy@netapp.com, made many changes from 1993 to the present.
483 Primary development and maintenance from 1990 to the present by
484 Christos Zoulas (christos@astron.com).
486 Altered by Chris Lowth, chris@lowth.com, 2000:
489 option to output mime type strings and using an alternative
490 magic file and internal logic.
492 Altered by Eric Fischer (enf@pobox.com), July, 2000,
493 to identify character codes and attempt to identify the languages
496 The list of contributors to the "Magdir" directory (source for the
498 file) is too long to include here.
499 You know who you are; thank you.
501 Copyright (c) Ian F. Darwin, Toronto, Canada, 1986-1999.
502 Covered by the standard Berkeley Software Distribution copyright; see the file
503 LEGAL.NOTICE in the source distribution.
509 were written by John Gilmore from his public-domain
511 program, and are not covered by the above license.
513 There must be a better way to automate the construction of the Magic
514 file from all the glop in Magdir.
516 .\" Compilation support has been done
517 .\" Better yet, the magic file should be compiled into binary (say,
519 .\" or, better yet, fixed-length
521 .\" strings for use in heterogenous network environments) for faster startup.
522 .\" Then the program would run as fast as the Version 7 program of the same
523 .\" name, with the flexibility of the System V version.
526 uses several algorithms that favor speed over accuracy,
527 thus it can be misled about the contents of
531 The support for text files (primarily for programming languages)
532 is simplistic, inefficient and requires recompilation to update.
533 .\" Else support has been done
534 .\" There should be an
536 .\" clause to follow a series of continuation lines.
538 .\" Regular expression support has been done
539 .\" The magic file and keywords should have regular expression support.
542 as a field delimiter is ugly and makes
543 it hard to edit the files, but is entrenched.
545 It might be advisable to allow upper-case letters in keywords
548 commands vs man page macros.
549 Regular expression support would make this easy.
551 The program doesn't grok
553 It should be able to figure
555 by seeing some keywords which
556 appear indented at the start of line.
557 Regular expression support would make this easy.
559 The list of keywords in
561 probably belongs in the Magic file.
562 This could be done by using some keyword like
564 for the offset value.
566 .\" Sorting has been done.
567 .\" Another optimization would be to sort
568 .\" the magic file so that we can just run down all the
569 .\" tests for the first byte, first word, first long, etc, once we
571 Complain about conflicts in the magic file entries.
572 Make a rule that the magic entries sort based on file offset rather
573 than position within the magic file?
575 The program should provide a way to give an estimate
579 We end up removing guesses (e.g.
581 as first 5 chars of file) because
582 they are not as good as other guesses (e.g.
587 Still, if the others don't pan out, it should be possible to use the
590 This program is slower than some vendors' file commands.
591 The new support for multiple character codes makes it even slower.
593 This manual page, and particularly this section, is too long.
595 You can obtain the original author's latest version by anonymous FTP
599 .Dv /pub/file/file-X.YZ.tar.gz