file - 描述檔案型態

(a) (b)
★[cclinux1]★/u/cp/89/8917026 > whereis file file: /usr/bin/file /usr/share/man/man1/file.1.gz /usr/share/man/mann/file.n.gz

★[cclinux1]★/u/cp/89/8917026 > ldd /usr/bin/file

libc.so.6 => /lib/libc.so.6 (0x40021000) /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)


(c)(d)(e)(f)
概要:
file [ -bcinsvzL ] [ -f namefile ] [ -m magicfiles ] file ...

描述:

file會試著將這個檔案分類。
//苦力翻譯小妹註:用的是很神奇的方法....有點看沒有 @@~ //不過這實在是一個很有趣的程式 ^_^ 依序用三個方法:分別是 filesystem test, magic number test[註4], language test。任何一種測試通過,那麼這個檔案的型態就可以確定並顯示出來。

其中file的分類可能是
text(代表這是一個純文字檔,裡面只包含ascii碼的文字,並且可以print在螢幕上) executable(執行檔,為UNIX或其他系統編譯出來的程式) data(其他檔案,通常是二進位檔或者不能print在螢幕上的檔案)

參數:

不加參數
★[cclinux1]★/u/cp/89/8917026/perl > file a.tar ./ /dev/hda

a.tar: GNU tar archive
./: directory
/dev/hda: block special (3/0)

-b
簡易模式,不會顯示file name
★[cclinux1]★/u/cp/89/8917026 > file -b 1.txt data

-f namefile
讀進來一個列出檔名的檔案(namefile),然後每行一個,列出這些檔案的型態。 ★[cclinux1]★/u/cp/89/8917026/java > /bin/ls > 1.txt

★[cclinux1]★/u/cp/89/8917026/java > file -f 1.txt
1.txt: ASCII text
Copy.class: compiled Java class data, version 45.3
Copy.java: Java program text
Cull.java: International language text
test.txt: ASCII text
當然也可以讓標準輸入(standard input, 一般而言是鍵盤輸入)來當做輸入
例如,
★[cclinux1]★/u/cp/89/8917026/java > file -f -
test.txt #這行是我輸入的
test.txt: ASCII text
Copy.java #這行也是我輸入的
Copy.java: Java program text


-i
輸出為mime type[註1]的檔案模式格式 比如說,可能用"text/plain, ASCII"來取代"ASCII text" 又比如說
#這是平常的file執行結果
★[cclinux1]★/u/cp/89/8917026/perl > file test.pl test.pl: perl commands text
#加上-i之後的執行結果
★[cclinux1]★/u/cp/89/8917026/perl > file -i test.pl test.pl: application/x-perl
當然,要讓file -i 可以運作,檔案的模式需要是可以認得的結構。[註3]

-m magicfiles

詳細的列出    
-m list Specify an alternate list of files containing
magic numbers. This can be a single
colon-separated list of files.
file, or a

-n
讓執行完file指令之後清空輸出緩衝區。可能在pipe的時候才比較有用。

-v
輸出file的版本及資訊。
★[cclinux1]★/u/cp/89/8917026/perl > file -v file-3.30
magic file from /usr/share/magic

-z
針對壓縮檔而言,試著file裡面的檔案 //苦力翻譯小妹註:不過我在cclinux上面試不出來? ★[cclinux1]★/u/cp/89/8917026/perl > tar -tf a.tar a.jpg
index.html
test.pl
★[cclinux1]★/u/cp/89/8917026/perl > file -z a.tar a.tar: GNU tar archive

-L

讓symlinks也可以運作
-L option causes symlinks to be folllowed, as the
like-named option in ls(1). (on systems that sup?
port symbolic links).


-s 通常file只會試著去讀取並決定stat指令[註2] 的回報。這樣可以預防在讀取檔案的時候發生問題。而� [上-s參數之後,file會讀取檔案的位置及其他資訊。這個在決定檔案系統的時候很有用。這個參數也 會讓file命令忽略stat所描述的檔案大小,因為有些系統會在raw disk partition[註5] 之下描述此一檔案之大小為0。 範例:
$ file -s /dev/hda{,1,2,3,4,5,6,7,8,9,10} /dev/hda: x86 boot sector
/dev/hda1: Linux/i386 ext2 filesystem /dev/hda2: x86 boot sector
/dev/hda3: x86 boot sector, extended partition table /dev/hda4: Linux/i386 ext2 filesystem /dev/hda5: Linux/i386 swap file /dev/hda6: Linux/i386 swap file /dev/hda7: Linux/i386 swap file /dev/hda8: Linux/i386 swap file /dev/hda9: empty
/dev/hda10: empty

[註1] mime
MIME (Multipurpose Internet Mail Extersions), 直譯成 "多功能網際網路郵件延展",為在電腦網路上, 直接傳送各種非文字內容檔案的網際網路標準。 MIME 標準將資料型態分為主資料型態 (primary data type) 及次資料型態 (sub-type)。 主資料型態有 "text" 表示文字資料
"image" 表示圖形資料
"video" 表示動畫資料
"audio" 表示聲音檔資料
"application" 表示執行檔或二進位檔
"multipart" 是用以標明內含有多個不同的資料型態 "message" 則是指一個檔案中含有另一個檔案。 等等。
次資料型態則是更進一步描述資料內容型態,讓接收電腦能夠根據這樣的訊息適當的處理、顯示資料。

[註2]
如,
★[cclinux1]★/u/cp/89/8917026/perl > stat a.tar

File: "a.tar"    
Size: 10240   Blocks: 20 Regular File
Access: (0644/-rw-r--r--) Uid: (13773/ hsinyi) Gid: ( 400/ cp)
Device: 7   Inode: 8753779 Links: 1
Access: Sun Sep 23 11:59:28 2001
Modify: Sun Sep 23 11:58:50 2001
Change: Sun Sep 23 11:58:50 2001
描述一個檔案的資訊  

[註3]
定義在 /usr/share/magic.mime

[註4]
定義在 /usr/share/magic

[註5]
從起始磁區開始寫入(而非將檔案寫入檔案系統)的分割區