file.man 13 KB

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