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