2b01f744042aca78163eabc281b0d16673ad5841
[dragonfly.git] / contrib / file / doc / file.man
1 .\" $File: file.man,v 1.87 2010/11/05 20:51:38 christos Exp $
2 .Dd July 23, 2010
3 .Dt FILE __CSECTION__
4 .Os
5 .Sh NAME
6 .Nm file
7 .Nd determine file type
8 .Sh SYNOPSIS
9 .Nm
10 .Bk -words
11 .Op Fl bchiklLNnprsvz0
12 .Op Fl -apple
13 .Op Fl -mime-encoding
14 .Op Fl -mime-type
15 .Op Fl e Ar testname
16 .Op Fl F Ar separator
17 .Op Fl f Ar namefile
18 .Op Fl m Ar magicfiles
19 .Ar
20 .Ek -words
21 .Nm
22 .Fl C
23 .Op Fl m Ar magicfiles
24 .Nm
25 .Op Fl -help
26 .Sh DESCRIPTION
27 This manual page documents version __VERSION__ of the
28 .Nm
29 command.
30 .Pp
31 .Nm
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.
35 The
36 .Em first
37 test that succeeds causes the file type to be printed.
38 .Pp
39 The type printed will usually contain one of the words
40 .Em text
41 (the file contains only
42 printing characters and a few common control
43 characters and is probably safe to read on an
44 .Dv ASCII
45 terminal),
46 .Em executable
47 (the file contains the result of compiling a program
48 in a form understandable to some
49 .Dv UNIX
50 kernel or another),
51 or
52 .Em data
53 meaning anything else (data is usually
54 .Sq binary
55 or non-printable).
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
61 have the word
62 .Sq text
63 printed.
64 Don't do as Berkeley did and change
65 .Sq shell commands text
66 to
67 .Sq shell script .
68 .Pp
69 The filesystem tests are based on examining the return from a
70 .Xr stat 2
71 system call.
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
76 implement them)
77 are intuited if they are defined in
78 the system header file
79 .In sys/stat.h .
80 .Pp
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)
84 .Dv a.out
85 file, whose format is defined in
86 .In elf.h ,
87 .In a.out.h
88 and possibly
89 .In exec.h
90 in the standard include directory.
91 These files have a
92 .Sq "magic number"
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.
97 The concept of a
98 .Sq "magic"
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
103 magic file
104 .Pa __MAGIC__.mgc ,
105 or the files in the directory
106 .Pa __MAGIC__
107 if the compiled file does not exist.
108 In addition, if
109 .Pa $HOME/.magic.mgc
110 or
111 .Pa $HOME/.magic
112 exists, it will be used in preference to the system magic files.
113 .Pp
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
121 in each set.
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
124 as
125 .Sq text
126 because they will be mostly readable on nearly any terminal;
127 UTF-16 and EBCDIC are only
128 .Sq character data
129 because, while
130 they contain text, it is text that will require translation
131 before it can be read.
132 In addition,
133 .Nm
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.
139 .Pp
140 Once
141 .Nm
142 has determined the character set used in a text-type file,
143 it will
144 attempt to determine in what language the file is written.
145 The language tests look for particular strings (cf.
146 .In names.h
147 ) that can appear anywhere in the first few blocks of a file.
148 For example, the keyword
149 .Em .br
150 indicates that the file is most likely a
151 .Xr troff 1
152 input file, just as the keyword
153 .Em struct
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
158 (such as
159 .Xr tar 1
160 archives).
161 .Pp
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
164 .Sq data .
165 .Sh OPTIONS
166 .Bl -tag -width indent
167 .It Fl b , -brief
168 Do not prepend filenames to output lines (brief mode).
169 .It Fl C , -compile
170 Write a
171 .Pa magic.mgc
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
176 .Fl m
177 flag to debug a new magic file before installing it.
178 .It Fl e , -exclude Ar testname
179 Exclude the test named in
180 .Ar testname
181 from the list of tests made to determine the file type.
182 Valid test names are:
183 .Bl -tag -width compress
184 .It apptype
185 .Dv EMX
186 application type (only on EMX).
187 .It ascii
188 Various types of text files (this test will try to guess the text encoding, irrespective of the setting of the
189 .Sq encoding
190 option).
191 .It encoding
192 Different text encodings for soft magic tests.
193 .It tokens
194 Looks for known tokens inside text files.
195 .It cdf
196 Prints details of Compound Document Files.
197 .It compress
198 Checks for, and looks inside, compressed files.
199 .It elf
200 Prints ELF file details.
201 .It soft
202 Consults magic files.
203 .It tar
204 Examines tar files.
205 .El
206 .It Fl F , -separator Ar separator
207 Use the specified string as the separator between the filename and the
208 file result returned.
209 Defaults to
210 .Sq \&: .
211 .It Fl f , -files-from Ar namefile
212 Read the names of the files to be examined from
213 .Ar namefile
214 (one per line)
215 before the argument list.
216 Either
217 .Ar namefile
218 or at least one filename argument must be present;
219 to test the standard input, use
220 .Sq -
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
226 .Dv POSIXLY_CORRECT
227 is not defined.
228 .It Fl i , -mime
229 Causes the file command to output mime type strings rather than the more
230 traditional human readable ones.
231 Thus it may say
232 .Sq text/plain; charset=us-ascii
233 rather than
234 .Sq ASCII text .
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
238 .Sq magic
239 file.
240 (See the FILES section, below).
241 .It Fl -mime-type , -mime-encoding
242 Like
243 .Fl i ,
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
248 have the string
249 .Sq "\[rs]012\- "
250 prepended.
251 (If you want a newline, see the
252 .Sq "\-r"
253 option.)
254 .It Fl l , -list
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
258 .Xr ls 1
259 (on systems that support symbolic links).
260 This is the default if the environment variable
261 .Dv POSIXLY_CORRECT
262 is defined.
263 .It Fl l
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.
269 .It Fl N , -no-pad
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
277 .Xr utime 2
278 or
279 .Xr utimes 2 ,
280 attempt to preserve the access time of files analyzed, to pretend that
281 .Nm
282 never read them.
283 .It Fl r , -raw
284 Don't translate unprintable characters to \eooo.
285 Normally
286 .Nm
287 translates unprintable characters to their octal representation.
288 .It Fl s , -special-files
289 Normally,
290 .Nm
291 only attempts to read and determine the type of argument files which
292 .Xr stat 2
293 reports are ordinary files.
294 This prevents problems, because reading special files may have peculiar
295 consequences.
296 Specifying the
297 .Fl s
298 option causes
299 .Nm
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
304 .Nm
305 to disregard the file size as reported by
306 .Xr stat 2
307 since on some systems it reports a zero size for raw disk partitions.
308 .It Fl v , -version
309 Print the version of the program and exit.
310 .It Fl z , -uncompress
311 Try to look inside compressed files.
312 .It Fl 0 , -print0
313 Output a null character
314 .Sq \e0
315 after the end of the filename.
316 Nice to
317 .Xr cut 1
318 the output.
319 This does not affect the separator which is still printed.
320 .It Fl -help
321 Print a help message and exit.
322 .El
323 .Sh FILES
324 .Bl -tag -width __MAGIC__.mgc -compact
325 .It Pa __MAGIC__.mgc
326 Default compiled list of magic.
327 .It Pa __MAGIC__
328 Directory containing default magic files.
329 .El
330 .Sh ENVIRONMENT
331 The environment variable
332 .Dv MAGIC
333 can be used to set the default magic file name.
334 If that variable is set, then
335 .Nm
336 will not attempt to open
337 .Pa $HOME/.magic .
338 .Nm
339 adds
340 .Sq .mgc
341 to the value of this variable as appropriate.
342 However,
343 .Pa file
344 has to exist in order for
345 .Pa file.mime
346 to be considered.
347 The environment variable
348 .Dv POSIXLY_CORRECT
349 controls (on systems that support symbolic links), whether
350 .Nm
351 will attempt to follow symlinks or not.
352 If set, then
353 .Nm
354 follows symlink, otherwise it does not.
355 This is also controlled by the
356 .Fl L
357 and
358 .Fl h
359 options.
360 .Sh SEE ALSO
361 .Xr magic __FSECTION__ ,
362 .Xr strings 1 ,
363 .Xr od 1 ,
364 .Xr hexdump 1 ,
365 .Xr file 1posix
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
369 contained therein.
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
374 .Pp
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.
379 For example,
380 .Bd -literal -offset indent
381 >10     string  language impress\       (imPRESS data)
382 .Ed
383 .Pp
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)
387 .Ed
388 .Pp
389 In addition, in this version, if a pattern string contains a backslash,
390 it must be escaped.
391 For example
392 .Bd -literal -offset indent
393 0       string          \ebegindata     Andrew Toolkit document
394 .Ed
395 .Pp
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
399 .Ed
400 .Pp
401 SunOS releases 3.2 and later from Sun Microsystems include a
402 .Nm
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
406 .Sq &
407 operator, used as,
408 for example,
409 .Bd -literal -offset indent
410 >16     long&0x7fffffff >0              not stripped
411 .Ed
412 .Sh MAGIC DIRECTORY
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.
419 .Pp
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.
423 If your old
424 .Nm
425 command uses a magic file,
426 keep the old magic file around for comparison purposes
427 (rename it to
428 .Pa __MAGIC__.orig ).
429 .Sh EXAMPLES
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)
437
438 $ file -s /dev/wd0{b,d}
439 /dev/wd0b: data
440 /dev/wd0d: x86 boot sector
441
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
452 /dev/hda9:  empty
453 /dev/hda10: empty
454
455 $ file -i file.c file /dev/{wd0a,hda}
456 file.c:      text/x-c
457 file:        application/x-executable
458 /dev/hda:    application/x-not-regular-file
459 /dev/wd0a:   application/x-not-regular-file
460
461 .Ed
462 .Sh HISTORY
463 There has been a
464 .Nm
465 command in every
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.
471 .Pp
472 This program, based on the System V version,
473 was written by Ian Darwin <ian@darwinsys.com>
474 without looking at anybody else's source code.
475 .Pp
476 John Gilmore revised the code extensively, making it better than
477 the first version.
478 Geoff Collyer found several inadequacies
479 and provided some magic file entries.
480 Contributions by the `&' operator by Rob McMahon, cudcv@warwick.ac.uk, 1989.
481 .Pp
482 Guy Harris, guy@netapp.com, made many changes from 1993 to the present.
483 .Pp
484 Primary development and maintenance from 1990 to the present by
485 Christos Zoulas (christos@astron.com).
486 .Pp
487 Altered by Chris Lowth, chris@lowth.com, 2000:
488 Handle the
489 .Fl i
490 option to output mime type strings, using an alternative
491 magic file and internal logic.
492 .Pp
493 Altered by Eric Fischer (enf@pobox.com), July, 2000,
494 to identify character codes and attempt to identify the languages
495 of non-ASCII files.
496 .Pp
497 Altered by Reuben Thomas (rrt@sc3d.org), 2007-2009, to improve MIME
498 support and merge MIME and non-MIME magic, support directories as well
499 as files of magic, apply many bug fixes, update and fix a lot of magic,
500 and improve the build system.
501 .Pp
502 The list of contributors to the
503 .Sq magic
504 directory (magic files)
505 is too long to include here.
506 You know who you are; thank you.
507 Many contributors are listed in the source files.
508 .Sh LEGAL NOTICE
509 Copyright (c) Ian F. Darwin, Toronto, Canada, 1986-1999.
510 Covered by the standard Berkeley Software Distribution copyright; see the file
511 COPYING in the source distribution.
512 .Pp
513 The files
514 .Dv tar.h
515 and
516 .Dv is_tar.c
517 were written by John Gilmore from his public-domain
518 .Xr tar 1
519 program, and are not covered by the above license.
520 .Sh BUGS
521 .Pp
522 Please report bugs and send patches to the bug tracker at
523 .Pa http://bugs.gw.com/
524 or the mailing list at
525 .Aq file@mx.gw.com .
526 .Pp
527 .Nm
528 uses several algorithms that favor speed over accuracy,
529 thus it can be misled about the contents of
530 text
531 files.
532 .Pp
533 The support for text files (primarily for programming languages)
534 is simplistic, inefficient and requires recompilation to update.
535 .Pp
536 The list of keywords in
537 .Dv ascmagic
538 probably belongs in the Magic file.
539 This could be done by using some keyword like
540 .Sq *
541 for the offset value.
542 .Pp
543 Complain about conflicts in the magic file entries.
544 Make a rule that the magic entries sort based on file offset rather
545 than position within the magic file?
546 .Pp
547 The program should provide a way to give an estimate
548 of
549 .Sq how good
550 a guess is.
551 We end up removing guesses (e.g.
552 .Sq From\
553 as first 5 chars of file) because
554 they are not as good as other guesses (e.g.
555 .Sq Newsgroups:
556 versus
557 .Sq Return-Path:
558 ).
559 Still, if the others don't pan out, it should be possible to use the
560 first guess.
561 .Pp
562 This manual page, and particularly this section, is too long.
563 .Sh RETURN CODE
564 .Nm
565 returns 0 on success, and non-zero on error.
566 .Sh AVAILABILITY
567 You can obtain the original author's latest version by anonymous FTP
568 on
569 .Dv ftp.astron.com
570 in the directory
571 .Dv /pub/file/file-X.YZ.tar.gz