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