19 KB

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