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