file.man 14 KB

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