file.man 18 KB

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