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