1. 程式人生 > >Linux命令(47)——file命令

Linux命令(47)——file命令

1.命令簡介

file命令用來識別檔案型別,對檔案的檢查分為檔案系統、魔法幻數檢查和語言檢查三個過程,也可用來辨別一些檔案的編碼格式。它是通過檢視檔案的頭部資訊來獲取檔案型別,而不是像Windows通過副檔名來確定檔案型別的。

2.命令格式

file [-bchiklLNnprsvz0] [--apple] [--mime-encoding] [--mime-type] [-e testname] [-F separator] [-f namefile] [-m magicfiles] file ...
file -C [-m magicfiles]
file [--help]

3.選項說明

-b,--brief:簡約模式,不顯示檔名稱;
-C,--compile:生成magic.mgc檔案。配合選項-m使用
-c, --checking-printout:對魔法檔案的解析結果列印輸出
-e, --exclude [testname]:排除對指定型別檔案的檢查,testname可取值有apptype、ascii、encoding、tokens、cdf、compress、elf、soft、tar
-F, --separator [separator]:使用指定分隔符替換輸出檔名後的預設的":"分隔符
-f, --files-from [namefile]:從檔案namefile中讀取待檢測的檔案,每行一個
-i, --mime:輸出mime型別的字串而不是可讀字串,比如輸出"text/plain; charset=us-ascii"
而不是"ASCII text" --mime-type, --mime-encoding:像-i,但是隻列印指定元素; -k, --keep-going:不在首次匹配時停止,繼續檢查 -l, --list:Print information about the strength of each magic pattern -L, --dereference:檢視對應軟連結對應檔案的檔案型別 -m, --magic-file [magicfiles]:指定magic file。magic file指的是那些具有特殊內容的檔案,比如C檔案,它會有#include字樣;tar檔案的前幾個位元組會有特殊的規則。而檢驗magic file規則就是根據這些特殊的格式去判斷一個檔案的型別。而這些規則是儲存在$HOME/.magic.mgc
-N, --no-pad:Don't pad filenames so that they align in the output -n, --no-buffer:強制重新整理標準輸出stdout。這個選項只在檢查多個檔案時有效。在通過管道獲取檔案型別時也可以使用該選項 -p, --preserve-date:保留待檢測檔案的access time,即使file命令不更改待檢測檔案的access time -r, --raw:不將不可列印字元轉換為\ooo的八進位制形式,正常情況下,file會做轉換 -s, --special-files:正常情況下,file命令只支援普通檔案的檢測,就像stat(2)一樣。使用該選項可以讓file命令支援特殊檔案,比如原始磁碟分割槽等 -v, --version:顯示版本資訊 -z, --uncompress:嘗試去解讀壓縮檔案的內容 -0, --print0:在檔名後輸出空字元'\0' --help:顯示幫助資訊

4.常用示例

(1)檢視檔案型別。

[[email protected] ~]# file Changelog 
Changelog: ASCII text

(2)不輸出檔名稱,只顯示檔案格式以及編碼。

[[email protected] ~]# file -b Changelog 
ASCII text

(3)輸出mime型別的字串。

[[email protected] ~]# file -i Changelog 
Changelog: text/plain; charset=us-ascii

(4)檢視軟連結對應檔案的檔案型別。

[[email protected] ~]# ll Changelog*
-rw-r--r-- 1 root root 1598 Nov  6 22:39 Changelog
lrwxrwxrwx 1 root root    9 Nov  6 23:07 Changelog.ln -> Changelog

#產看軟連結本身型別
[[email protected] ~]# file Changelog.ln
Changelog.ln: symbolic link to `Changelog'

#檢視軟連結對應檔案的檔案型別
[[email protected] ~]# file -L Changelog.ln
Changelog.ln: ASCII text

參考文獻

[1]man file
[2]linux shell file與magic file檔案
[3]Linux命令詳解-file