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