我使用過的Linux命令之file - 檢測並顯示檔案型別

hai0808發表於2017-11-26

用途說明

file命令是用來檢測並顯示檔案型別(determine file type)。

常用引數

-b  不顯示檔名稱,只顯示檔案型別。在shell指令碼中時有用。

-i     顯示MIME類別。

-L  直接顯示符號連線所指向的檔案的類別。

-f namefile    指定名稱檔案(namefile),該檔案每一行為一個檔名,file命令將按每一行的檔名辨別該檔案的型別

使用示例

示例一 顯示檔案型別

[root@web ~]# file install.log 
install.log: UTF-8 Unicode text
[root@web ~]# file -b install.log      <== 不顯示檔名稱 
UTF-8 Unicode text
[root@web ~]# file -i install.log      <== 顯示MIME類別。 
install.log: text/plain; charset=utf-8
[root@web ~]# file -b -i install.log 
text/plain; charset=utf-8
[root@web ~]#

示例二 顯示符號連結的檔案型別

[root@rhel55 ~]# ls -l /var/mail 
lrwxrwxrwx 1 root root 10 08-13 00:11 /var/mail -> spool/mail

[root@rhel55 ~]# file /var/mail 
/var/mail: symbolic link to `spool/mail'
[root@rhel55 ~]# file -L /var/mail 
/var/mail: directory
[root@rhel55 ~]# file /var/spool/mail 
/var/spool/mail: directory
[root@rhel55 ~]# file -L /var/spool/mail 
/var/spool/mail: directory
[root@rhel55 ~]#

 

示例三

[root@web186 root]# file * 
1.sh:                                      ASCII text
1.txt:                                     empty
20080308xzmf.txt:                          ASCII text, with CRLF line terminators
448FD15874B0DC51.wav:                      RIFF (little-endian) data, WAVE audio, ITU G.711 a-law, mono 8000 Hz
449110BA327B23C6.vox:                      8086 relocatable (Microsoft) 
600d.wav:                                  RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, stereo 44100 Hz
anaconda-ks.cfg:                           ASCII English text
banner.swf:                                data
commons-beanutils.jar:                     Zip archive data, at least v1.0 to extract
cvs_accept.sh:                             Bourne shell script text executable
data:                                      directory
employer0605.txt:                          Non-ISO extended-ASCII text
GetFirstFile.class:                        compiled Java class data, version 49.0
GetFirstFile.java:                         ASCII Java program text
HELPFILE:                                  C++ program text
httptunnel.zip:                            Zip archive data, at least v2.0 to extract
iptables_man.txt:                          ASCII English text, with overstriking
lnx61su_171.tar.gz:                        gzip compressed data, from Unix
megamgr.bin.filepart:                      ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.0.0, statically linked, stripped
MegaPR_Linux_A02.tar.gz:                   gzip compressed data, was "MegaPR_Linux_A02.tar", from Win/32
mess0429.txt:                              ISO-8859 text, with very long lines
smartmontools-5.36.tar.gz:                 gzip compressed data, from Unix, max compression
sms.qunfa:                                 ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), not stripped
sms.vip.cfg:                               ISO-8859 text, with very long lines
top.txt:                                   ASCII text, with CRLF, LF line terminators
voice20061128.rar:                         RAR archive data
vox2amr.sh:                                Bourne-Again shell script text executable
[root@web186 root]#

 

注:使用file命令顯示出來的檔案型別有些情況下不一定太準確,如上面黑體顯示的.vox檔案格式,應該是Dialogic VOX File格式的。

問題思考

1. 怎樣自定義某種檔案型別?

相關文章