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