file.man 18 KB

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