file.man 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. .TH FILE __CSECTION__ "Copyright but distributable"
  2. .\" $Id: file.man,v 1.33 1999/02/14 17:16:07 christos Exp $
  3. .SH NAME
  4. file
  5. \- determine file type
  6. .SH SYNOPSIS
  7. .B file
  8. [
  9. .B \-bcnsvzL
  10. ]
  11. [
  12. .B \-f
  13. namefile ]
  14. [
  15. .B \-m
  16. magicfiles ]
  17. file ...
  18. .SH DESCRIPTION
  19. This manual page documents version __VERSION__ of the
  20. .B file
  21. command.
  22. .B File
  23. tests each argument in an attempt to classify it.
  24. There are three sets of tests, performed in this order:
  25. filesystem tests, magic number tests, and language tests.
  26. The
  27. .I first
  28. test that succeeds causes the file type to be printed.
  29. .PP
  30. The type printed will usually contain one of the words
  31. .B text
  32. (the file contains only
  33. .SM ASCII
  34. characters and is probably safe to read on an
  35. .SM ASCII
  36. terminal),
  37. .B executable
  38. (the file contains the result of compiling a program
  39. in a form understandable to some \s-1UNIX\s0 kernel or another),
  40. or
  41. .B data
  42. meaning anything else (data is usually `binary' or non-printable).
  43. Exceptions are well-known file formats (core files, tar archives)
  44. that are known to contain binary data.
  45. When modifying the file
  46. .I __MAGIC__
  47. or the program itself,
  48. .B "preserve these keywords" .
  49. People depend on knowing that all the readable files in a directory
  50. have the word ``text'' printed.
  51. Don't do as Berkeley did \- change ``shell commands text''
  52. to ``shell script''.
  53. .PP
  54. The filesystem tests are based on examining the return from a
  55. .BR stat (2)
  56. system call.
  57. The program checks to see if the file is empty,
  58. or if it's some sort of special file.
  59. Any known file types appropriate to the system you are running on
  60. (sockets, symbolic links, or named pipes (FIFOs) on those systems that
  61. implement them)
  62. are intuited if they are defined in
  63. the system header file
  64. .IR sys/stat.h .
  65. .PP
  66. The magic number tests are used to check for files with data in
  67. particular fixed formats.
  68. The canonical example of this is a binary executable (compiled program)
  69. .I a.out
  70. file, whose format is defined in
  71. .I a.out.h
  72. and possibly
  73. .I exec.h
  74. in the standard include directory.
  75. These files have a `magic number' stored in a particular place
  76. near the beginning of the file that tells the \s-1UNIX\s0 operating system
  77. that the file is a binary executable, and which of several types thereof.
  78. The concept of `magic number' has been applied by extension to data files.
  79. Any file with some invariant identifier at a small fixed
  80. offset into the file can usually be described in this way.
  81. The information in these files is read from the magic file
  82. .I __MAGIC__.
  83. .PP
  84. If an argument appears to be an
  85. .SM ASCII
  86. file,
  87. .B file
  88. attempts to guess its language.
  89. The language tests look for particular strings (cf
  90. .IR names.h )
  91. that can appear anywhere in the first few blocks of a file.
  92. For example, the keyword
  93. .B .br
  94. indicates that the file is most likely a
  95. .BR troff (1)
  96. input file, just as the keyword
  97. .B struct
  98. indicates a C program.
  99. These tests are less reliable than the previous
  100. two groups, so they are performed last.
  101. The language test routines also test for some miscellany
  102. (such as
  103. .BR tar (1)
  104. archives) and determine whether an unknown file should be
  105. labelled as `ascii text' or `data'.
  106. .SH OPTIONS
  107. .TP 8
  108. .B \-b
  109. Do not prepend filenames to output lines (brief mode).
  110. .TP 8
  111. .B \-c
  112. Cause a checking printout of the parsed form of the magic file.
  113. This is usually used in conjunction with
  114. .B \-m
  115. to debug a new magic file before installing it.
  116. .TP 8
  117. .B \-f namefile
  118. Read the names of the files to be examined from
  119. .I namefile
  120. (one per line)
  121. before the argument list.
  122. Either
  123. .I namefile
  124. or at least one filename argument must be present;
  125. to test the standard input, use ``-'' as a filename argument.
  126. .TP 8
  127. .B \-m list
  128. Specify an alternate list of files containing magic numbers.
  129. This can be a single file, or a colon-separated list of files.
  130. .TP 8
  131. .B \-n
  132. Force stdout to be flushed after check a file. This is only useful if
  133. checking a list of files. It is intended to be used by programs want
  134. filetype output from a pipe.
  135. .TP 8
  136. .B \-v
  137. Print the version of the program and exit.
  138. .TP 8
  139. .B \-z
  140. Try to look inside compressed files.
  141. .TP 8
  142. .B \-L
  143. option causes symlinks to be followed, as the like-named option in
  144. .BR ls (1).
  145. (on systems that support symbolic links).
  146. .TP 8
  147. .B \-s
  148. Normally,
  149. .B file
  150. only attempts to read and determine the type of argument files which
  151. .BR stat (2)
  152. reports are ordinary files.
  153. This prevents problems, because reading special files may have peculiar
  154. consequences.
  155. Specifying the
  156. .BR \-s
  157. option causes
  158. .B file
  159. to also read argument files which are block or character special files.
  160. This is useful for determining the filesystem types of the data in raw
  161. disk partitions, which are block special files.
  162. This option also causes
  163. .B file
  164. to disregard the file size as reported by
  165. .BR stat (2)
  166. since on some systems it reports a zero size for raw disk partitions.
  167. .SH FILES
  168. .I __MAGIC__
  169. \- default list of magic numbers
  170. .SH ENVIRONMENT
  171. The environment variable
  172. .B MAGIC
  173. can be used to set the default magic number files.
  174. .SH SEE ALSO
  175. .BR magic (__FSECTION__)
  176. \- description of magic file format.
  177. .br
  178. .BR strings (1), " od" (1), " hexdump(1)"
  179. \- tools for examining non-textfiles.
  180. .SH STANDARDS CONFORMANCE
  181. This program is believed to exceed the System V Interface Definition
  182. of FILE(CMD), as near as one can determine from the vague language
  183. contained therein.
  184. Its behaviour is mostly compatible with the System V program of the same name.
  185. This version knows more magic, however, so it will produce
  186. different (albeit more accurate) output in many cases.
  187. .PP
  188. The one significant difference
  189. between this version and System V
  190. is that this version treats any white space
  191. as a delimiter, so that spaces in pattern strings must be escaped.
  192. For example,
  193. .br
  194. >10 string language impress\ (imPRESS data)
  195. .br
  196. in an existing magic file would have to be changed to
  197. .br
  198. >10 string language\e impress (imPRESS data)
  199. .br
  200. In addition, in this version, if a pattern string contains a backslash,
  201. it must be escaped. For example
  202. .br
  203. 0 string \ebegindata Andrew Toolkit document
  204. .br
  205. in an existing magic file would have to be changed to
  206. .br
  207. 0 string \e\ebegindata Andrew Toolkit document
  208. .br
  209. .PP
  210. SunOS releases 3.2 and later from Sun Microsystems include a
  211. .BR file (1)
  212. command derived from the System V one, but with some extensions.
  213. My version differs from Sun's only in minor ways.
  214. It includes the extension of the `&' operator, used as,
  215. for example,
  216. .br
  217. >16 long&0x7fffffff >0 not stripped
  218. .SH MAGIC DIRECTORY
  219. The magic file entries have been collected from various sources,
  220. mainly USENET, and contributed by various authors.
  221. Christos Zoulas (address below) will collect additional
  222. or corrected magic file entries.
  223. A consolidation of magic file entries
  224. will be distributed periodically.
  225. .PP
  226. The order of entries in the magic file is significant.
  227. Depending on what system you are using, the order that
  228. they are put together may be incorrect.
  229. If your old
  230. .B file
  231. command uses a magic file,
  232. keep the old magic file around for comparison purposes
  233. (rename it to
  234. .IR __MAGIC__.orig ).
  235. .SH EXAMPLES
  236. .nf
  237. $ file file.c file /dev/hda
  238. file.c: C program text
  239. file: ELF 32-bit LSB executable, Intel 80386, version 1,
  240. dynamically linked, not stripped
  241. /dev/hda: block special
  242. $ file -s /dev/hda{,1,2,3,4,5,6,7,8,9,10}
  243. /dev/hda: x86 boot sector
  244. /dev/hda1: Linux/i386 ext2 filesystem
  245. /dev/hda2: x86 boot sector
  246. /dev/hda3: x86 boot sector, extended partition table
  247. /dev/hda4: Linux/i386 ext2 filesystem
  248. /dev/hda5: Linux/i386 swap file
  249. /dev/hda6: Linux/i386 swap file
  250. /dev/hda7: Linux/i386 swap file
  251. /dev/hda8: Linux/i386 swap file
  252. /dev/hda9: empty
  253. /dev/hda10: empty
  254. .fi
  255. .SH HISTORY
  256. There has been a
  257. .B file
  258. command in every \s-1UNIX\s0 since at least Research Version 6
  259. (man page dated January, 1975).
  260. The System V version introduced one significant major change:
  261. the external list of magic number types.
  262. This slowed the program down slightly but made it a lot more flexible.
  263. .PP
  264. This program, based on the System V version,
  265. was written by Ian Darwin without looking at anybody else's source code.
  266. .PP
  267. John Gilmore revised the code extensively, making it better than
  268. the first version.
  269. Geoff Collyer found several inadequacies
  270. and provided some magic file entries.
  271. The program has undergone continued evolution since.
  272. .SH AUTHOR
  273. Written by Ian F. Darwin, UUCP address {utzoo | ihnp4}!darwin!ian,
  274. Internet address ian@sq.com,
  275. postal address: P.O. Box 603, Station F, Toronto, Ontario, CANADA M4Y 2L8.
  276. .PP
  277. Altered by Rob McMahon, cudcv@warwick.ac.uk, 1989, to extend the `&' operator
  278. from simple `x&y != 0' to `x&y op z'.
  279. .PP
  280. Altered by Guy Harris, guy@netapp.com, 1993, to:
  281. .RS
  282. .PP
  283. put the ``old-style'' `&'
  284. operator back the way it was, because 1) Rob McMahon's change broke the
  285. previous style of usage, 2) the SunOS ``new-style'' `&' operator,
  286. which this version of
  287. .B file
  288. supports, also handles `x&y op z', and 3) Rob's change wasn't documented
  289. in any case;
  290. .PP
  291. put in multiple levels of `>';
  292. .PP
  293. put in ``beshort'', ``leshort'', etc. keywords to look at numbers in the
  294. file in a specific byte order, rather than in the native byte order of
  295. the process running
  296. .BR file .
  297. .RE
  298. .PP
  299. Changes by Ian Darwin and various authors including
  300. Christos Zoulas (christos@astron.com), 1990-1999.
  301. .SH LEGAL NOTICE
  302. Copyright (c) Ian F. Darwin, Toronto, Canada,
  303. 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993.
  304. .PP
  305. This software is not subject to and may not be made subject to any
  306. license of the American Telephone and Telegraph Company, Sun
  307. Microsystems Inc., Digital Equipment Inc., Lotus Development Inc., the
  308. Regents of the University of California, The X Consortium or MIT, or
  309. The Free Software Foundation.
  310. .PP
  311. This software is not subject to any export provision of the United States
  312. Department of Commerce, and may be exported to any country or planet.
  313. .PP
  314. Permission is granted to anyone to use this software for any purpose on
  315. any computer system, and to alter it and redistribute it freely, subject
  316. to the following restrictions:
  317. .PP
  318. 1. The author is not responsible for the consequences of use of this
  319. software, no matter how awful, even if they arise from flaws in it.
  320. .PP
  321. 2. The origin of this software must not be misrepresented, either by
  322. explicit claim or by omission. Since few users ever read sources,
  323. credits must appear in the documentation.
  324. .PP
  325. 3. Altered versions must be plainly marked as such, and must not be
  326. misrepresented as being the original software. Since few users
  327. ever read sources, credits must appear in the documentation.
  328. .PP
  329. 4. This notice may not be removed or altered.
  330. .PP
  331. A few support files (\fIgetopt\fP, \fIstrtok\fP)
  332. distributed with this package
  333. are by Henry Spencer and are subject to the same terms as above.
  334. .PP
  335. A few simple support files (\fIstrtol\fP, \fIstrchr\fP)
  336. distributed with this package
  337. are in the public domain; they are so marked.
  338. .PP
  339. The files
  340. .I tar.h
  341. and
  342. .I is_tar.c
  343. were written by John Gilmore from his public-domain
  344. .B tar
  345. program, and are not covered by the above restrictions.
  346. .SH BUGS
  347. There must be a better way to automate the construction of the Magic
  348. file from all the glop in Magdir. What is it?
  349. Better yet, the magic file should be compiled into binary (say,
  350. .BR ndbm (3)
  351. or, better yet, fixed-length
  352. .SM ASCII
  353. strings for use in heterogenous network environments) for faster startup.
  354. Then the program would run as fast as the Version 7 program of the same name,
  355. with the flexibility of the System V version.
  356. .PP
  357. .B File
  358. uses several algorithms that favor speed over accuracy,
  359. thus it can be misled about the contents of
  360. .SM ASCII
  361. files.
  362. .PP
  363. The support for
  364. .SM ASCII
  365. files (primarily for programming languages)
  366. is simplistic, inefficient and requires recompilation to update.
  367. .PP
  368. There should be an ``else'' clause to follow a series of continuation lines.
  369. .PP
  370. The magic file and keywords should have regular expression support.
  371. Their use of
  372. .SM "ASCII TAB"
  373. as a field delimiter is ugly and makes
  374. it hard to edit the files, but is entrenched.
  375. .PP
  376. It might be advisable to allow upper-case letters in keywords
  377. for e.g.,
  378. .BR troff (1)
  379. commands vs man page macros.
  380. Regular expression support would make this easy.
  381. .PP
  382. The program doesn't grok \s-2FORTRAN\s0.
  383. It should be able to figure \s-2FORTRAN\s0 by seeing some keywords which
  384. appear indented at the start of line.
  385. Regular expression support would make this easy.
  386. .PP
  387. The list of keywords in
  388. .I ascmagic
  389. probably belongs in the Magic file.
  390. This could be done by using some keyword like `*' for the offset value.
  391. .PP
  392. Another optimisation would be to sort
  393. the magic file so that we can just run down all the
  394. tests for the first byte, first word, first long, etc, once we
  395. have fetched it. Complain about conflicts in the magic file entries.
  396. Make a rule that the magic entries sort based on file offset rather
  397. than position within the magic file?
  398. .PP
  399. The program should provide a way to give an estimate
  400. of ``how good'' a guess is.
  401. We end up removing guesses (e.g. ``From '' as first 5 chars of file) because
  402. they are not as good as other guesses (e.g. ``Newsgroups:'' versus
  403. "Return-Path:"). Still, if the others don't pan out, it should be
  404. possible to use the first guess.
  405. .PP
  406. This program is slower than some vendors' file commands.
  407. .PP
  408. This manual page, and particularly this section, is too long.
  409. .SH AVAILABILITY
  410. You can obtain the original author's latest version by anonymous FTP
  411. on
  412. .B ftp.astron.com
  413. in the directory
  414. .I /pub/file/file-X.YY.tar.gz