Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / file / file.1
1 .\" $FreeBSD: src/usr.bin/file/file.1,v 1.16.2.10 2003/03/16 04:47:04 obrien Exp $
2 .\" $Id: file.man,v 1.44 2003/02/27 20:47:46 christos Exp $
3 .Dd February 27, 2003
4 .Dt FILE 1 "Copyright but distributable"
5 .Os
6 .Sh NAME
7 .Nm file
8 .Nd determine file type
9 .Sh SYNOPSIS
10 .Nm
11 .Op Fl bciknNsvzL
12 .Op Fl f Ar namefile
13 .Op Fl F Ar separator
14 .Op Fl m Ar magicfiles
15 .Ar
16 .Nm
17 .Fl C
18 .Op Fl m Ar magicfile
19 .Sh DESCRIPTION
20 This manual page documents version 3.41 of the
21 .Nm
22 utility which tests each argument in an attempt to classify it.
23 There are three sets of tests, performed in this order:
24 file system tests, magic number tests, and language tests.
25 The
26 .Em first
27 test that succeeds causes the file type to be printed.
28 .Pp
29 The type printed will usually contain one of the words
30 .Dq Li text
31 (the file contains only
32 printing characters and a few common control
33 characters and is probably safe to read on an
34 .Tn ASCII
35 terminal),
36 .Dq Li executable
37 (the file contains the result of compiling a program
38 in a form understandable to some
39 .Ux
40 kernel or another),
41 or
42 .Dq Li data
43 meaning anything else (data is usually
44 .Sq binary
45 or non-printable).
46 Exceptions are well-known file formats (core files, tar archives)
47 that are known to contain binary data.
48 When modifying the file
49 .Pa /usr/share/misc/magic
50 or the program itself,
51 .Em "preserve these keywords" .
52 People depend on knowing that all the readable files in a directory
53 have the word
54 .Dq Li text
55 printed.
56 Don't do as Berkeley did and change
57 .Dq Li "shell commands text"
58 to
59 .Dq Li "shell script" .
60 Note that the file
61 .Pa /usr/share/misc/magic
62 is built mechanically from a large number of small files in
63 the subdirectory
64 .Pa Magdir
65 in the source distribution of this program.
66 .Pp
67 The file system tests are based on examining the return from a
68 .Xr stat 2
69 system call.
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
74 implement them)
75 are intuited if they are defined in
76 the system header file
77 .Aq Pa sys/stat.h .
78 .Pp
79 The magic number tests are used to check for files with data in
80 particular fixed formats.
81 The canonical example of this is a binary executable (compiled program)
82 .Pa a.out
83 file, whose format is defined in
84 .Aq Pa a.out.h
85 and possibly
86 .Aq Pa exec.h
87 in the standard include directory.
88 These files have a
89 .Sq "magic number"
90 stored in a particular place
91 near the beginning of the file that tells the
92 .Ux
93 operating system
94 that the file is a binary executable, and which of several types thereof.
95 The concept of
96 .Sq "magic number"
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
101 magic file
102 .Pa /usr/share/misc/magic.mgc ,
103 or
104 .Pa /usr/share/misc/magic
105 if the compile file does not exist.
106 .Pp
107 If a file does not match any of the entries in the magic file,
108 it is examined to see if it seems to be a text file.
109 ASCII, ISO-8859-x, non-ISO 8-bit extended-ASCII character sets
110 (such as those used on Macintosh and IBM PC systems),
111 UTF-8-encoded Unicode, UTF-16-encoded Unicode, and EBCDIC
112 character sets can be distinguished by the different
113 ranges and sequences of bytes that constitute printable text
114 in each set.
115 If a file passes any of these tests, its character set is reported.
116 ASCII, ISO-8859-x, UTF-8, and extended-ASCII files are identified
117 as
118 .Dq Li text
119 because they will be mostly readable on nearly any terminal;
120 UTF-16 and EBCDIC are only
121 .Dq Li "character data"
122 because, while
123 they contain text, it is text that will require translation
124 before it can be read.
125 In addition,
126 .Nm
127 will attempt to determine other characteristics of text-type files.
128 If the lines of a file are terminated by CR, CRLF, or NEL, instead
129 of the
130 .Ux Ns -standard
131 LF, this will be reported.
132 Files that contain embedded escape sequences or overstriking
133 will also be identified.
134 .Pp
135 Once
136 .Nm
137 has determined the character set used in a text-type file,
138 it will
139 attempt to determine in what language the file is written.
140 The language tests look for particular strings (cf
141 .Pa names.h )
142 that can appear anywhere in the first few blocks of a file.
143 For example, the keyword
144 .Ic .br
145 indicates that the file is most likely a
146 .Xr troff 1
147 input file, just as the keyword
148 .Ic struct
149 indicates a C program.
150 These tests are less reliable than the previous
151 two groups, so they are performed last.
152 The language test routines also test for some miscellany
153 (such as
154 .Xr tar 1
155 archives).
156 .Pp
157 Any file that cannot be identified as having been written
158 in any of the character sets listed above is simply said to be
159 .Dq Li data .
160 .Sh OPTIONS
161 .Bl -tag -width indent
162 .It Fl b
163 Do not prepend filenames to output lines (brief mode).
164 .It Fl c
165 Cause a checking printout of the parsed form of the magic file.
166 This is usually used in conjunction with
167 .Fl m
168 to debug a new magic file before installing it.
169 .It Fl C
170 Write a
171 .Pa magic.mgc
172 output file that contains a pre-parsed version of
173 file.
174 .It Fl f Ar namefile
175 Read the names of the files to be examined from
176 .Ar namefile
177 (one per line)
178 before the argument list.
179 Either
180 .Ar namefile
181 or at least one filename argument must be present;
182 to test the standard input, use
183 .Dq Fl
184 as a filename argument.
185 .It Fl F Ar separator
186 Use the specified separator character instead of
187 .Ql \&: .
188 .It Fl i
189 Causes the file command to output mime type strings rather than the more
190 traditional human readable ones.
191 Thus it may say
192 .Dq Li "text/plain; charset=us-ascii"
193 rather than
194 .Dq Li "ASCII text" .
195 In order for this option to work, file changes the way
196 it handles files recognised by the command itself (such as many of the
197 text file types, directories etc), and makes use of an alternative
198 .Pa magic
199 file.
200 (See
201 .Sx FILES
202 section, below).
203 .It Fl k
204 Don't stop at the first match, keep going.
205 .It Fl m Ar list
206 Specify an alternate list of files containing magic numbers.
207 This can be a single file, or a colon-separated list of files.
208 .It Fl n
209 Force stdout to be flushed after checking each file.
210 This is only useful if checking a list of files.
211 It is intended to be used by programs that want
212 filetype output from a pipe.
213 .It Fl N
214 Don't pad output to align filenames nicely.
215 .It Fl v
216 Print the version of the program and exit.
217 .It Fl z
218 Try to look inside compressed files.
219 .It Fl L
220 option causes symlinks to be followed, as the like-named option in
221 .Xr ls 1 .
222 (on systems that support symbolic links).
223 .It Fl s
224 Normally,
225 .Nm
226 only attempts to read and determine the type of argument files which
227 .Xr stat 2
228 reports are ordinary files.
229 This prevents problems, because reading special files may have peculiar
230 consequences.
231 Specifying the
232 .Fl s
233 option causes
234 .Nm
235 to also read argument files which are block or character special files.
236 This is useful for determining the file system types of the data in raw
237 disk partitions, which are block special files.
238 This option also causes
239 .Nm
240 to disregard the file size as reported by
241 .Xr stat 2
242 since on some systems it reports a zero size for raw disk partitions.
243 .El
244 .Sh FILES
245 .Bl -tag -width ".Pa /usr/share/misc/magic.mime" -compact
246 .It Pa /usr/share/misc/magic.mgc
247 default compiled list of magic numbers
248 .It Pa /usr/share/misc/magic
249 default list of magic numbers
250 .It Pa /usr/share/misc/magic.mime
251 default list of magic numbers, used to output mime types when the
252 .Fl i
253 option is specified.
254 .El
255 .Sh ENVIRONMENT
256 The environment variable
257 .Ev MAGIC
258 can be used to set the default magic number files.
259 .Sh SEE ALSO
260 .Xr hexdump 1 ,
261 .Xr od 1 ,
262 .Xr strings 1 ,
263 .Xr magic 5
264 .Sh STANDARDS CONFORMANCE
265 This program is believed to exceed the
266 .St -svid4
267 of FILE(CMD), as near as one can determine from the vague language
268 contained therein.
269 Its behaviour is mostly compatible with the System V program of the same name.
270 This version knows more magic, however, so it will produce
271 different (albeit more accurate) output in many cases.
272 .Pp
273 The one significant difference
274 between this version and System V
275 is that this version treats any white space
276 as a delimiter, so that spaces in pattern strings must be escaped.
277 For example,
278 .Pp
279 .Dl ">10        string  language impress\       (imPRESS data)"
280 .Pp
281 in an existing magic file would have to be changed to
282 .Pp
283 .Dl ">10        string  language\e impress      (imPRESS data)"
284 .Pp
285 In addition, in this version, if a pattern string contains a backslash,
286 it must be escaped.
287 For example
288 .Pp
289 .Dl "0  string          \ebegindata     Andrew Toolkit document"
290 .Pp
291 in an existing magic file would have to be changed to
292 .Pp
293 .Dl "0  string          \e\ebegindata   Andrew Toolkit document"
294 .Pp
295 SunOS releases 3.2 and later from Sun Microsystems include a
296 .Xr file 1
297 command derived from the System V one, but with some extensions.
298 My version differs from Sun's only in minor ways.
299 It includes the extension of the
300 .Sq Ic &
301 operator, used as,
302 for example,
303 .Pp
304 .Dl ">16        long&0x7fffffff >0              not stripped"
305 .Sh MAGIC DIRECTORY
306 The magic file entries have been collected from various sources,
307 mainly USENET, and contributed by various authors.
308 .An Christos Zoulas
309 (address below) will collect additional
310 or corrected magic file entries.
311 A consolidation of magic file entries
312 will be distributed periodically.
313 .Pp
314 The order of entries in the magic file is significant.
315 Depending on what system you are using, the order that
316 they are put together may be incorrect.
317 If your old
318 .Nm
319 command uses a magic file,
320 keep the old magic file around for comparison purposes
321 (rename it to
322 .Pa /usr/share/misc/magic.orig ) .
323 .Sh EXAMPLES
324 .Bd -literal
325 $ file file.c file /dev/{wd0a,hda}
326 file.c:    C program text
327 file:      ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
328            dynamically linked (uses shared libs), stripped
329 /dev/wd0a: block special (0/0)
330 /dev/hda:  block special (3/0)
331 $ file -s /dev/wd0{b,d}
332 /dev/wd0b: data
333 /dev/wd0d: x86 boot sector
334 $ file -s /dev/hda{,1,2,3,4,5,6,7,8,9,10}       # Linux
335 /dev/hda:   x86 boot sector
336 /dev/hda1:  Linux/i386 ext2 filesystem
337 /dev/hda2:  x86 boot sector
338 /dev/hda3:  x86 boot sector, extended partition table
339 /dev/hda4:  Linux/i386 ext2 filesystem
340 /dev/hda5:  Linux/i386 swap file
341 /dev/hda6:  Linux/i386 swap file
342 /dev/hda7:  Linux/i386 swap file
343 /dev/hda8:  Linux/i386 swap file
344 /dev/hda9:  empty
345 /dev/hda10: empty
346
347 $ file -s /dev/rwd0e                            # BSD
348 /dev/rwd0e:
349 Unix Fast File system (little-endian),
350 last mounted on /usr,
351 last written at Mon Feb 10 13:22:40 2003,
352 clean flag 2,
353 number of blocks 28754208,
354 number of data blocks 27812712,
355 number of cylinder groups 3566,
356 block size 8192,
357 fragment size 1024,
358 minimum percentage of free blocks 5,
359 rotational delay 0ms,
360 disk rotational speed 60rps,
361 TIME optimization
362
363 $ file -i file.c file /dev/{wd0a,hda}
364 file.c:    text/x-c
365 file:      application/x-executable, dynamically linked (uses shared libs),
366            not stripped
367 /dev/hda:  application/x-not-regular-file
368 /dev/wd0a: application/x-not-regular-file
369 .Ed
370 .Sh HISTORY
371 There has been a
372 .Nm
373 command in every
374 .Ux
375 since at least Research Version 4
376 (man page dated November, 1973).
377 The System V version introduced one significant major change:
378 the external list of magic number types.
379 This slowed the program down slightly but made it a lot more flexible.
380 .Pp
381 This program, based on the System V version,
382 was written by
383 .An Ian Darwin Aq ian@darwinsys.com
384 without looking at anybody else's source code.
385 .Pp
386 .An John Gilmore
387 revised the code extensively, making it better than
388 the first version.
389 .An Geoff Collyer
390 found several inadequacies
391 and provided some magic file entries.
392 Contributions by the
393 .Sq Ic &
394 operator by
395 .An Rob McMahon Aq cudcv@warwick.ac.uk ,
396 1989.
397 .Pp
398 .An Guy Harris Aq guy@netapp.com ,
399 made many changes from 1993 to the present.
400 .Pp
401 Primary development and maintenance from 1990 to the present by
402 .An Christos Zoulas Aq christos@astron.com .
403 .Pp
404 Altered by
405 .An Chris Lowth Aq chris@lowth.com ,
406 2000:
407 Handle the
408 .Fl i
409 option to output mime type strings and using an alternative
410 magic file and internal logic.
411 .Pp
412 Altered by
413 .An Eric Fischer Aq enf@pobox.com ,
414 July, 2000,
415 to identify character codes and attempt to identify the languages
416 of
417 .No non- Ns Tn ASCII
418 files.
419 .Pp
420 The list of contributors to the
421 .Pa Magdir
422 directory (source for the
423 .Pa /usr/share/misc/magic
424 file) is too long to include here.
425 You know who you are; thank you.
426 .Sh LEGAL NOTICE
427 Copyright (c) Ian F. Darwin, Toronto, Canada, 1986-1999.
428 Covered by the standard Berkeley Software Distribution copyright; see the file
429 .Pa LEGAL.NOTICE
430 in the source distribution.
431 .Pp
432 The files
433 .Pa tar.h
434 and
435 .Pa is_tar.c
436 were written by
437 .An John Gilmore
438 from his public-domain
439 .Nm tar
440 program, and are not covered by the above license.
441 .Sh BUGS
442 There must be a better way to automate the construction of the
443 .Pa Magic
444 file from all the glop in
445 .Pa Magdir .
446 What is it?
447 Better yet, the magic file should be compiled into binary (say,
448 .Xr ndbm 3
449 or, better yet, fixed-length
450 .Tn ASCII
451 strings for use in heterogenous network environments) for faster startup.
452 Then the program would run as fast as the Version 7 program of the same name,
453 with the flexibility of the System V version.
454 .Pp
455 The
456 .Nm
457 utility uses several algorithms that favor speed over accuracy,
458 thus it can be misled about the contents of
459 text
460 files.
461 .Pp
462 The support for
463 text
464 files (primarily for programming languages)
465 is simplistic, inefficient and requires recompilation to update.
466 .Pp
467 There should be an
468 .Ic else
469 clause to follow a series of continuation lines.
470 .Pp
471 The magic file and keywords should have regular expression support.
472 Their use of
473 .Tn "ASCII TAB"
474 as a field delimiter is ugly and makes
475 it hard to edit the files, but is entrenched.
476 .Pp
477 It might be advisable to allow upper-case letters in keywords
478 for e.g.,
479 .Xr troff 1
480 commands vs man page macros.
481 Regular expression support would make this easy.
482 .Pp
483 The program doesn't grok
484 .Tn FORTRAN .
485 It should be able to figure
486 .Tn FORTRAN
487 by seeing some keywords which
488 appear indented at the start of line.
489 Regular expression support would make this easy.
490 .Pp
491 The list of keywords in
492 .Pa ascmagic
493 probably belongs in the
494 .Pa Magic
495 file.
496 This could be done by using some keyword like
497 .Sq Ic *
498 for the offset value.
499 .Pp
500 Another optimisation would be to sort
501 the magic file so that we can just run down all the
502 tests for the first byte, first word, first long, etc, once we
503 have fetched it.
504 Complain about conflicts in the magic file entries.
505 Make a rule that the magic entries sort based on file offset rather
506 than position within the magic file?
507 .Pp
508 The program should provide a way to give an estimate
509 of
510 .Dq how good
511 a guess is.
512 We end up removing guesses (e.g.\&
513 .Dq Li "From "
514 as first 5 chars of file) because
515 they are not as good as other guesses (e.g.\&
516 .Dq Li "Newsgroups:"
517 versus
518 .Dq Li "Return-Path:" ) .
519 Still, if the others don't pan out, it should be
520 possible to use the first guess.
521 .Pp
522 This program is slower than some vendors' file commands.
523 The new support for multiple character codes makes it even slower.
524 .Pp
525 This manual page, and particularly this section, is too long.
526 .Sh AVAILABILITY
527 You can obtain the original author's latest version by anonymous FTP
528 on
529 .Pa ftp.astron.com
530 in the directory
531 .Pa /pub/file/file-X.YZ.tar.gz