Linux head和tail命令


介紹

 head和tail是一組想對應的命令,默認分別顯示文件的開頭和末尾10行記錄。

 

head命令

head命令默認顯示頭部的前N行

Usage: head [OPTION]... [FILE]...
默認輸出前10行
當指定多個文件時,在輸出前輸出各個文件名
當沒有指定文件或指定的文件是“-”說明讀取標准輸入

Mandatory arguments to long options are mandatory for short options too.
  -c, --bytes=[-]K         print the first K bytes of each file;
                             with the leading `-', print all but the last
                             K bytes of each file 顯示頭部前多少字節,這里的K是單位的標示,具體的單位參考下面列表,當使用-K時顯示除了尾部多少字節外的所有內容
  -n, --lines=[-]K         print the first K lines instead of the first 10;
                             with the leading `-', print all but the last
                             K lines of each file顯示頭部的多少行,默認顯示頭部10行,例如-n -b代表顯示除了尾部512行外的所有行
  -q, --quiet, --silent    never print headers giving file names當指定多個文件時,出現內容中不輸出文件名
  -v, --verbose            always print headers giving file names
      --help     display this help and exit
      --version  output version information and exit

K may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

tail命令

tail命令同head命名正好相反,tail默認輸出尾部10行

Usage: tail [OPTION]... [FILE]...
默認輸出尾部10行
當指定多個文件時,默認在輸出內容前顯示文件名
當沒有指定文件或者指定的文件是“-”時,讀取標准輸入

Mandatory arguments to long options are mandatory for short options too.對於長選項不可忽略的參數對應短選項也不可忽略
  -c, --bytes=K            output the last K bytes; alternatively, use -c +K顯示尾部的字節數,當使用+k參數時表示從k字節開始顯示到末尾,例如-c +1b 顯示512字節開始到末尾的內容
                           to output bytes starting with the Kth of each file
  -f, --follow[={name|descriptor}]
                           output appended data as the file grows;
                           -f, --follow, and --follow=descriptor are
                           equivalent追加顯示文件最新的內容
  -F                       same as --follow=name --retry等同於使用 -f filename --retry操作
  -n, --lines=K            output the last K lines, instead of the last 10;默認顯示輸出尾部的10行,當使用+k參數時指從第K行開始顯示到末尾,例如-n +6顯示第6行到末尾的內容
                           or use -n +K to output lines starting with the Kth
      --max-unchanged-stats=N
                           with --follow=name, reopen a FILE which has not
                           changed size after N (default 5) iterations
                           to see if it has been unlinked or renamed
                           (this is the usual case of rotated log files).
                           With inotify, this option is rarely useful.
      --pid=PID            with -f, terminate after process ID, PID dies--指定進程id,可以結合-f參數一起使用,當指定的進程終止時顯示也終止
  -q, --quiet, --silent    never output headers giving file names ###同時顯示多個文件時不輸出文件名
      --retry              keep trying to open a file even when it is or當連接的文件突然不可訪問時嘗試重新連接
                             becomes inaccessible; useful when following by
                             name, i.e., with --follow=name
  -s, --sleep-interval=N   with -f, sleep for approximately N seconds當使用-f參數時可以,使用-s顯示間隔
                             (default 1.0) between iterations.
                           With inotify and --pid=P, check process P at
                           least once every N seconds.
  -v, --verbose            always output headers giving file names
      --help     display this help and exit
      --version  output version information and exit

If the first character of K (the number of bytes or lines) is a `+',
print beginning with the Kth item from the start of each file, otherwise,
print the last K items in the file.  K may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

tail命令參數和head概念差不多,但是要注意二者的+k,-k的各自含義,tail有一個非常有意思的參數是-f參數,使用該參數可以不停的追加顯示一個文件最新插入的內容。

1.追加顯示文件內容

tail -f /tmp/orzdba_mysql.log 

當文件有新的內容插入進來時它會不停的顯示新插入的內容

2.當進程8770終止時,tail也結束

tail -f /tmp/orzdba_mysql.log --pid=8770

 

總結

 tail -f參數經常會用來監控日志文件的顯示,結合實際需要可以充分發揮它的作用

 

 

 

 

備注:

    作者:pursuer.chen

    博客:http://www.cnblogs.com/chenmh

本站點所有隨筆都是原創,歡迎大家轉載;但轉載時必須注明文章來源,且在文章開頭明顯處給明鏈接。

《歡迎交流討論》


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM