20 KB

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