歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 每天一個Linux命令(15)tail命令

每天一個Linux命令(15)tail命令

日期:2017/3/3 12:51:15   编辑:Linux技術

tail命令用於輸入文件中的尾部內容。tail命令默認在屏幕上顯示指定文件的末尾10行。 如果給定的文件不止一個,則在顯示的每個文件前面加一個文件名標題。

[b] (1)用法:[/b] 用法: tail [必要參數] [選擇參數] [文件]

如果沒有指定文件或者文件名為“-”,則讀取標准輸入。[b] (2)功能:[/b]

功能: 輸出文件的末尾部分[b] (3)選項參數:[/b]

1) -n <k行數> 顯示文件末尾k行內容 2) -c <k字節數> 顯示文件末尾k個字節數

3) -f                   循環讀取                4) -q                  不顯示處理信息

5) -v                  顯示詳細的處理信息[b] (4)實例:[/b]

1)[root@localhost Documents]# tail -n 5 ./tail_text 查看文件後5行的內容

[root@localhost Documents]# cat tail_text
> 01 the first line!
> 02 the second line!
> 03 the third line!
> 04 the forth line!
> 05 the fifth line!
> 06 the sixth line!
> o7 the seventh line!
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleven line!
> 12 the twelve line!
[root@localhost Documents]# tail -n 5 ./tail_text
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleven line!
> 12 the twelve line!

   等價於tail -5 text_tail 查看後5行的內容

[root@localhost Documents]# tail -5 tail_text
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleven line!
> 12 the twelve line!

2)[root@localhost Documents]# tail -n +5 tail_text 從第5行開始顯示

[root@localhost Documents]# tail -n +5 tail_text
> 05 the fifth line!
> 06 the sixth line!
> o7 the seventh line!
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleven line!
> 12 the twelve line!

3)[root@localhost Documents]# head -n -5 tail_text與[root@localhost Documents]# tail -n -5 tail_text

[root@localhost Documents]# head -n 5 tail_text          //顯示文件前5行的內容
> 01 the first line!
> 02 the second line!
> 03 the third line!
> 04 the forth line!
> 05 the fifth line!
[root@localhost Documents]# head -n -5 tail_text          //除了文件後五行全部顯示
> 01 the first line!
> 02 the second line!
> 03 the third line!
> 04 the forth line!
> 05 the fifth line!
> 06 the sixth line!
> o7 the seventh line!                                          //head命令的-n參數,當後面的整數為正為負是有區別的
[root@localhost Documents]# tail -n 5 tail_text                 //tail命令的-n參數,當後面的整數為正為負是一樣的
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleven line!
> 12 the twelve line!
[root@localhost Documents]# tail -n -5 tail_text               //都是顯示末尾的整數的絕對值行
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleven line!
> 12 the twelve line!

4)[root@localhost Documents]# tail -c 30 tail_text 顯示末尾的字節數

[root@localhost Documents]# tail -c 30 tail_text
n line!
> 12 the twelve line!
[root@localhost Documents]# tail -c -30 tail_text
n line!
> 12 the twelve line!
[root@localhost Documents]# head -c 30 tail_text
> 01 the first line!
> 02 the [root@localhost Documents]# head -c -30 tail_text
> 01 the first line!
> 02 the second line!
> 03 the third line!
> 04 the forth line!
> 05 the fifth line!
> 06 the sixth line!
> o7 the seventh line!
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleve[root@localhost Documents]#

5)[root@localhost Documents]# tail -f tail_text 循環讀取內容輸出到標准輸出

[root@localhost Documents]# tail -f tail_text                             //默認是後10行
> 03 the third line!
> 04 the forth line!
> 05 the fifth line!
> 06 the sixth line!
> o7 the seventh line!
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleven line!
> 12 the twelve line!
^C
[root@localhost Documents]# tail -f -n 12 tail_text                     //也可以自己指定
> 01 the first line!
> 02 the second line!
> 03 the third line!
> 04 the forth line!
> 05 the fifth line!
> 06 the sixth line!
> o7 the seventh line!
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleven line!
> 12 the twelve line!
^Z
[6]+  已停止               tail -f -n 12 tail_text
[root@localhost Documents]# tail -f -n 7 tail_text
> 06 the sixth line!
> o7 the seventh line!
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleven line!
> 12 the twelve line!

當然,也可以把信息輸入到文件中:

[root@localhost Documents]# tail -f tail_text>tempory
^Z
[9]+  已停止               tail -f tail_text > tempory
[root@localhost Documents]# cat tempory
> 03 the third line!
> 04 the forth line!
> 05 the fifth line!
> 06 the sixth line!
> o7 the seventh line!
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleven line!
> 12 the twelve line!

6)[root@localhost Documents]# tail -n +5 tail_text與[root@localhost Documents]# tail -n 5 tail_text

[root@localhost Documents]# tail -n +5 tail_text
> 05 the fifth line!
> 06 the sixth line!
> o7 the seventh line!
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleven line!
> 12 the twelve line!

[root@localhost Documents]# tail -n 5 tail_text

> 08 the eighth line!

> 09 the nineth line!

> 10 the tenth line!

> 11 the eleven line!

> 12 the twelve line!

[root@localhost Documents]# head -n +5 tail_text

> 01 the first line!

> 02 the second line!

> 03 the third line!

> 04 the forth line!

> 05 the fifth line!

[root@localhost Documents]# head -n 5 tail_text

> 01 the first line!

> 02 the second line!

> 03 the third line!

> 04 the forth line!> 05 the fifth line!

7)[root@localhost Documents]# tail -n +10 tail_text |head -n -2

[root@localhost Documents]# tail -n +10 tail_text       //從第10行顯示到尾部
> 10 the tenth line!
> 11 the eleven line!
> 12 the twelve line!
[root@localhost Documents]# head -n -2 tail_text       //除了末尾兩行之外前面的都顯示
> 01 the first line!
> 02 the second line!
> 03 the third line!
> 04 the forth line!
> 05 the fifth line!
> 06 the sixth line!
> o7 the seventh line!
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
[root@localhost Documents]# tail -n +10 tail_text |head -n -2   //綜合起來,用管道命令就是後一個命令處理前面的結果,因此達到只顯示第10行的效果
> 10 the tenth line!
[root@localhost Documents]#

Copyright © Linux教程網 All Rights Reserved