14 KB

  1. .TH FILE __CSECTION__ "Copyright but distributable"
  2. .\" $Id:,v 1.39 2001/04/27 22:48:33 christos Exp $
  3. .SH NAME
  4. file
  5. \- determine file type
  7. .B file
  8. [
  9. .B \-bciknsvzL
  10. ]
  11. [
  12. .B \-f
  13. .I namefile
  14. ]
  15. [
  16. .B \-m
  17. .I magicfiles
  18. ]
  19. .I file
  20. \&...
  21. .br
  22. .B file
  23. .B -C
  24. [
  25. .B \-m
  26. magicfile ]
  28. This manual page documents version __VERSION__ of the
  29. .B file
  30. command.
  31. .PP
  32. .B File
  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 number tests, and language tests.
  36. The
  37. .I 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. .B 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. .SM ASCII
  46. terminal),
  47. .B executable
  48. (the file contains the result of compiling a program
  49. in a form understandable to some \s-1UNIX\s0 kernel or another),
  50. or
  51. .B data
  52. meaning anything else (data is usually `binary' or non-printable).
  53. Exceptions are well-known file formats (core files, tar archives)
  54. that are known to contain binary data.
  55. When modifying the file
  56. .I __MAGIC__
  57. or the program itself,
  58. .B "preserve these keywords" .
  59. People depend on knowing that all the readable files in a directory
  60. have the word ``text'' printed.
  61. Don't do as Berkeley did and change ``shell commands text''
  62. to ``shell script''.
  63. Note that the file
  64. .I __MAGIC__
  65. is built mechanically from a large number of small files in
  66. the subdirectory
  67. .I Magdir
  68. in the source distribution of this program.
  69. .PP
  70. The filesystem tests are based on examining the return from a
  71. .BR 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
  79. the system header file
  80. .IR <sys/stat.h> .
  81. .PP
  82. The magic number 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. .I a.out
  86. file, whose format is defined in
  87. .I a.out.h
  88. and possibly
  89. .I exec.h
  90. in the standard include directory.
  91. These files have a `magic number' stored in a particular place
  92. near the beginning of the file that tells the \s-1UNIX\s0 operating system
  93. that the file is a binary executable, and which of several types thereof.
  94. The concept of `magic number' has been applied by extension to data files.
  95. Any file with some invariant identifier at a small fixed
  96. offset into the file can usually be described in this way.
  97. The information identifying these files is read from the compiled
  98. magic file
  99. .I __MAGIC__.mgc ,
  100. or
  101. .I __MAGIC__
  102. if the compile file does not exist.
  103. .PP
  104. If a file does not match any of the entries in the magic file,
  105. it is examined to see if it seems to be a text file.
  106. ASCII, ISO-8859-x, non-ISO 8-bit extended-ASCII character sets
  107. (such as those used on Macintosh and IBM PC systems),
  108. UTF-8-encoded Unicode, UTF-16-encoded Unicode, and EBCDIC
  109. character sets can be distinguished by the different
  110. ranges and sequences of bytes that constitute printable text
  111. in each set.
  112. If a file passes any of these tests, its character set is reported.
  113. ASCII, ISO-8859-x, UTF-8, and extended-ASCII files are identified
  114. as ``text'' because they will be mostly readable on nearly any terminal;
  115. UTF-16 and EBCDIC are only ``character data'' because, while
  116. they contain text, it is text that will require translation
  117. before it can be read.
  118. In addition,
  119. .B file
  120. will attempt to determine other characteristics of text-type files.
  121. If the lines of a file are terminated by CR, CRLF, or NEL, instead
  122. of the Unix-standard LF, this will be reported.
  123. Files that contain embedded escape sequences or overstriking
  124. will also be identified.
  125. .PP
  126. Once
  127. .B file
  128. has determined the character set used in a text-type file,
  129. it will
  130. attempt to determine in what language the file is written.
  131. The language tests look for particular strings (cf
  132. .IR names.h )
  133. that can appear anywhere in the first few blocks of a file.
  134. For example, the keyword
  135. .B .br
  136. indicates that the file is most likely a
  137. .BR troff (1)
  138. input file, just as the keyword
  139. .B struct
  140. indicates a C program.
  141. These tests are less reliable than the previous
  142. two groups, so they are performed last.
  143. The language test routines also test for some miscellany
  144. (such as
  145. .BR tar (1)
  146. archives).
  147. .PP
  148. Any file that cannot be identified as having been written
  149. in any of the character sets listed above is simply said to be ``data''.
  150. .SH OPTIONS
  151. .TP 8
  152. .B \-b
  153. Do not prepend filenames to output lines (brief mode).
  154. .TP 8
  155. .B \-c
  156. Cause a checking printout of the parsed form of the magic file.
  157. This is usually used in conjunction with
  158. .B \-m
  159. to debug a new magic file before installing it.
  160. .TP 8
  161. .B \-C
  162. Write a magic.mgc output file that contains a pre-parsed version of
  163. file.
  164. .TP 8
  165. .BI \-f " namefile"
  166. Read the names of the files to be examined from
  167. .I namefile
  168. (one per line)
  169. before the argument list.
  170. Either
  171. .I namefile
  172. or at least one filename argument must be present;
  173. to test the standard input, use ``\-'' as a filename argument.
  174. .TP 8
  175. .B \-i
  176. Causes the file command to output mime type strings rather than the more
  177. traditional human readable ones. Thus it may say
  178. ``text/plain; charset=us-ascii''
  179. rather
  180. than ``ASCII text''. In order for this option to work, file changes the way
  181. it handles files recognised by the command itself (such as many of the
  182. text file types, directories etc), and makes use of an alternative
  183. ``magic'' file.
  184. (See ``FILES'' section, below).
  185. .TP 8
  186. .B \-k
  187. Don't stop at the first match, keep going.
  188. .TP 8
  189. .BI \-m " list"
  190. Specify an alternate list of files containing magic numbers.
  191. This can be a single file, or a colon-separated list of files.
  192. .TP 8
  193. .B \-n
  194. Force stdout to be flushed after checking each file. This is only useful if
  195. checking a list of files. It is intended to be used by programs that want
  196. filetype output from a pipe.
  197. .TP 8
  198. .B \-v
  199. Print the version of the program and exit.
  200. .TP 8
  201. .B \-z
  202. Try to look inside compressed files.
  203. .TP 8
  204. .B \-L
  205. option causes symlinks to be followed, as the like-named option in
  206. .BR ls (1).
  207. (on systems that support symbolic links).
  208. .TP 8
  209. .B \-s
  210. Normally,
  211. .B file
  212. only attempts to read and determine the type of argument files which
  213. .BR stat (2)
  214. reports are ordinary files.
  215. This prevents problems, because reading special files may have peculiar
  216. consequences.
  217. Specifying the
  218. .BR \-s
  219. option causes
  220. .B file
  221. to also read argument files which are block or character special files.
  222. This is useful for determining the filesystem types of the data in raw
  223. disk partitions, which are block special files.
  224. This option also causes
  225. .B file
  226. to disregard the file size as reported by
  227. .BR stat (2)
  228. since on some systems it reports a zero size for raw disk partitions.
  229. .SH FILES
  230. .I __MAGIC__.mgc
  231. \- defaults compiled list of magic numbers
  232. .PP
  233. .I __MAGIC__
  234. \- default list of magic numbers
  235. .PP
  236. .I __MAGIC__.mime
  237. \- default list of magic numbers, used to output mime types when the -i option
  238. is specified.
  240. The environment variable
  241. .B MAGIC
  242. can be used to set the default magic number files.
  243. .SH SEE ALSO
  244. .BR magic (__FSECTION__)
  245. \- description of magic file format.
  246. .br
  247. .BR strings (1), " od" (1), " hexdump(1)"
  248. \- tools for examining non-textfiles.
  250. This program is believed to exceed the System V Interface Definition
  251. of FILE(CMD), as near as one can determine from the vague language
  252. contained therein.
  253. Its behaviour is mostly compatible with the System V program of the same name.
  254. This version knows more magic, however, so it will produce
  255. different (albeit more accurate) output in many cases.
  256. .PP
  257. The one significant difference
  258. between this version and System V
  259. is that this version treats any white space
  260. as a delimiter, so that spaces in pattern strings must be escaped.
  261. For example,
  262. .br
  263. >10 string language impress\ (imPRESS data)
  264. .br
  265. in an existing magic file would have to be changed to
  266. .br
  267. >10 string language\e impress (imPRESS data)
  268. .br
  269. In addition, in this version, if a pattern string contains a backslash,
  270. it must be escaped. For example
  271. .br
  272. 0 string \ebegindata Andrew Toolkit document
  273. .br
  274. in an existing magic file would have to be changed to
  275. .br
  276. 0 string \e\ebegindata Andrew Toolkit document
  277. .br
  278. .PP
  279. SunOS releases 3.2 and later from Sun Microsystems include a
  280. .BR file (1)
  281. command derived from the System V one, but with some extensions.
  282. My version differs from Sun's only in minor ways.
  283. It includes the extension of the `&' operator, used as,
  284. for example,
  285. .br
  286. >16 long&0x7fffffff >0 not stripped
  288. The magic file entries have been collected from various sources,
  289. mainly USENET, and contributed by various authors.
  290. Christos Zoulas (address below) will collect additional
  291. or corrected magic file entries.
  292. A consolidation of magic file entries
  293. will be distributed periodically.
  294. .PP
  295. The order of entries in the magic file is significant.
  296. Depending on what system you are using, the order that
  297. they are put together may be incorrect.
  298. If your old
  299. .B file
  300. command uses a magic file,
  301. keep the old magic file around for comparison purposes
  302. (rename it to
  303. .IR __MAGIC__.orig ).
  305. .nf
  306. $ file file.c file /dev/hda
  307. file.c: C program text
  308. file: ELF 32-bit LSB executable, Intel 80386, version 1,
  309. dynamically linked, not stripped
  310. /dev/hda: block special
  311. $ file -s /dev/hda{,1,2,3,4,5,6,7,8,9,10}
  312. /dev/hda: x86 boot sector
  313. /dev/hda1: Linux/i386 ext2 filesystem
  314. /dev/hda2: x86 boot sector
  315. /dev/hda3: x86 boot sector, extended partition table
  316. /dev/hda4: Linux/i386 ext2 filesystem
  317. /dev/hda5: Linux/i386 swap file
  318. /dev/hda6: Linux/i386 swap file
  319. /dev/hda7: Linux/i386 swap file
  320. /dev/hda8: Linux/i386 swap file
  321. /dev/hda9: empty
  322. /dev/hda10: empty
  323. $ file -i file.c file /dev/hda
  324. file.c: text/x-c
  325. file: application/x-executable, dynamically linked (uses shared libs), not stripped
  326. /dev/hda: application/x-not-regular-file
  327. .fi
  328. .SH HISTORY
  329. There has been a
  330. .B file
  331. command in every \s-1UNIX\s0 since at least Research Version 6
  332. (man page dated January 16, 1975).
  333. The System V version introduced one significant major change:
  334. the external list of magic number types.
  335. This slowed the program down slightly but made it a lot more flexible.
  336. .PP
  337. This program, based on the System V version,
  338. was written by Ian Darwin <>
  339. without looking at anybody else's source code.
  340. .PP
  341. John Gilmore revised the code extensively, making it better than
  342. the first version.
  343. Geoff Collyer found several inadequacies
  344. and provided some magic file entries.
  345. Contributions by the `&' operator by Rob McMahon,, 1989.
  346. .PP
  347. Guy Harris,, made many changes from 1993 to the present.
  348. .PP
  349. Primary development and maintenance from 1990 to the present by
  350. Christos Zoulas (
  351. .PP
  352. Altered by Chris Lowth,, 2000:
  353. Handle the ``-i'' option to output mime type strings and using an alternative
  354. magic file and internal logic.
  355. .PP
  356. Altered by Eric Fischer (, July, 2000,
  357. to identify character codes and attempt to identify the languages
  358. of non-ASCII files.
  359. .PP
  360. The list of contributors to the "Magdir" directory (source for the
  361. /etc/magic
  362. file) is too long to include here. You know who you are; thank you.
  364. Copyright (c) Ian F. Darwin, Toronto, Canada, 1986-1999.
  365. Covered by the standard Berkeley Software Distribution copyright; see the file
  366. LEGAL.NOTICE in the source distribution.
  367. .PP
  368. The files
  369. .I tar.h
  370. and
  371. .I is_tar.c
  372. were written by John Gilmore from his public-domain
  373. .B tar
  374. program, and are not covered by the above license.
  375. .SH BUGS
  376. There must be a better way to automate the construction of the Magic
  377. file from all the glop in Magdir. What is it?
  378. Better yet, the magic file should be compiled into binary (say,
  379. .BR ndbm (3)
  380. or, better yet, fixed-length
  381. .SM ASCII
  382. strings for use in heterogenous network environments) for faster startup.
  383. Then the program would run as fast as the Version 7 program of the same name,
  384. with the flexibility of the System V version.
  385. .PP
  386. .B File
  387. uses several algorithms that favor speed over accuracy,
  388. thus it can be misled about the contents of
  389. text
  390. files.
  391. .PP
  392. The support for
  393. text
  394. files (primarily for programming languages)
  395. is simplistic, inefficient and requires recompilation to update.
  396. .PP
  397. There should be an ``else'' clause to follow a series of continuation lines.
  398. .PP
  399. The magic file and keywords should have regular expression support.
  400. Their use of
  401. .SM "ASCII TAB"
  402. as a field delimiter is ugly and makes
  403. it hard to edit the files, but is entrenched.
  404. .PP
  405. It might be advisable to allow upper-case letters in keywords
  406. for e.g.,
  407. .BR troff (1)
  408. commands vs man page macros.
  409. Regular expression support would make this easy.
  410. .PP
  411. The program doesn't grok \s-2FORTRAN\s0.
  412. It should be able to figure \s-2FORTRAN\s0 by seeing some keywords which
  413. appear indented at the start of line.
  414. Regular expression support would make this easy.
  415. .PP
  416. The list of keywords in
  417. .I ascmagic
  418. probably belongs in the Magic file.
  419. This could be done by using some keyword like `*' for the offset value.
  420. .PP
  421. Another optimisation would be to sort
  422. the magic file so that we can just run down all the
  423. tests for the first byte, first word, first long, etc, once we
  424. have fetched it. Complain about conflicts in the magic file entries.
  425. Make a rule that the magic entries sort based on file offset rather
  426. than position within the magic file?
  427. .PP
  428. The program should provide a way to give an estimate
  429. of ``how good'' a guess is.
  430. We end up removing guesses (e.g. ``From '' as first 5 chars of file) because
  431. they are not as good as other guesses (e.g. ``Newsgroups:'' versus
  432. ``Return-Path:''). Still, if the others don't pan out, it should be
  433. possible to use the first guess.
  434. .PP
  435. This program is slower than some vendors' file commands.
  436. The new support for multiple character codes makes it even slower.
  437. .PP
  438. This manual page, and particularly this section, is too long.
  440. You can obtain the original author's latest version by anonymous FTP
  441. on
  442. .B
  443. in the directory
  444. .I /pub/file/file-X.YY.tar.gz