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