file.man 17 KB

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