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