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