file.man 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. .TH FILE __CSECTION__ "March 2006" "Debian/GNU Linux" "Copyrighted but distributable"
  2. .\" $Id: file.man,v 1.57 2005/08/18 15:18:22 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 recognised 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. .TP 8
  277. .B "\-\-help"
  278. Print a help message and exit.
  279. .SH FILES
  280. .TP
  281. .I __MAGIC__.mgc
  282. Default compiled list of magic numbers
  283. .TP
  284. .I __MAGIC__
  285. Default list of magic numbers
  286. .TP
  287. .I __MAGIC__.mime.mgc
  288. Default compiled list of magic numbers, used to output mime types when
  289. the -i option is specified.
  290. .TP
  291. .I __MAGIC__.mime
  292. Default list of magic numbers, used to output mime types when the -i option
  293. is specified.
  294. .SH ENVIRONMENT
  295. The environment variable
  296. .B MAGIC
  297. can be used to set the default magic number file name.
  298. If that variable is set, then
  299. .B file
  300. will not attempt to open
  301. .BR $HOME/.magic .
  302. .B file
  303. adds ".mime" and/or ".mgc" to the value of this variable as appropriate.
  304. However,
  305. .B file
  306. has to exist in order for
  307. .BR file .mime
  308. to be considered.
  309. The environment variable
  310. .B POSIXLY_CORRECT
  311. controls (on systems that support symbolic links), if
  312. .B file
  313. will attempt to follow symlinks or not. If set, then
  314. .B file
  315. follows symlink, otherwise it does not. This is also controlled
  316. by the
  317. .B L
  318. and
  319. .B h
  320. options.
  321. .SH SEE ALSO
  322. .BR magic (__FSECTION__)
  323. \- description of magic file format.
  324. .br
  325. .BR strings (1), " od" (1), " hexdump(1)"
  326. \- tools for examining non-textfiles.
  327. .SH STANDARDS CONFORMANCE
  328. This program is believed to exceed the System V Interface Definition
  329. of FILE(CMD), as near as one can determine from the vague language
  330. contained therein.
  331. Its behaviour is mostly compatible with the System V program of the same name.
  332. This version knows more magic, however, so it will produce
  333. different (albeit more accurate) output in many cases.
  334. .PP
  335. The one significant difference
  336. between this version and System V
  337. is that this version treats any white space
  338. as a delimiter, so that spaces in pattern strings must be escaped.
  339. For example,
  340. .br
  341. >10 string language impress\ (imPRESS data)
  342. .br
  343. in an existing magic file would have to be changed to
  344. .br
  345. >10 string language\e impress (imPRESS data)
  346. .br
  347. In addition, in this version, if a pattern string contains a backslash,
  348. it must be escaped.
  349. For example
  350. .br
  351. 0 string \ebegindata Andrew Toolkit document
  352. .br
  353. in an existing magic file would have to be changed to
  354. .br
  355. 0 string \e\ebegindata Andrew Toolkit document
  356. .br
  357. .PP
  358. SunOS releases 3.2 and later from Sun Microsystems include a
  359. .BR file (1)
  360. command derived from the System V one, but with some extensions.
  361. My version differs from Sun's only in minor ways.
  362. It includes the extension of the `&' operator, used as,
  363. for example,
  364. .br
  365. >16 long&0x7fffffff >0 not stripped
  366. .SH MAGIC DIRECTORY
  367. The magic file entries have been collected from various sources,
  368. mainly USENET, and contributed by various authors.
  369. Christos Zoulas (address below) will collect additional
  370. or corrected magic file entries.
  371. A consolidation of magic file entries
  372. will be distributed periodically.
  373. .PP
  374. The order of entries in the magic file is significant.
  375. Depending on what system you are using, the order that
  376. they are put together may be incorrect.
  377. .SH EXAMPLES
  378. .nf
  379. $ file file.c file /dev/{wd0a,hda}
  380. file.c: C program text
  381. file: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),
  382. dynamically linked (uses shared libs), stripped
  383. /dev/wd0a: block special (0/0)
  384. /dev/hda: block special (3/0)
  385. $ file -s /dev/wd0{b,d}
  386. /dev/wd0b: data
  387. /dev/wd0d: x86 boot sector
  388. $ file -s /dev/hda{,1,2,3,4,5,6,7,8,9,10}
  389. /dev/hda: x86 boot sector
  390. /dev/hda1: Linux/i386 ext2 filesystem
  391. /dev/hda2: x86 boot sector
  392. /dev/hda3: x86 boot sector, extended partition table
  393. /dev/hda4: Linux/i386 ext2 filesystem
  394. /dev/hda5: Linux/i386 swap file
  395. /dev/hda6: Linux/i386 swap file
  396. /dev/hda7: Linux/i386 swap file
  397. /dev/hda8: Linux/i386 swap file
  398. /dev/hda9: empty
  399. /dev/hda10: empty
  400. $ file -i file.c file /dev/{wd0a,hda}
  401. file.c: text/x-c
  402. file: application/x-executable, dynamically linked (uses shared libs),
  403. not stripped
  404. /dev/hda: application/x-not-regular-file
  405. /dev/wd0a: application/x-not-regular-file
  406. .fi
  407. .SH HISTORY
  408. There has been a
  409. .B file
  410. command in every \s-1UNIX\s0 since at least Research Version 4
  411. (man page dated November, 1973).
  412. The System V version introduced one significant major change:
  413. the external list of magic number types.
  414. This slowed the program down slightly but made it a lot more flexible.
  415. .PP
  416. This program, based on the System V version,
  417. was written by Ian Darwin <ian@darwinsys.com>
  418. without looking at anybody else's source code.
  419. .PP
  420. John Gilmore revised the code extensively, making it better than
  421. the first version.
  422. Geoff Collyer found several inadequacies
  423. and provided some magic file entries.
  424. Contributions by the `&' operator by Rob McMahon, cudcv@warwick.ac.uk, 1989.
  425. .PP
  426. Guy Harris, guy@netapp.com, made many changes from 1993 to the present.
  427. .PP
  428. Primary development and maintenance from 1990 to the present by
  429. Christos Zoulas (christos@astron.com).
  430. .PP
  431. Altered by Chris Lowth, chris@lowth.com, 2000:
  432. Handle the ``-i'' option to output mime type strings and using an alternative
  433. magic file and internal logic.
  434. .PP
  435. Altered by Eric Fischer (enf@pobox.com), July, 2000,
  436. to identify character codes and attempt to identify the languages
  437. of non-ASCII files.
  438. .PP
  439. The list of contributors to the "Magdir" directory (source for the
  440. .I __MAGIC__
  441. file) is too long to include here.
  442. You know who you are; thank you.
  443. .SH LEGAL NOTICE
  444. Copyright (c) Ian F. Darwin, Toronto, Canada, 1986-1999.
  445. Covered by the standard Berkeley Software Distribution copyright; see the file
  446. LEGAL.NOTICE in the source distribution.
  447. .PP
  448. The files
  449. .I tar.h
  450. and
  451. .I is_tar.c
  452. were written by John Gilmore from his public-domain
  453. .B tar
  454. program, and are not covered by the above license.
  455. .SH BUGS
  456. There must be a better way to automate the construction of the Magic
  457. file from all the glop in magdir.
  458. What is it?
  459. Better yet, the magic file should be compiled into binary (say,
  460. .BR ndbm (3)
  461. or, better yet, fixed-length
  462. .SM ASCII
  463. strings for use in heterogenous network environments) for faster startup.
  464. Then the program would run as fast as the Version 7 program of the same name,
  465. with the flexibility of the System V version.
  466. .PP
  467. .B File
  468. uses several algorithms that favor speed over accuracy,
  469. thus it can be misled about the contents of
  470. text
  471. files.
  472. .PP
  473. The support for
  474. text
  475. files (primarily for programming languages)
  476. is simplistic, inefficient and requires recompilation to update.
  477. .PP
  478. There should be an ``else'' clause to follow a series of continuation lines.
  479. .PP
  480. The magic file and keywords should have regular expression support.
  481. Their use of
  482. .SM "ASCII TAB"
  483. as a field delimiter is ugly and makes
  484. it hard to edit the files, but is entrenched.
  485. .PP
  486. It might be advisable to allow upper-case letters in keywords
  487. for e.g.,
  488. .BR troff (1)
  489. commands vs man page macros.
  490. Regular expression support would make this easy.
  491. .PP
  492. The program doesn't grok \s-2FORTRAN\s0.
  493. It should be able to figure \s-2FORTRAN\s0 by seeing some keywords which
  494. appear indented at the start of line.
  495. Regular expression support would make this easy.
  496. .PP
  497. The list of keywords in
  498. .I ascmagic
  499. probably belongs in the Magic file.
  500. This could be done by using some keyword like `*' for the offset value.
  501. .PP
  502. Another optimisation would be to sort
  503. the magic file so that we can just run down all the
  504. tests for the first byte, first word, first long, etc, once we
  505. have fetched it.
  506. Complain about conflicts in the magic file entries.
  507. Make a rule that the magic entries sort based on file offset rather
  508. than position within the magic file?
  509. .PP
  510. The program should provide a way to give an estimate
  511. of ``how good'' a guess is.
  512. We end up removing guesses (e.g. ``From '' as first 5 chars of file) because
  513. they are not as good as other guesses (e.g. ``Newsgroups:'' versus
  514. ``Return-Path:'').
  515. Still, if the others don't pan out, it should be possible to use the
  516. first guess.
  517. .PP
  518. This program is slower than some vendors' file commands.
  519. The new support for multiple character codes makes it even slower.
  520. .PP
  521. This manual page, and particularly this section, is too long.
  522. .SH RETURN CODE
  523. .B file
  524. almost always returns 0. It returns a different if it cannot open a file.
  525. .SH AVAILABILITY
  526. You can obtain the original author's latest version by anonymous FTP
  527. on
  528. .B ftp.astron.com
  529. in the directory
  530. .I /pub/file/file-X.YZ.tar.gz
  531. .PP
  532. This
  533. .B Debian
  534. version adds a number of new magix entries. It can be
  535. obtained from every site carrying a
  536. .B Debian
  537. distribution (ftp.debian.org and mirrors).