14.2 檔案和檔案系統

14.2.1 確定檔案類型:file

指令 file 可查看 /etc/magic 來確定檔案或檔案清單的類型。

tester@linux:~> file /usr/bin/file
/usr/bin/file: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), \
    for GNU/Linux 2.2.5, dynamically linked (uses shared libs), stripped
   

參數 -f list 會使用檔案名稱清單來指定要檢驗的檔案。-z 可讓 file 查看壓縮檔案內部:

tester@linux:~> file /usr/share/man/man1/file.1.gz
usr/share/man/man1/file.1.gz: gzip compressed data, from Unix, max compression
tester@linux:~> file -z /usr/share/man/man1/file.1.gz
/usr/share/man/man1/file.1.gz: ASCII troff or preprocessor input text \
    (gzip compressed data, from Unix, max compression)
   

14.2.2 檔案系統與其使用率:mountdfdu

指令 mount 會顯示哪個檔案系統 (設備和類型) 已裝載於哪個裝載點:

tester@linux:~> mount
/dev/hda3 on / type reiserfs (rw,acl,user_xattr)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
udev on /dev type tmpfs (rw)
devpts on /dev/pts type devpts (rw,mode=0620,gid=5)
/dev/hda1 on /boot type ext2 (rw,acl,user_xattr)
/dev/hda4 on /local type reiserfs (rw,acl,user_xattr)
/dev/fd0 on /media/floppy type subfs (rw,nosuid,nodev,noatime,fs=floppyfss,p
   

使用指令 df 來取得檔案系統總使用量的資訊。參數 -h (或 --human-readable) 可將輸出轉換為一般使用者容易了解的形式。

tester@linux:~> df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/hda3              11G  3.2G  6.9G  32% /
udev                  252M  104K  252M   1% /dev
/dev/hda1              16M  6.6M  7.8M  46% /boot
/dev/hda4              27G   34M   27G   1% /local
   

使用指令 du,可以顯示指定目錄及其子目錄下所有檔案的大小。參數 -s 可以不顯示輸出的詳細資訊。-h 會再次將資料轉換成人類可判讀的格式:

tester@linux:~> du -sh /local
1.7M    /local
   

14.2.3 其它有關 ELF 二進位檔的資訊

使用 readelf 公用程式來讀取二進位檔的內容。這個公用程式甚至可以搭配為其他硬體架構所建的 ELF 檔案使用。

tester@linux:~> readelf --file-header /bin/ls
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Intel 80386
  Version:                           0x1
  Entry point address:               0x8049b60
  Start of program headers:          52 (bytes into file)
  Start of section headers:          81112 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         9
  Size of section headers:           40 (bytes)
  Number of section headers:         30
  Section header string table index: 29
  

14.2.4 檔案內容:stat

指令 stat 會顯示檔案內容:

tester@linux:~> stat /etc/profile
  File: `/etc/profile'
  Size: 7930            Blocks: 16         IO Block: 4096   regular file
Device: 303h/771d       Inode: 40657       Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2006-01-06 16:45:43.000000000 +0100
Modify: 2005-11-21 14:54:35.000000000 +0100
Change: 2005-12-19 09:51:04.000000000 +0100
   

參數 --filesystem 會產生特定檔案的檔案系統詳細內容:

tester@linux:~> stat /etc/profile --filesystem
  File: "/etc/profile"
    ID: 0        Namelen: 255     Type: reiserfs
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 2622526    Free: 1809771    Available: 1809771
Inodes: Total: 0          Free: 0