file.man 17 KB

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