Linux系統中file命令的使用詳解

來源:互聯網
上載者:User

   命令簡介:

  該命令用來識別檔案類型,也可用來辨別一些檔案的編碼格式。它是通過查看檔案的頭部資訊來擷取檔案類型,而不是像Windows通過副檔名來確定檔案類型的。

  執行許可權 :All User

  指令所在路徑:/usr/bin/file

  命令文法:

  代碼如下:

  file [ -bchikLnNprsvz ] [ -f namefile ] [ -F separator ] [ -m magicfiles ] file ...

  命令參數:

  下表列出了部分常用的參數。

  使用樣本:

  代碼如下:

  [root@DB-Server ~]# file --help

  Usage: file [OPTION]... [FILE]...

  Determine file type of FILEs.

  -m, --magic-file LIST use LIST as a colon-separated list of magic number files

  -z, --uncompress try to look inside compressed files

  -b, --brief do not prepend filenames to output lines

  -c, --checking-printout print the parsed form of the magic file, use in conjunction with -m to debug a new magic file before installing it

  -f, --files-from FILE read the filenames to be examined from FILE

  -F, --separator string use string as separator instead of `:'

  -i, --mime output mime type strings

  -k, --keep-going don't stop at the first match

  -L, --dereference causes symlinks to be followed -n, --no-buffer do not buffer output

  -N, --no-pad do not pad output

  -p, --preserve-date preserve access times on files

  -r, --raw don't translate unprintable chars to ooo

  -s, --special-files treat special (block/char devices) files as ordinary ones

  --help display this help and exit

  --version output version information and exit

  當然你也可以使用 man file 擷取更加詳細的協助文檔資訊。

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

  通過下面兩個命令對時,就可以清晰的瞭解參數-b的作用。

  代碼如下:

  [root@DB-Server ~]# file Temp.txt Temp.txt: UTF-8 Unicode text, with very long lines, with CRLF line terminators[root@DB-Server ~]# file -b Temp.txtUTF-8 Unicode text, with very long lines, with CRLF line terminators

  3: 輸出mime類型的字串

  代碼如下:

  [root@DB-Server ~]# file -i Temp.txt Temp.txt: text/plain; charset=utf-8

  4: 查看檔案中的檔案名稱的檔案類型

  這個參數非常適合shell指令碼去尋找、判別某種檔案類型的資料。

  你可以像下面這樣使用 file 命令確定檔案的類型。下面的截圖顯示了用 file 命令確定不同檔案類型的例子。

  代碼如下:

  tecmint@tecmint ~/Linux-Tricks $ dir

  BACKUP master.zip

  crossroads-stable.tar.gz num.txt

  EDWARD-MAYA-2011-2012-NEW-REMIX.mp3 reggea.xspf

  Linux-Security-Optimization-Book.gif tmp-link

  tecmint@tecmint ~/Linux-Tricks $ file BACKUP/

  BACKUP/: directory

  tecmint@tecmint ~/Linux-Tricks $ file master.zip

  master.zip: Zip archive data, at least v1.0 to extract

  tecmint@tecmint ~/Linux-Tricks $ file crossroads-stable.tar.gz

  crossroads-stable.tar.gz: gzip compressed data, from Unix, last modified: Tue Apr 5 15:15:20 2011

  tecmint@tecmint ~/Linux-Tricks $ file Linux-Security-Optimization-Book.gif

  Linux-Security-Optimization-Book.gif: GIF image data, version 89a, 200 x 259

  tecmint@tecmint ~/Linux-Tricks $ file EDWARD-MAYA-2011-2012-NEW-REMIX.mp3

  EDWARD-MAYA-2011-2012-NEW-REMIX.mp3: Audio file with ID3 version 2.3.0, contains: MPEG ADTS, layer III, v1, 192 kbps, 44.1 kHz, JntStereo

  tecmint@tecmint ~/Linux-Tricks $ file /dev/sda1

  /dev/sda1: block special

  tecmint@tecmint ~/Linux-Tricks $ file /dev/tty1

  /dev/tty1: character special

  5:嘗試去解讀壓縮檔的內容

  代碼如下:

  [root@DB-Server ~]# file -z Temp.txt.gz Temp.txt.gz: UTF-8 Unicode text, with very long lines, with CRLF line terminators (gzip compressed data, was "Temp.txt", from Unix, last modified: Tue Jun 24 00:34:15 2014)[root@DB-Server ~]#

  6: 查看軟連結對應檔案的檔案類型

  如下所示,建立一個軟連結sfile,然後分別用file 和帶參數的file -L查看

  代碼如下:

  [root@DB-Server ~]# ln -s Temp.txt.gz sfile[root@DB-Server ~]# file sfile sfile: symbolic link to `Temp.txt.gz'[root@DB-Server ~]# file -L sfile sfile: gzip compressed data, was "Temp.txt", from Unix, last modified: Tue Jun 24 00:34:15 2014[root@DB-Server ~]#

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.