]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.bin/file/file.1
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.bin / file / file.1
1 .\" $FreeBSD$
2 .\" $Id: file.man,v 1.57 2005/08/18 15:18:22 christos Exp $
3 .Dd August 18, 2005
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 bchikLnNprsvz
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 4.21 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 Do not 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 is 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 .In 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 .In a.out.h
85 and possibly
86 .In 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 In addition
107 .Nm
108 will look in
109 .Pa $HOME/.magic.mgc ,
110 or
111 .Pa $HOME/.magic
112 for magic entries.
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 .Dq Li text
126 because they will be mostly readable on nearly any terminal;
127 UTF-16 and EBCDIC are only
128 .Dq Li "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
137 .Ux Ns -standard
138 LF, this will be reported.
139 Files that contain embedded escape sequences or overstriking
140 will also be identified.
141 .Pp
142 Once
143 .Nm
144 has determined the character set used in a text-type file,
145 it will
146 attempt to determine in what language the file is written.
147 The language tests look for particular strings (cf
148 .Pa names.h )
149 that can appear anywhere in the first few blocks of a file.
150 For example, the keyword
151 .Ic .br
152 indicates that the file is most likely a
153 .Xr troff 1
154 input file, just as the keyword
155 .Ic struct
156 indicates a C program.
157 These tests are less reliable than the previous
158 two groups, so they are performed last.
159 The language test routines also test for some miscellany
160 (such as
161 .Xr tar 1
162 archives).
163 .Pp
164 Any file that cannot be identified as having been written
165 in any of the character sets listed above is simply said to be
166 .Dq Li data .
167 .Sh OPTIONS
168 .Bl -tag -width indent
169 .It Fl b , -brief
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
174 .Fl m
175 to debug a new magic file before installing it.
176 .It Fl C , -compile
177 Write a
178 .Pa magic.mgc
179 output file that contains a pre-parsed version of
180 file.
181 .It Fl f , -files-from Ar namefile
182 Read the names of the files to be examined from
183 .Ar namefile
184 (one per line)
185 before the argument list.
186 Either
187 .Ar namefile
188 or at least one filename argument must be present;
189 to test the standard input, use
190 .Dq Fl
191 as a filename argument.
192 .It Fl F , -separator Ar separator
193 Use the specified string as the separator between the filename and the
194 file result returned.
195 Defaults to
196 .Ql \&: .
197 .It Fl h , -no-dereference
198 Causes symlinks not to be followed
199 (on systems that support symbolic links).
200 This is the default if the
201 environment variable
202 .Ev POSIXLY_CORRECT
203 is not defined.
204 .It Fl i , -mime
205 Causes the file command to output mime type strings rather than the more
206 traditional human readable ones.
207 Thus it may say
208 .Dq Li "text/plain; charset=us-ascii"
209 rather than
210 .Dq Li "ASCII text" .
211 In order for this option to work, file changes the way
212 it handles files recognised by the command itself (such as many of the
213 text file types, directories etc), and makes use of an alternative
214 .Pa magic
215 file.
216 (See
217 .Sx FILES
218 section, below).
219 .It Fl k , -keep-going
220 Do not stop at the first match, keep going.
221 .It Fl L , -dereference
222 option causes symlinks to be followed, as the like-named option in
223 .Xr ls 1
224 (on systems that support symbolic links).
225 This is the default if the environment variable
226 .Ev POSIXLY_CORRECT
227 is defined.
228 .It Fl m , -magic-file Ar list
229 Specify an alternate list of files containing magic numbers.
230 This can be a single file, or a colon-separated list of files.
231 If a compiled magic file is found alongside, it will be used instead.
232 With the
233 .Fl i
234 or
235 .Fl -mime
236 option, the program adds
237 .Pa .mime
238 to each file name.
239 .It Fl n , -no-buffer
240 Force stdout to be flushed after checking each file.
241 This is only useful if checking a list of files.
242 It is intended to be used by programs that want
243 filetype output from a pipe.
244 .It Fl N , -no-pad
245 Do not pad filenames so that they align in the output.
246 .It Fl p , -preserve-date
247 On systems that support
248 .Xr utime 3
249 or
250 .Xr utimes 2 ,
251 attempt to preserve the access time of files analyzed, to pretend that
252 .Nm
253 never read them.
254 .It Fl r , -raw
255 Do not translate unprintable characters to \eooo.
256 Normally
257 .Nm
258 translates unprintable characters to their octal representation.
259 .It Fl s , -special-files
260 Normally,
261 .Nm
262 only attempts to read and determine the type of argument files which
263 .Xr stat 2
264 reports are ordinary files.
265 This prevents problems, because reading special files may have peculiar
266 consequences.
267 Specifying the
268 .Fl s
269 option causes
270 .Nm
271 to also read argument files which are block or character special files.
272 This is useful for determining the file system types of the data in raw
273 disk partitions, which are block special files.
274 This option also causes
275 .Nm
276 to disregard the file size as reported by
277 .Xr stat 2
278 since on some systems it reports a zero size for raw disk partitions.
279 .It Fl v , -version
280 Print the version of the program and exit.
281 .It Fl z , -uncompress
282 Try to look inside compressed files.
283 .It Fl -help
284 Print a help message and exit.
285 .El
286 .Sh FILES
287 .Bl -tag -width ".Pa /usr/share/misc/magic.mime" -compact
288 .It Pa /usr/share/misc/magic.mgc
289 Default compiled list of magic numbers
290 .It Pa /usr/share/misc/magic
291 Default list of magic numbers
292 .It Pa /usr/share/misc/magic.mime.mgc
293 Default compiled list of magic numbers, used to output mime types when
294 the
295 .Fl i
296 option is specified.
297 .It Pa /usr/share/misc/magic.mime
298 Default list of magic numbers, used to output mime types when the
299 .Fl i
300 option is specified.
301 .El
302 .Sh ENVIRONMENT
303 The environment variable
304 .Ev MAGIC
305 can be used to set the default magic number file name.
306 If that variable is set, then
307 .Nm
308 will not attempt to open
309 .Pa $HOME/.magic .
310 .Nm
311 adds
312 .Pa .mime
313 and/or
314 .Pa .mgc
315 to the value of this variable as appropriate.
316 The environment variable
317 .Ev POSIXLY_CORRECT
318 controls (on systems that support symbolic links), if
319 .Nm
320 will attempt to follow symlinks or not.
321 If set, then
322 .Nm
323 follows symlink, otherwise it does not.
324 This is also controlled
325 by the
326 .Fl L
327 and
328 .Fl h
329 options.
330 .Sh SEE ALSO
331 .Xr hexdump 1 ,
332 .Xr od 1 ,
333 .Xr strings 1 ,
334 .Xr magic 5
335 .Sh STANDARDS CONFORMANCE
336 This program is believed to exceed the
337 .St -svid4
338 of FILE(CMD), as near as one can determine from the vague language
339 contained therein.
340 Its behaviour is mostly compatible with the System V program of the same name.
341 This version knows more magic, however, so it will produce
342 different (albeit more accurate) output in many cases.
343 .Pp
344 The one significant difference
345 between this version and System V
346 is that this version treats any white space
347 as a delimiter, so that spaces in pattern strings must be escaped.
348 For example,
349 .Pp
350 .Dl ">10        string  language impress\       (imPRESS data)"
351 .Pp
352 in an existing magic file would have to be changed to
353 .Pp
354 .Dl ">10        string  language\e impress      (imPRESS data)"
355 .Pp
356 In addition, in this version, if a pattern string contains a backslash,
357 it must be escaped.
358 For example
359 .Pp
360 .Dl "0  string          \ebegindata     Andrew Toolkit document"
361 .Pp
362 in an existing magic file would have to be changed to
363 .Pp
364 .Dl "0  string          \e\ebegindata   Andrew Toolkit document"
365 .Pp
366 SunOS releases 3.2 and later from Sun Microsystems include a
367 .Xr file 1
368 command derived from the System V one, but with some extensions.
369 My version differs from Sun's only in minor ways.
370 It includes the extension of the
371 .Sq Ic &
372 operator, used as,
373 for example,
374 .Pp
375 .Dl ">16        long&0x7fffffff >0              not stripped"
376 .Sh MAGIC DIRECTORY
377 The magic file entries have been collected from various sources,
378 mainly USENET, and contributed by various authors.
379 .An Christos Zoulas
380 (address below) will collect additional
381 or corrected magic file entries.
382 A consolidation of magic file entries
383 will be distributed periodically.
384 .Pp
385 The order of entries in the magic file is significant.
386 Depending on what system you are using, the order that
387 they are put together may be incorrect.
388 If your old
389 .Nm
390 command uses a magic file,
391 keep the old magic file around for comparison purposes
392 (rename it to
393 .Pa /usr/share/misc/magic.orig ) .
394 .Sh EXAMPLES
395 .Bd -literal
396 $ file file.c file /dev/{wd0a,hda}
397 file.c:    C program text
398 file:      ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
399            dynamically linked (uses shared libs), stripped
400 /dev/wd0a: block special (0/0)
401 /dev/hda:  block special (3/0)
402 $ file -s /dev/wd0{b,d}
403 /dev/wd0b: data
404 /dev/wd0d: x86 boot sector
405 $ file -s /dev/hda{,1,2,3,4,5,6,7,8,9,10}
406 /dev/hda:   x86 boot sector
407 /dev/hda1:  Linux/i386 ext2 filesystem
408 /dev/hda2:  x86 boot sector
409 /dev/hda3:  x86 boot sector, extended partition table
410 /dev/hda4:  Linux/i386 ext2 filesystem
411 /dev/hda5:  Linux/i386 swap file
412 /dev/hda6:  Linux/i386 swap file
413 /dev/hda7:  Linux/i386 swap file
414 /dev/hda8:  Linux/i386 swap file
415 /dev/hda9:  empty
416 /dev/hda10: empty
417
418 $ file -i file.c file /dev/{wd0a,hda}
419 file.c:    text/x-c
420 file:      application/x-executable, dynamically linked (uses shared libs),
421            not stripped
422 /dev/hda:  application/x-not-regular-file
423 /dev/wd0a: application/x-not-regular-file
424 .Ed
425 .Sh HISTORY
426 There has been a
427 .Nm
428 command in every
429 .Ux
430 since at least Research Version 4
431 (man page dated November, 1973).
432 The System V version introduced one significant major change:
433 the external list of magic number types.
434 This slowed the program down slightly but made it a lot more flexible.
435 .Pp
436 This program, based on the System V version,
437 was written by
438 .An Ian Darwin Aq ian@darwinsys.com
439 without looking at anybody else's source code.
440 .Pp
441 .An John Gilmore
442 revised the code extensively, making it better than
443 the first version.
444 .An Geoff Collyer
445 found several inadequacies
446 and provided some magic file entries.
447 Contributions by the
448 .Sq Ic &
449 operator by
450 .An Rob McMahon Aq cudcv@warwick.ac.uk ,
451 1989.
452 .Pp
453 .An Guy Harris Aq guy@netapp.com ,
454 made many changes from 1993 to the present.
455 .Pp
456 Primary development and maintenance from 1990 to the present by
457 .An Christos Zoulas Aq christos@astron.com .
458 .Pp
459 Altered by
460 .An Chris Lowth Aq chris@lowth.com ,
461 2000:
462 Handle the
463 .Fl i
464 option to output mime type strings and using an alternative
465 magic file and internal logic.
466 .Pp
467 Altered by
468 .An Eric Fischer Aq enf@pobox.com ,
469 July, 2000,
470 to identify character codes and attempt to identify the languages
471 of
472 .No non- Ns Tn ASCII
473 files.
474 .Pp
475 The list of contributors to the
476 .Pa Magdir
477 directory (source for the
478 .Pa /usr/share/misc/magic
479 file) is too long to include here.
480 You know who you are; thank you.
481 .Sh LEGAL NOTICE
482 Copyright (c)
483 .An Ian F. Darwin ,
484 Toronto, Canada, 1986-1999.
485 Covered by the standard Berkeley Software Distribution copyright; see the file
486 .Pa LEGAL.NOTICE
487 in the source distribution.
488 .Pp
489 The files
490 .Pa tar.h
491 and
492 .Pa is_tar.c
493 were written by
494 .An John Gilmore
495 from his public-domain
496 .Nm tar
497 program, and are not covered by the above license.
498 .Sh BUGS
499 There must be a better way to automate the construction of the
500 .Pa Magic
501 file from all the glop in
502 .Pa Magdir .
503 What is it?
504 Better yet, the magic file should be compiled into binary (say,
505 .Xr ndbm 3
506 or, better yet, fixed-length
507 .Tn ASCII
508 strings for use in heterogenous network environments) for faster startup.
509 Then the program would run as fast as the Version 7 program of the same name,
510 with the flexibility of the System V version.
511 .Pp
512 The
513 .Nm
514 utility uses several algorithms that favor speed over accuracy,
515 thus it can be misled about the contents of
516 text
517 files.
518 .Pp
519 The support for
520 text
521 files (primarily for programming languages)
522 is simplistic, inefficient and requires recompilation to update.
523 .Pp
524 There should be an
525 .Ic else
526 clause to follow a series of continuation lines.
527 .Pp
528 The magic file and keywords should have regular expression support.
529 Their use of
530 .Tn "ASCII TAB"
531 as a field delimiter is ugly and makes
532 it hard to edit the files, but is entrenched.
533 .Pp
534 It might be advisable to allow upper-case letters in keywords
535 for e.g.,
536 .Xr troff 1
537 commands vs man page macros.
538 Regular expression support would make this easy.
539 .Pp
540 The program does not grok
541 .Tn FORTRAN .
542 It should be able to figure
543 .Tn FORTRAN
544 by seeing some keywords which
545 appear indented at the start of line.
546 Regular expression support would make this easy.
547 .Pp
548 The list of keywords in
549 .Pa ascmagic
550 probably belongs in the
551 .Pa Magic
552 file.
553 This could be done by using some keyword like
554 .Sq Ic *
555 for the offset value.
556 .Pp
557 Another optimisation would be to sort
558 the magic file so that we can just run down all the
559 tests for the first byte, first word, first long, etc, once we
560 have fetched it.
561 Complain about conflicts in the magic file entries.
562 Make a rule that the magic entries sort based on file offset rather
563 than position within the magic file?
564 .Pp
565 The program should provide a way to give an estimate
566 of
567 .Dq how good
568 a guess is.
569 We end up removing guesses (e.g.\&
570 .Dq Li "From "
571 as first 5 chars of file) because
572 they are not as good as other guesses (e.g.\&
573 .Dq Li "Newsgroups:"
574 versus
575 .Dq Li "Return-Path:" ) .
576 Still, if the others do not pan out, it should be possible to use the
577 first guess.
578 .Pp
579 This program is slower than some vendors' file commands.
580 The new support for multiple character codes makes it even slower.
581 .Pp
582 This manual page, and particularly this section, is too long.
583 .Sh AVAILABILITY
584 You can obtain the original author's latest version by anonymous FTP
585 on
586 .Pa ftp.astron.com
587 in the directory
588 .Pa /pub/file/file-X.YZ.tar.gz