xref: /dflybsd-src/contrib/file/doc/file.man (revision 884717e1debcf4b08bda1d29d01b0c8a34b86a59)
1.\" $File: file.man,v 1.96 2011/07/12 11:23:38 rrt Exp $
2.Dd April 20, 2011
3.Dt FILE __CSECTION__
4.Os
5.Sh NAME
6.Nm file
7.Nd determine file type
8.Sh SYNOPSIS
9.Nm
10.Bk -words
11.Op Fl bchiklLNnprsvz0
12.Op Fl Fl apple
13.Op Fl Fl mime-encoding
14.Op Fl Fl mime-type
15.Op Fl e Ar testname
16.Op Fl F Ar separator
17.Op Fl f Ar namefile
18.Op Fl m Ar magicfiles
19.Ar
20.Ek
21.Nm
22.Fl C
23.Op Fl m Ar magicfiles
24.Nm
25.Op Fl Fl help
26.Sh DESCRIPTION
27This manual page documents version __VERSION__ of the
28.Nm
29command.
30.Pp
31.Nm
32tests each argument in an attempt to classify it.
33There are three sets of tests, performed in this order:
34filesystem tests, magic tests, and language tests.
35The
36.Em first
37test that succeeds causes the file type to be printed.
38.Pp
39The type printed will usually contain one of the words
40.Em text
41(the file contains only
42printing characters and a few common control
43characters and is probably safe to read on an
44.Dv ASCII
45terminal),
46.Em executable
47(the file contains the result of compiling a program
48in a form understandable to some
49.Tn UNIX
50kernel or another),
51or
52.Em data
53meaning anything else (data is usually
54.Dq binary
55or non-printable).
56Exceptions are well-known file formats (core files, tar archives)
57that are known to contain binary data.
58When modifying magic files or the program itself, make sure to
59.Em "preserve these keywords" .
60Users depend on knowing that all the readable files in a directory
61have the word
62.Dq text
63printed.
64Don't do as Berkeley did and change
65.Dq shell commands text
66to
67.Dq shell script .
68.Pp
69The filesystem tests are based on examining the return from a
70.Xr stat 2
71system call.
72The program checks to see if the file is empty,
73or if it's some sort of special file.
74Any known file types appropriate to the system you are running on
75(sockets, symbolic links, or named pipes (FIFOs) on those systems that
76implement them)
77are intuited if they are defined in the system header file
78.In sys/stat.h .
79.Pp
80The magic 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.Dv a.out
84file, whose format is defined in
85.In elf.h ,
86.In a.out.h
87and possibly
88.In exec.h
89in the standard include directory.
90These files have a
91.Dq "magic number"
92stored in a particular place
93near the beginning of the file that tells the
94.Tn UNIX
95operating system
96that the file is a binary executable, and which of several types thereof.
97The concept of a
98.Dq "magic"
99has been applied by extension to data files.
100Any file with some invariant identifier at a small fixed
101offset into the file can usually be described in this way.
102The information identifying these files is read from the compiled
103magic file
104.Pa __MAGIC__.mgc ,
105or the files in the directory
106.Pa __MAGIC__
107if the compiled file does not exist.
108In addition, if
109.Pa $HOME/.magic.mgc
110or
111.Pa $HOME/.magic
112exists, it will be used in preference to the system magic files.
113.Pp
114If a file does not match any of the entries in the magic file,
115it is examined to see if it seems to be a text file.
116ASCII, ISO-8859-x, non-ISO 8-bit extended-ASCII character sets
117(such as those used on Macintosh and IBM PC systems),
118UTF-8-encoded Unicode, UTF-16-encoded Unicode, and EBCDIC
119character sets can be distinguished by the different
120ranges and sequences of bytes that constitute printable text
121in each set.
122If a file passes any of these tests, its character set is reported.
123ASCII, ISO-8859-x, UTF-8, and extended-ASCII files are identified
124as
125.Dq text
126because they will be mostly readable on nearly any terminal;
127UTF-16 and EBCDIC are only
128.Dq character data
129because, while
130they contain text, it is text that will require translation
131before it can be read.
132In addition,
133.Nm
134will attempt to determine other characteristics of text-type files.
135If the lines of a file are terminated by CR, CRLF, or NEL, instead
136of the Unix-standard LF, this will be reported.
137Files that contain embedded escape sequences or overstriking
138will also be identified.
139.Pp
140Once
141.Nm
142has determined the character set used in a text-type file,
143it will
144attempt to determine in what language the file is written.
145The language tests look for particular strings (cf.
146.In names.h )
147that can appear anywhere in the first few blocks of a file.
148For example, the keyword
149.Em .br
150indicates that the file is most likely a
151.Xr troff 1
152input file, just as the keyword
153.Em struct
154indicates a C program.
155These tests are less reliable than the previous
156two groups, so they are performed last.
157The language test routines also test for some miscellany
158(such as
159.Xr tar 1
160archives).
161.Pp
162Any file that cannot be identified as having been written
163in any of the character sets listed above is simply said to be
164.Dq data .
165.Sh OPTIONS
166.Bl -tag -width indent
167.It Fl b , Fl Fl brief
168Do not prepend filenames to output lines (brief mode).
169.It Fl C , Fl Fl compile
170Write a
171.Pa magic.mgc
172output file that contains a pre-parsed version of the magic file or directory.
173.It Fl c , Fl Fl checking-printout
174Cause a checking printout of the parsed form of the magic file.
175This is usually used in conjunction with the
176.Fl m
177flag to debug a new magic file before installing it.
178.It Fl e , Fl Fl exclude Ar testname
179Exclude the test named in
180.Ar testname
181from the list of tests made to determine the file type.
182Valid test names are:
183.Bl -tag -width compress
184.It apptype
185.Dv EMX
186application type (only on EMX).
187.It ascii
188Various types of text files (this test will try to guess the text
189encoding, irrespective of the setting of the
190.Sq encoding
191option).
192.It encoding
193Different text encodings for soft magic tests.
194.It tokens
195Looks for known tokens inside text files.
196.It cdf
197Prints details of Compound Document Files.
198.It compress
199Checks for, and looks inside, compressed files.
200.It elf
201Prints ELF file details.
202.It soft
203Consults magic files.
204.It tar
205Examines tar files.
206.El
207.It Fl F , Fl Fl separator Ar separator
208Use the specified string as the separator between the filename and the
209file result returned.
210Defaults to
211.Sq \&: .
212.It Fl f , Fl Fl files-from Ar namefile
213Read the names of the files to be examined from
214.Ar namefile
215(one per line)
216before the argument list.
217Either
218.Ar namefile
219or at least one filename argument must be present;
220to test the standard input, use
221.Sq -
222as a filename argument.
223.It Fl h , Fl Fl no-dereference
224option causes symlinks not to be followed
225(on systems that support symbolic links).
226This is the default if the environment variable
227.Dv POSIXLY_CORRECT
228is not defined.
229.It Fl i , Fl Fl mime
230Causes the file command to output mime type strings rather than the more
231traditional human readable ones.
232Thus it may say
233.Sq text/plain; charset=us-ascii
234rather than
235.Dq ASCII text .
236.It Fl Fl mime-type , Fl Fl mime-encoding
237Like
238.Fl i ,
239but print only the specified element(s).
240.It Fl k , Fl Fl keep-going
241Don't stop at the first match, keep going.
242Subsequent matches will be
243have the string
244.Sq "\[rs]012\- "
245prepended.
246(If you want a newline, see the
247.Fl r
248option.)
249.It Fl l , Fl Fl list
250Print information about the strength of each magic pattern.
251.It Fl L , Fl Fl dereference
252option causes symlinks to be followed, as the like-named option in
253.Xr ls 1
254(on systems that support symbolic links).
255This is the default if the environment variable
256.Ev POSIXLY_CORRECT
257is defined.
258.It Fl l
259Shows sorted patterns list in the order which is used for the matching.
260.It Fl m , Fl Fl magic-file Ar magicfiles
261Specify an alternate list of files and directories containing magic.
262This can be a single item, or a colon-separated list.
263If a compiled magic file is found alongside a file or directory,
264it will be used instead.
265.It Fl N , Fl Fl no-pad
266Don't pad filenames so that they align in the output.
267.It Fl n , Fl Fl no-buffer
268Force stdout to be flushed after checking each file.
269This is only useful if checking a list of files.
270It is intended to be used by programs that want filetype output from a pipe.
271.It Fl p , Fl Fl preserve-date
272On systems that support
273.Xr utime 3
274or
275.Xr utimes 2 ,
276attempt to preserve the access time of files analyzed, to pretend that
277.Nm
278never read them.
279.It Fl r , Fl Fl raw
280Don't translate unprintable characters to \eooo.
281Normally
282.Nm
283translates unprintable characters to their octal representation.
284.It Fl s , Fl Fl special-files
285Normally,
286.Nm
287only attempts to read and determine the type of argument files which
288.Xr stat 2
289reports are ordinary files.
290This prevents problems, because reading special files may have peculiar
291consequences.
292Specifying the
293.Fl s
294option causes
295.Nm
296to also read argument files which are block or character special files.
297This is useful for determining the filesystem types of the data in raw
298disk partitions, which are block special files.
299This option also causes
300.Nm
301to disregard the file size as reported by
302.Xr stat 2
303since on some systems it reports a zero size for raw disk partitions.
304.It Fl v , Fl Fl version
305Print the version of the program and exit.
306.It Fl z , Fl Fl uncompress
307Try to look inside compressed files.
308.It Fl 0 , Fl Fl print0
309Output a null character
310.Sq \e0
311after the end of the filename.
312Nice to
313.Xr cut 1
314the output.
315This does not affect the separator which is still printed.
316.It Fl -help
317Print a help message and exit.
318.El
319.Sh FILES
320.Bl -tag -width __MAGIC__.mgc -compact
321.It Pa __MAGIC__.mgc
322Default compiled list of magic.
323.It Pa __MAGIC__
324Directory containing default magic files.
325.El
326.Sh ENVIRONMENT
327The environment variable
328.Ev MAGIC
329can be used to set the default magic file name.
330If that variable is set, then
331.Nm
332will not attempt to open
333.Pa $HOME/.magic .
334.Nm
335adds
336.Dq Pa .mgc
337to the value of this variable as appropriate.
338However,
339.Pa file
340has to exist in order for
341.Pa file.mime
342to be considered.
343The environment variable
344.Ev POSIXLY_CORRECT
345controls (on systems that support symbolic links), whether
346.Nm
347will attempt to follow symlinks or not.
348If set, then
349.Nm
350follows symlink, otherwise it does not.
351This is also controlled by the
352.Fl L
353and
354.Fl h
355options.
356.Sh SEE ALSO
357.Xr magic __FSECTION__ ,
358.Xr hexdump 1 ,
359.Xr od 1 ,
360.Xr strings 1 ,
361.Sh STANDARDS CONFORMANCE
362This program is believed to exceed the System V Interface Definition
363of FILE(CMD), as near as one can determine from the vague language
364contained therein.
365Its behavior is mostly compatible with the System V program of the same name.
366This version knows more magic, however, so it will produce
367different (albeit more accurate) output in many cases.
368.\" URL: http://www.opengroup.org/onlinepubs/009695399/utilities/file.html
369.Pp
370The one significant difference
371between this version and System V
372is that this version treats any white space
373as a delimiter, so that spaces in pattern strings must be escaped.
374For example,
375.Bd -literal -offset indent
376\*[Gt]10	string	language impress\ 	(imPRESS data)
377.Ed
378.Pp
379in an existing magic file would have to be changed to
380.Bd -literal -offset indent
381\*[Gt]10	string	language\e impress	(imPRESS data)
382.Ed
383.Pp
384In addition, in this version, if a pattern string contains a backslash,
385it must be escaped.
386For example
387.Bd -literal -offset indent
3880	string		\ebegindata	Andrew Toolkit document
389.Ed
390.Pp
391in an existing magic file would have to be changed to
392.Bd -literal -offset indent
3930	string		\e\ebegindata	Andrew Toolkit document
394.Ed
395.Pp
396SunOS releases 3.2 and later from Sun Microsystems include a
397.Nm
398command derived from the System V one, but with some extensions.
399This version differs from Sun's only in minor ways.
400It includes the extension of the
401.Sq \*[Am]
402operator, used as,
403for example,
404.Bd -literal -offset indent
405\*[Gt]16	long\*[Am]0x7fffffff	\*[Gt]0		not stripped
406.Ed
407.Sh MAGIC DIRECTORY
408The magic file entries have been collected from various sources,
409mainly USENET, and contributed by various authors.
410Christos Zoulas (address below) will collect additional
411or corrected magic file entries.
412A consolidation of magic file entries
413will be distributed periodically.
414.Pp
415The order of entries in the magic file is significant.
416Depending on what system you are using, the order that
417they are put together may be incorrect.
418If your old
419.Nm
420command uses a magic file,
421keep the old magic file around for comparison purposes
422(rename it to
423.Pa __MAGIC__.orig ) .
424.Sh EXAMPLES
425.Bd -literal -offset indent
426$ file file.c file /dev/{wd0a,hda}
427file.c:   C program text
428file:     ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
429          dynamically linked (uses shared libs), stripped
430/dev/wd0a: block special (0/0)
431/dev/hda: block special (3/0)
432
433$ file -s /dev/wd0{b,d}
434/dev/wd0b: data
435/dev/wd0d: x86 boot sector
436
437$ file -s /dev/hda{,1,2,3,4,5,6,7,8,9,10}
438/dev/hda:   x86 boot sector
439/dev/hda1:  Linux/i386 ext2 filesystem
440/dev/hda2:  x86 boot sector
441/dev/hda3:  x86 boot sector, extended partition table
442/dev/hda4:  Linux/i386 ext2 filesystem
443/dev/hda5:  Linux/i386 swap file
444/dev/hda6:  Linux/i386 swap file
445/dev/hda7:  Linux/i386 swap file
446/dev/hda8:  Linux/i386 swap file
447/dev/hda9:  empty
448/dev/hda10: empty
449
450$ file -i file.c file /dev/{wd0a,hda}
451file.c:      text/x-c
452file:        application/x-executable
453/dev/hda:    application/x-not-regular-file
454/dev/wd0a:   application/x-not-regular-file
455
456.Ed
457.Sh HISTORY
458There has been a
459.Nm
460command in every
461.Dv UNIX since at least Research Version 4
462(man page dated November, 1973).
463The System V version introduced one significant major change:
464the external list of magic types.
465This slowed the program down slightly but made it a lot more flexible.
466.Pp
467This program, based on the System V version,
468was written by Ian Darwin
469.Aq ian@darwinsys.com
470without looking at anybody else's source code.
471.Pp
472John Gilmore revised the code extensively, making it better than
473the first version.
474Geoff Collyer found several inadequacies
475and provided some magic file entries.
476Contributions by the
477.Sq \*[Am]
478operator by Rob McMahon,
479.Aq cudcv@warwick.ac.uk ,
4801989.
481.Pp
482Guy Harris,
483.Aq guy@netapp.com ,
484made many changes from 1993 to the present.
4851989.
486.Pp
487Primary development and maintenance from 1990 to the present by
488Christos Zoulas
489.Aq christos@astron.com .
490.Pp
491Altered by Chris Lowth
492.Aq chris@lowth.com ,
4932000: handle the
494.Fl i
495option to output mime type strings, using an alternative
496magic file and internal logic.
497.Pp
498Altered by Eric Fischer
499.Aq enf@pobox.com ,
500July, 2000,
501to identify character codes and attempt to identify the languages
502of non-ASCII files.
503.Pp
504Altered by Reuben Thomas
505.Aq rrt@sc3d.org ,
5062007-2011, to improve MIME support, merge MIME and non-MIME magic,
507support directories as well as files of magic, apply many bug fixes,
508update and fix a lot of magic, improve the build system, improve the
509documentation, and rewrite the Python bindings in pure Python.
510.Pp
511The list of contributors to the
512.Sq magic
513directory (magic files)
514is too long to include here.
515You know who you are; thank you.
516Many contributors are listed in the source files.
517.Sh LEGAL NOTICE
518Copyright (c) Ian F. Darwin, Toronto, Canada, 1986-1999.
519Covered by the standard Berkeley Software Distribution copyright; see the file
520COPYING in the source distribution.
521.Pp
522The files
523.Pa tar.h
524and
525.Pa is_tar.c
526were written by John Gilmore from his public-domain
527.Xr tar 1
528program, and are not covered by the above license.
529.Sh RETURN CODE
530.Nm
531returns 0 on success, and non-zero on error.
532.Sh BUGS
533.Pp
534Please report bugs and send patches to the bug tracker at
535.Pa http://bugs.gw.com/
536or the mailing list at
537.Aq file@mx.gw.com .
538.Sh TODO
539.Pp
540Fix output so that tests for MIME and APPLE flags are not needed all
541over the place, and actual output is only done in one place. This
542needs a design. Suggestion: push possible outputs on to a list, then
543pick the last-pushed (most specific, one hopes) value at the end, or
544use a default if the list is empty. This should not slow down evaluation.
545.Pp
546Continue to squash all magic bugs. See Debian BTS for a good source.
547.Pp
548Store arbitrarily long strings, for example for %s patterns, so that
549they can be printed out. Fixes Debian bug #271672. Would require more
550complex store/load code in apprentice.
551.Pp
552Add syntax for relative offsets after current level (Debian bug #466037).
553.Pp
554Make file -ki work, i.e. give multiple MIME types.
555.Pp
556Add a zip library so we can peek inside Office2007 documents to
557figure out what they are.
558.Pp
559Add an option to print URLs for the sources of the file descriptions.
560.Sh AVAILABILITY
561You can obtain the original author's latest version by anonymous FTP
562on
563.Pa ftp.astron.com
564in the directory
565.Pa /pub/file/file-X.YZ.tar.gz .
566