Linux command – file & test

file

因為 Linux 不依據檔尾的名稱來判斷是什麼檔案,所以我們可以使用 #file 這個程式來判斷檔案的格式.而這個程式是依據 /usr/share/magic 所定義的檔頭來做分辨.

[root@benjr ~]# file /etc/passwd
/etc/passwd: ASCII text
[root@benjr ~]# file /bin/ls
/bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), stripped

下次遇到不確定的檔案可先用 #file 來確認.

test

test 一樣可以檢查檔案,方式很多種,先以下面的範例來做說明.

  • -e FILE
    FILE exists

當執行指令時都會回傳一個執行後的代碼給變數 $?,成功的執行完指令後會回傳一個 0 值(有錯誤時會回傳 錯誤代碼).
利用 test 的指令來看看 變數 $? 的變化.

root@ubuntu:~# test -e /etc/passwd
root@ubuntu:~# echo $?
0
root@ubuntu:~# test -e /etc/passwd11
root@ubuntu:~# echo $?
1

可以很明顯看到當 test 成功後回傳得值為 0 ,失敗則回傳 1 .

通常變數 $? 常會與 && || 一起使用,我們現來看一下 && || 的用法

  • cmd1 && cmd2 : 若 cmd1 執行正確且無錯誤 ($?=0),則執行 cmd2 ,反之若 cmd1 執行完畢且為錯誤 ($?≠0),則 cmd2 不執行.
  • cmd1 || cmd2 : 若 cmd1 執行正確且無錯誤 ($?=0),則 cmd2 不執行 ,反之若 cmd1 執行完畢且為錯誤 ($?≠0),則開始執行 cmd2.

下面的範例的語法是常用的架構 (這 && 與 || 的順序就是固定),用以判斷 command1 的執行是成功還是失敗,成功則執行 command2 失敗則執行 command 3

command1 && command2 || command3

使用上面常用的 && || 的判斷式的架構範例.

root@ubuntu:~# test -e /etc/passwd && echo "File exist" || echo "File not exist"
File exist
root@ubuntu:~# test -e /etc/passwd11 && echo "File exist" || echo "File not exist"
File not exist

其他可以用參數.

  • -b FILE
    FILE exists and is block special (檔案存在且為 block device ,通常為儲存裝置)

    [root@localhost ~]# test -b /dev/sda && echo "Block device" || echo "Non-Block device"
    Block device
    [root@localhost ~]# test -b /dev/ttyS0 && echo "Block device" || echo "Non-Block device"
    Non-Block device
    
  • -c FILE
    FILE exists and is character special (檔案存在且為 character device,通常為字元輸出/入裝置)

    [root@localhost ~]# test -c /dev/sda && echo "Character device" || echo "Non-Character device"
    Non-Character device
    [root@localhost ~]# test -c /dev/ttyS0 && echo "Character device" || echo "Non-Character device" 
    Character device
    
  • -d FILE
    FILE exists and is a directory (目錄存在)

    [root@localhost ~]# test -d /root && echo "Directory" || echo "Non-Directory"
    Directory 
    [root@localhost ~]# test -d /root/.bash_profile && echo "Directory" || echo "Non-Directory"
    Non-Directory
    
  • -f FILE
    FILE exists and is a regular file (檔案存在)

    [root@localhost ~]# test -f /root/.bash_profile && echo "File" || echo "Non-File"
    File
    [root@localhost ~]# test -f /root/ && echo "File" || echo "Non-File"
    Non-File
    
  • -g FILE
    FILE exists and is set-group-ID ,特殊權限使用第四個數字來表示,SGID (Set group ID : 2) 檔案的執行,會自動轉換成 Group 的身份來執行.

    [root@localhost ~]# touch test
    [root@localhost ~]# test -g /root/test && echo "Set Group ID" || echo "Non-Set Group ID"
    Non-Set Group ID
    [root@localhost ~]# chmod 2644 test
    [root@localhost ~]# ll test
    -rw-r-Sr-- 1 root root 0  2月  1 19:14 test
    [root@localhost ~]# test -g /root/test && echo "Set Group ID" || echo "Non-Set Group ID"
    Set Group ID
    
  • -G FILE
    FILE exists and is owned by the effective group ID
  • -h FILE
    FILE exists and is a symbolic link (same as -L),檔案存在且為 soft link.

    [root@localhost ~]# touch test
    [root@localhost ~]# test -h /root/test && echo "Link" || echo "Non-Link"
    Non-Link
    [root@localhost ~]# ln -s test test.softlink
    [root@localhost ~]# test -h /root/test.softlink && echo "Link" || echo "Non-Link"
    Link
    
  • -k FILE
    FILE exists and has its sticky bit set ,特殊權限使用第四個數字來表示,(sTicky :1)只有擁有者有權限去刪除,搬移自己建立的檔案.

    [root@localhost ~]# touch test
    [root@localhost ~]# test -k /root/test && echo "Sticky" || echo "Non-Sticky"
    Non-Sticky
    [root@localhost ~]# chmod 1644 test
    [root@localhost ~]# ll test
    -rw-r--r-T 1 root root 0  2月  1 19:14 test
    [root@localhost ~]# test -k /root/test && echo "Sticky" || echo "Non-Sticky"
    Sticky
    
  • -L FILE
    FILE exists and is a symbolic link (same as -h),檔案存在且為 soft link.

    [root@localhost ~]# touch test
    [root@localhost ~]# test -h /root/test && echo "Link" || echo "Non-Link"
    Non-Link
    [root@localhost ~]# ln -s test test.softlink
    [root@localhost ~]# test -h /root/test.softlink && echo "Link" || echo "Non-Link"
    Link
    
  • -O FILE
    FILE exists and is owned by the effective user ID.
  • -p FILE
    FILE exists and is a named pipe.
  • -r FILE
    FILE exists and read permission is granted,檔案存在且有讀取權限.

    [root@localhost ~]# touch test
    [root@localhost ~]# test -r /root/test && echo "Read" || echo "Deny"
    Read
    [root@localhost ~]# su ben -
    [ben@localhost root]$ test -r /root/test && echo "Read" || echo "Deny"
    Deny
    [ben@localhost root]$ exit
    exit
    
  • -s FILE
    FILE exists and has a size greater than zero,檔案存在且大小不為 0.

    [root@localhost ~]# touch test
    [root@localhost ~]# test -s /root/test && echo "Non-zero" || echo "zero"
    zero
    [root@localhost ~]# echo "test" > test
    [root@localhost ~]# test -s /root/test && echo "Non-zero" || echo "zero"
    Non-zero
    
  • -S FILE
    FILE exists and is a socket.
  • -t FD
    File descriptor (FD) is opened on a terminal.
  • -u FILE
    FILE exists and its set-user-ID bit is set ,特殊權限使用第四個數字來表示, SUID (Set user ID :4 ) 檔案的執行,會自動轉換成檔案所有人的身分執行.

    [root@localhost ~]# touch test
    [root@localhost ~]# test -u /root/test && echo "Set User ID" || echo "Non-Set User ID"
    Non-Set User ID
    [root@localhost ~]# chmod 4644 test
    [root@localhost ~]# ll test
    -rwSr--r-- 1 root root 0  2月  1 19:11 test
    [root@localhost ~]# test -u /root/test && echo "Set User ID" || echo "Non-Set User ID"
    Set User ID
    
  • -w FILE
    FILE exists and write permission is granted,檔案存在且有寫入權限.

    [root@localhost ~]# touch test
    [root@localhost ~]# test -w /root/test && echo "Write" || echo "Deny"
    Write
    [root@localhost ~]# su ben -
    [ben@localhost root]$ test -w /root/test && echo "Write" || echo "Deny"
    Deny
    [ben@localhost root]$ exit
    exit
    
  • -x FILE
    FILE exists and execute (or search) permission is granted,檔案存在且有執行權限.

    [root@localhost ~]# touch test
    [root@localhost ~]# test -x /root/test && echo "Execute" || echo "Deny"
    Deny
    [root@localhost ~]# chmod u+x test
    [root@localhost ~]# test -x /root/test && echo "Execute" || echo "Deny"
    Execute
    

除了比較一個檔案外,test 還可以比較兩個檔案.

  • FILE1 -ef FILE2
    FILE1 and FILE2 have the same device and inode numbers
  • FILE1 -nt FILE2
    FILE1 is newer (modification date) than FILE2
  • FILE1 -ot FILE2
    FILE1 is older than FILE2

下面的例子用來比較兩個檔案的新舊,並觀察變數 $? 的變化.

root@ubuntu:~# test /etc/passwd -nt /etc/group
root@ubuntu:~# echo $?
0
root@ubuntu:~# test /etc/passwd -ot /etc/group
root@ubuntu:~# echo $?
1

字串資料的比較

  • STRING1 == STRING2
    The strings are equal.
  • STRING1 != STRING2
    The strings are not equal.
  • -n STRING
    The length of STRING is nonzero.
  • -z STRING
    The length of STRING is zero.

整數之間的比較

  • INTEGER1 -eq INTEGER2
    INTEGER1 is equal to INTEGER2
  • INTEGER1 -ge INTEGER2
    INTEGER1 is greater than or equal to INTEGER2
  • INTEGER1 -gt INTEGER2
    INTEGER1 is greater than INTEGER2
  • INTEGER1 -le INTEGER2
    INTEGER1 is less than or equal to INTEGER2
  • INTEGER1 -lt INTEGER2
    INTEGER1 is less than INTEGER2
  • INTEGER1 -ne INTEGER2
    INTEGER1 is not equal to INTEGER2
沒有解決問題,試試搜尋本站其他內容

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料