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