觀看檔案內容


		cat 		 由第一行開始連續 (concatenate) 顯示檔案內容。

tac 由最後一行開始連續顯示檔案內容。
more 一頁一頁的顯示檔案內容。
lessmore 的功能且可以往前翻頁。
head 只看頭幾行。
tail 只看最後幾行。
nl 顯示時亦輸出行號。
od 以二進位的方式顯示檔案內容。

  1. cat:由第一行開始連續 (concatenate) 顯示檔案內容。
    [root@dywHome2 test]# cat [-nETvtA] NAME
    參數說明: 
    -n:   列出行號。 
    -E:   行尾列出符號 $ 。 
    -T:   TAB 以符號 ^I 列出。 
    -v:   列出非列印字元,除 TAB 與 LFD,例如 DOS 檔案的斷行符號。 
    -t:   同 -vT。 
    -A:   同 -vET。 
    #範例: 
    [root@dywHome2 test]# cat foo
    This is a test file for cat.
    OK!     everything is OK.
    [root@dywHome2 test]# cat -n foo
         1  This is a test file for cat.
         2  OK!     everything is OK.
    [root@dywHome2 test]# cat -E foo
    This is a test file for cat.$
    OK!     everything is OK.$
    [root@dywHome2 test]# cat -T foo
    This is a test file for cat.
    OK!^Ieverything is OK.
    [root@dywHome2 test]# cat -nA foo
         1  This is a test file for cat.$
         2  OK!^Ieverything is OK.$
    
  2. tac:由最後一行開始連續顯示檔案內容。
    [root@dywHome2 test]# tac filename 
    #範例: 
    [root@dywHome2 test]# tac foo
    OK!     everything is OK.
    This is a test file for cat.
    
  3. more:一頁一頁的顯示檔案內容。
    [root@dywHome2 test]# more filename 
    #範例: 
    [root@dywHome2 test]# more /etc/inittab
    #
    # inittab       This file describes how the INIT process should set up
    #               the system in a certain run-level.
    #
    # Author:       Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
    #               Modified for RHS Linux by Marc Ewing and Donnie Barnes
    #
    
    # Default runlevel. The runlevels used by Mandriva Linux are:
    #   0 - halt (Do NOT set initdefault to this)
    #   1 - Single user mode
    #   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
    #   3 - Full multiuser mode
    #   4 - unused
    #   5 - X11
    #   6 - reboot (Do NOT set initdefault to this)
    [root@dywHome2 test]# ls -al ~ | more
    
  4. less:有 more 的功能且可以往前翻頁。
    [root @test /root ]# less NAME 
    #範例: 
    [root@dywHome2 test]# less /etc/man.config
    # /word 搜尋字串 word, n 繼續向下搜尋。
    # q 離開。
    
  5. head:只看頭幾行。
    [root @test /root]# head [-n number] [檔名] 
    參數說明: 
    -n :顯示 number 行 
    #範例: 
    [root @test /root]# head ~/.bashrc  %*<==預設情況下,顯示頭十行 *)
    [root @test /root]# head -n 20 ~/.bashrc%*<==顯示頭二十行!*)
    
  6. tail:只看最後幾行。
    [root @test /root ]# tail [-n number] [檔名] 
    參數說明: 
    -n :顯示 number 行 
    #範例: 
    [root @test /root]# tail ~/.bashrc 
    [root @test /root]# tail -n 5 ~/.bashrc %*<==只顯示最後面五行!*)
    [root @test /root]# head –n 20 ~/.bashrc | tail –n 10 %*<==顯示 ~/.bashrc 的第 11 到第 20 行 *)
    
  7. nl:顯示時亦輸出行號。
    [root @test /root ]# nl [檔名] 
    參數說明: 
    #範例: 
    [root @test /root]# nl ~/.bashrc %*<==同 cat -n ~/.bachrc*)
    
  8. od:以二進位的方式顯示檔案內容。
    [root@dywHome2 test]# od NAME 
    #範例: 
    [root@dywHome2 test]# od foo
    0000000 064124 071551 064440 020163 020141 062564 072163 063040
    0000020 066151 020145 067546 020162 060543 027164 047412 020513
    0000040 062411 062566 074562 064164 067151 020147 071551 047440
    0000060 027113 000012
    0000063
    

練習題

  1. 如何以 cat 顯示檔案 foo 內容?
    Sol. cat foo
  2. 如何以 cat 顯示檔案 foo 內容,並顯示行號?
    Sol. cat -n foo
  3. 如何以 cat 顯示檔案 foo 內容,並列出行尾符號?
    Sol. cat -E foo
  4. 如何以 cat 顯示檔案 foo 內容,並列出非列印符號?
    Sol. cat -v foo
  5. cat -vET foo 顯示檔案內容,亦可用那個較簡單的選項執行?
    Sol. cat -A foo
  6. 要將 /etc/manpath.config 整個內容顯示出來,並且要將 <tab> 按鍵也以特殊符號顯示,應該如何下達指令?
    Sol. cat -A /etc/manpath.config
  7. 如何由最後一行開始連續顯示檔案 foo 內容?
    Sol. tac foo
  8. 如何一頁一頁的顯示檔案 foo 內容,但不能前後翻頁?
    Sol. more foo
  9. 如何查看檔案 /var/log/messages 內容,且可前後翻頁?
    Sol. less /var/log/messages
  10. 如何一頁一頁的顯示檔案 foo 內容,且可前後翻頁?
    Sol. less foo
  11. 使用 less 查看檔案 foo 內容時,如何退出查詢?
    Sol. 鍵入 q
  12. 使用 less 查看檔案 foo 內容時,如何查詢字串 word?
    Sol. 鍵入 /word 或 ?word
  13. 如何只顯示檔案 foo 的前 20 行內容?
    Sol. head -n 20 foo
  14. head 指令預設為顯示檔案內容的那部分?
    Sol. 前 10 行
  15. 如何只查看檔案 /var/log/messages 的前面 15 行?
    Sol. head -n 15 /var/log/messages
  16. 如何只查看檔案 /var/log/messages 的後面 15 行?
    Sol. tail -n 15 /var/log/messages
  17. nl 指令的功能為何?
    Sol. 顯示檔案內容並輸出行號。
  18. od 指令的功能為何?
    Sol. 以二進位的方式顯示檔案內容。

  DYWANG_HOME