xref: /freebsd/contrib/file/doc/file.man (revision c36e54bb328697af1e6113812caecbd3bac89fe0)
1.\" $File: file.man,v 1.117 2015/06/03 19:51:27 christos Exp $
2.Dd June 3, 2015
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 bcEhiklLNnprsvzZ0
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 E
185On filesystem errors (file not found etc), instead of handling the error
186as regular output as POSIX mandates and keep going, issue an error message
187and exit.
188.It Fl e , Fl Fl exclude Ar testname
189Exclude the test named in
190.Ar testname
191from the list of tests made to determine the file type.
192Valid test names are:
193.Bl -tag -width compress
194.It apptype
195.Dv EMX
196application type (only on EMX).
197.It ascii
198Various types of text files (this test will try to guess the text
199encoding, irrespective of the setting of the
200.Sq encoding
201option).
202.It encoding
203Different text encodings for soft magic tests.
204.It tokens
205Ignored for backwards compatibility.
206.It cdf
207Prints details of Compound Document Files.
208.It compress
209Checks for, and looks inside, compressed files.
210.It elf
211Prints ELF file details.
212.It soft
213Consults magic files.
214.It tar
215Examines tar files.
216.El
217.It Fl Fl extension
218Print a slash-separated list of valid extensions for the file type found.
219.It Fl F , Fl Fl separator Ar separator
220Use the specified string as the separator between the filename and the
221file result returned.
222Defaults to
223.Sq \&: .
224.It Fl f , Fl Fl files-from Ar namefile
225Read the names of the files to be examined from
226.Ar namefile
227(one per line)
228before the argument list.
229Either
230.Ar namefile
231or at least one filename argument must be present;
232to test the standard input, use
233.Sq -
234as a filename argument.
235Please note that
236.Ar namefile
237is unwrapped and the enclosed filenames are processed when this option is
238encountered and before any further options processing is done.
239This allows one to process multiple lists of files with different command line
240arguments on the same
241.Nm
242invocation.
243Thus if you want to set the delimiter, you need to do it before you specify
244the list of files, like:
245.Dq Fl F Ar @ Fl f Ar namefile ,
246instead of:
247.Dq Fl f Ar namefile Fl F Ar @ .
248.It Fl h , Fl Fl no-dereference
249option causes symlinks not to be followed
250(on systems that support symbolic links).
251This is the default if the environment variable
252.Dv POSIXLY_CORRECT
253is not defined.
254.It Fl i , Fl Fl mime
255Causes the file command to output mime type strings rather than the more
256traditional human readable ones.
257Thus it may say
258.Sq text/plain; charset=us-ascii
259rather than
260.Dq ASCII text .
261.It Fl Fl mime-type , Fl Fl mime-encoding
262Like
263.Fl i ,
264but print only the specified element(s).
265.It Fl k , Fl Fl keep-going
266Don't stop at the first match, keep going.
267Subsequent matches will be
268have the string
269.Sq "\[rs]012\- "
270prepended.
271(If you want a newline, see the
272.Fl r
273option.)
274The magic pattern with the highest strength (see the
275.Fl l
276option) comes first.
277.It Fl l , Fl Fl list
278Shows a list of patterns and their strength sorted descending by
279.Xr magic 4
280strength
281which is used for the matching (see also the
282.Fl k
283option).
284.It Fl L , Fl Fl dereference
285option causes symlinks to be followed, as the like-named option in
286.Xr ls 1
287(on systems that support symbolic links).
288This is the default if the environment variable
289.Ev POSIXLY_CORRECT
290is defined.
291.It Fl m , Fl Fl magic-file Ar magicfiles
292Specify an alternate list of files and directories containing magic.
293This can be a single item, or a colon-separated list.
294If a compiled magic file is found alongside a file or directory,
295it will be used instead.
296.It Fl N , Fl Fl no-pad
297Don't pad filenames so that they align in the output.
298.It Fl n , Fl Fl no-buffer
299Force stdout to be flushed after checking each file.
300This is only useful if checking a list of files.
301It is intended to be used by programs that want filetype output from a pipe.
302.It Fl p , Fl Fl preserve-date
303On systems that support
304.Xr utime 3
305or
306.Xr utimes 2 ,
307attempt to preserve the access time of files analyzed, to pretend that
308.Nm
309never read them.
310.It Fl P , Fl Fl parameter Ar name=value
311Set various parameter limits.
312.Bl -column "elf_phnum" "Default" "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" -offset indent
313.It Sy "Name" Ta Sy "Default" Ta Sy "Explanation"
314.It Li indir Ta 15 Ta recursion limit for indirect magic
315.It Li name Ta 30 Ta use count limit for name/use magic
316.It Li elf_notes Ta 256 Ta max ELF notes processed
317.It Li elf_phnum Ta 128 Ta max ELF program sections processed
318.It Li elf_shnum Ta 32768 Ta max ELF sections processed
319.El
320.It Fl r , Fl Fl raw
321Don't translate unprintable characters to \eooo.
322Normally
323.Nm
324translates unprintable characters to their octal representation.
325.It Fl s , Fl Fl special-files
326Normally,
327.Nm
328only attempts to read and determine the type of argument files which
329.Xr stat 2
330reports are ordinary files.
331This prevents problems, because reading special files may have peculiar
332consequences.
333Specifying the
334.Fl s
335option causes
336.Nm
337to also read argument files which are block or character special files.
338This is useful for determining the filesystem types of the data in raw
339disk partitions, which are block special files.
340This option also causes
341.Nm
342to disregard the file size as reported by
343.Xr stat 2
344since on some systems it reports a zero size for raw disk partitions.
345.It Fl v , Fl Fl version
346Print the version of the program and exit.
347.It Fl z , Fl Fl uncompress
348Try to look inside compressed files.
349.It Fl Z , Fl Fl uncompress-noreport
350Try to look inside compressed files, but report information about the contents
351only not the compression.
352.It Fl 0 , Fl Fl print0
353Output a null character
354.Sq \e0
355after the end of the filename.
356Nice to
357.Xr cut 1
358the output.
359This does not affect the separator, which is still printed.
360.It Fl -help
361Print a help message and exit.
362.El
363.Sh FILES
364.Bl -tag -width __MAGIC__.mgc -compact
365.It Pa __MAGIC__.mgc
366Default compiled list of magic.
367.It Pa __MAGIC__
368Directory containing default magic files.
369.El
370.Sh ENVIRONMENT
371The environment variable
372.Ev MAGIC
373can be used to set the default magic file name.
374If that variable is set, then
375.Nm
376will not attempt to open
377.Pa $HOME/.magic .
378.Nm
379adds
380.Dq Pa .mgc
381to the value of this variable as appropriate.
382However,
383.Pa file
384has to exist in order for
385.Pa file.mime
386to be considered.
387The environment variable
388.Ev POSIXLY_CORRECT
389controls (on systems that support symbolic links), whether
390.Nm
391will attempt to follow symlinks or not.
392If set, then
393.Nm
394follows symlink, otherwise it does not.
395This is also controlled by the
396.Fl L
397and
398.Fl h
399options.
400.Sh SEE ALSO
401.Xr magic __FSECTION__ ,
402.Xr hexdump 1 ,
403.Xr od 1 ,
404.Xr strings 1 ,
405.Xr fstyp 8
406.Sh STANDARDS CONFORMANCE
407This program is believed to exceed the System V Interface Definition
408of FILE(CMD), as near as one can determine from the vague language
409contained therein.
410Its behavior is mostly compatible with the System V program of the same name.
411This version knows more magic, however, so it will produce
412different (albeit more accurate) output in many cases.
413.\" URL: http://www.opengroup.org/onlinepubs/009695399/utilities/file.html
414.Pp
415The one significant difference
416between this version and System V
417is that this version treats any white space
418as a delimiter, so that spaces in pattern strings must be escaped.
419For example,
420.Bd -literal -offset indent
421\*[Gt]10	string	language impress\ 	(imPRESS data)
422.Ed
423.Pp
424in an existing magic file would have to be changed to
425.Bd -literal -offset indent
426\*[Gt]10	string	language\e impress	(imPRESS data)
427.Ed
428.Pp
429In addition, in this version, if a pattern string contains a backslash,
430it must be escaped.
431For example
432.Bd -literal -offset indent
4330	string		\ebegindata	Andrew Toolkit document
434.Ed
435.Pp
436in an existing magic file would have to be changed to
437.Bd -literal -offset indent
4380	string		\e\ebegindata	Andrew Toolkit document
439.Ed
440.Pp
441SunOS releases 3.2 and later from Sun Microsystems include a
442.Nm
443command derived from the System V one, but with some extensions.
444This version differs from Sun's only in minor ways.
445It includes the extension of the
446.Sq \*[Am]
447operator, used as,
448for example,
449.Bd -literal -offset indent
450\*[Gt]16	long\*[Am]0x7fffffff	\*[Gt]0		not stripped
451.Ed
452.Sh MAGIC DIRECTORY
453The magic file entries have been collected from various sources,
454mainly USENET, and contributed by various authors.
455Christos Zoulas (address below) will collect additional
456or corrected magic file entries.
457A consolidation of magic file entries
458will be distributed periodically.
459.Pp
460The order of entries in the magic file is significant.
461Depending on what system you are using, the order that
462they are put together may be incorrect.
463If your old
464.Nm
465command uses a magic file,
466keep the old magic file around for comparison purposes
467(rename it to
468.Pa __MAGIC__.orig ) .
469.Sh EXAMPLES
470.Bd -literal -offset indent
471$ file file.c file /dev/{wd0a,hda}
472file.c:   C program text
473file:     ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
474          dynamically linked (uses shared libs), stripped
475/dev/wd0a: block special (0/0)
476/dev/hda: block special (3/0)
477
478$ file -s /dev/wd0{b,d}
479/dev/wd0b: data
480/dev/wd0d: x86 boot sector
481
482$ file -s /dev/hda{,1,2,3,4,5,6,7,8,9,10}
483/dev/hda:   x86 boot sector
484/dev/hda1:  Linux/i386 ext2 filesystem
485/dev/hda2:  x86 boot sector
486/dev/hda3:  x86 boot sector, extended partition table
487/dev/hda4:  Linux/i386 ext2 filesystem
488/dev/hda5:  Linux/i386 swap file
489/dev/hda6:  Linux/i386 swap file
490/dev/hda7:  Linux/i386 swap file
491/dev/hda8:  Linux/i386 swap file
492/dev/hda9:  empty
493/dev/hda10: empty
494
495$ file -i file.c file /dev/{wd0a,hda}
496file.c:      text/x-c
497file:        application/x-executable
498/dev/hda:    application/x-not-regular-file
499/dev/wd0a:   application/x-not-regular-file
500
501.Ed
502.Sh HISTORY
503There has been a
504.Nm
505command in every
506.Dv UNIX since at least Research Version 4
507(man page dated November, 1973).
508The System V version introduced one significant major change:
509the external list of magic types.
510This slowed the program down slightly but made it a lot more flexible.
511.Pp
512This program, based on the System V version,
513was written by Ian Darwin
514.Aq ian@darwinsys.com
515without looking at anybody else's source code.
516.Pp
517John Gilmore revised the code extensively, making it better than
518the first version.
519Geoff Collyer found several inadequacies
520and provided some magic file entries.
521Contributions by the
522.Sq \*[Am]
523operator by Rob McMahon,
524.Aq cudcv@warwick.ac.uk ,
5251989.
526.Pp
527Guy Harris,
528.Aq guy@netapp.com ,
529made many changes from 1993 to the present.
5301989.
531.Pp
532Primary development and maintenance from 1990 to the present by
533Christos Zoulas
534.Aq christos@astron.com .
535.Pp
536Altered by Chris Lowth
537.Aq chris@lowth.com ,
5382000: handle the
539.Fl i
540option to output mime type strings, using an alternative
541magic file and internal logic.
542.Pp
543Altered by Eric Fischer
544.Aq enf@pobox.com ,
545July, 2000,
546to identify character codes and attempt to identify the languages
547of non-ASCII files.
548.Pp
549Altered by Reuben Thomas
550.Aq rrt@sc3d.org ,
5512007-2011, to improve MIME support, merge MIME and non-MIME magic,
552support directories as well as files of magic, apply many bug fixes,
553update and fix a lot of magic, improve the build system, improve the
554documentation, and rewrite the Python bindings in pure Python.
555.Pp
556The list of contributors to the
557.Sq magic
558directory (magic files)
559is too long to include here.
560You know who you are; thank you.
561Many contributors are listed in the source files.
562.Sh LEGAL NOTICE
563Copyright (c) Ian F. Darwin, Toronto, Canada, 1986-1999.
564Covered by the standard Berkeley Software Distribution copyright; see the file
565COPYING in the source distribution.
566.Pp
567The files
568.Pa tar.h
569and
570.Pa is_tar.c
571were written by John Gilmore from his public-domain
572.Xr tar 1
573program, and are not covered by the above license.
574.Sh RETURN CODE
575.Nm
576returns 0 on success, and non-zero on error.
577.Sh BUGS
578.Pp
579Please report bugs and send patches to the bug tracker at
580.Pa http://bugs.gw.com/
581or the mailing list at
582.Aq file@mx.gw.com
583(visit
584.Pa http://mx.gw.com/mailman/listinfo/file
585first to subscribe).
586.Sh TODO
587.Pp
588Fix output so that tests for MIME and APPLE flags are not needed all
589over the place, and actual output is only done in one place.
590This needs a design.
591Suggestion: push possible outputs on to a list, then pick the
592last-pushed (most specific, one hopes) value at the end, or
593use a default if the list is empty.
594This should not slow down evaluation.
595.Pp
596The handling of
597.Dv MAGIC_CONTINUE
598and printing \e012- between entries is clumsy and complicated; refactor
599and centralize.
600.Pp
601Some of the encoding logic is hard-coded in encoding.c and can be moved
602to the magic files if we had a !:charset annotation
603.Pp
604Continue to squash all magic bugs.
605See Debian BTS for a good source.
606.Pp
607Store arbitrarily long strings, for example for %s patterns, so that
608they can be printed out.
609Fixes Debian bug #271672.
610This can be done by allocating strings in a string pool, storing the
611string pool at the end of the magic file and converting all the string
612pointers to relative offsets from the string pool.
613.Pp
614Add syntax for relative offsets after current level (Debian bug #466037).
615.Pp
616Make file -ki work, i.e. give multiple MIME types.
617.Pp
618Add a zip library so we can peek inside Office2007 documents to
619print more details about their contents.
620.Pp
621Add an option to print URLs for the sources of the file descriptions.
622.Pp
623Combine script searches and add a way to map executable names to MIME
624types (e.g. have a magic value for !:mime which causes the resulting
625string to be looked up in a table).
626This would avoid adding the same magic repeatedly for each new
627hash-bang interpreter.
628.Pp
629When a file descriptor is available, we can skip and adjust the buffer
630instead of the hacky buffer management we do now.
631.Pp
632Fix
633.Dq name
634and
635.Dq use
636to check for consistency at compile time (duplicate
637.Dq name ,
638.Dq use
639pointing to undefined
640.Dq name
641).
642Make
643.Dq name
644/
645.Dq use
646more efficient by keeping a sorted list of names.
647Special-case ^ to flip endianness in the parser so that it does not
648have to be escaped, and document it.
649.Pp
650If the offsets specified internally in the file exceed the buffer size
651(
652.Dv HOWMANY
653variable in file.h), then we don't seek to that offset, but we give up.
654It would be better if buffer managements was done when the file descriptor
655is available so move around the file.
656One must be careful though because this has performance (and thus security
657considerations).
658.Sh AVAILABILITY
659You can obtain the original author's latest version by anonymous FTP
660on
661.Pa ftp.astron.com
662in the directory
663.Pa /pub/file/file-X.YZ.tar.gz .
664