自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

關(guān)于 tail 命令的幾個(gè)實(shí)用例子

系統(tǒng) Linux
前一篇文章我們介紹了 head 命令的使用,今天我們?cè)趤?lái)介紹一下 tail。

顧名思義,tail 命令輸出單個(gè)或多個(gè)文件的最后部分內(nèi)容。默認(rèn)情況下,tail 命令將會(huì)打印文件的最后 10 行內(nèi)容。在實(shí)際應(yīng)用中,我們經(jīng)常用它來(lái)實(shí)時(shí)讀取日志文件。

tail 命令的語(yǔ)法結(jié)構(gòu)如下所示:

tail [options] [files]

作為演示,我們使用如下文件內(nèi)容來(lái)介紹 tail 命令:?

The Mysterious Affair at Styles
The Secret Adversary
The Murder on the Links
The Man in the Brown Suit
The Secret of Chimneys
The Murder of Roger Ackroyd
The Big Four
The Mystery of the Blue Train
The Seven Dials Mystery
The Murder at the Vicarage
Giant's Bread
The Floating Admiral
The Sittaford Mystery
Peril at End House
Lord Edgware Dies
Murder on the Orient Express
Unfinished Portrait
Why Didn't They Ask Evans?
Three Act Tragedy
Death in the Clouds

剛剛我們提到過(guò),tail 命令默認(rèn)會(huì)顯示文件的最后 10 行內(nèi)容,但是當(dāng)文件內(nèi)容總共不足 10 行時(shí),會(huì)顯示整個(gè)文件的內(nèi)容。當(dāng)然大多數(shù)情況下,我們不使用它的默認(rèn)行為,而是根據(jù)實(shí)際情況來(lái)選擇應(yīng)用。接下來(lái)我們根據(jù)例子來(lái)逐個(gè)介紹下。

1,使用 tail 命令打印文件的最后 x 行內(nèi)容

打印文件的最后 x 行內(nèi)容(而不是默認(rèn)的最后 10 行),可參考如下語(yǔ)法結(jié)構(gòu):

tail -n x <filename>

比如,查看文件的最后 5 行內(nèi)容:?

$ tail -n 5 tiap.txt
Murder on the Orient Express
Unfinished Portrait
Why Didn't They Ask Evans?
Three Act Tragedy
Death in the Clouds

小提示:你也可以直接使用 tail -x 而不是 tail -n x 來(lái)顯示最后 x 行內(nèi)容。

2,打印從 x 行開(kāi)始往后的所有內(nèi)容

如果想要打印從 x 行開(kāi)始往后的所有內(nèi)容,可使用 +x 選項(xiàng),如下所示:

tail -n +x <filename>

比如,我們的示例文檔,想要打印從第 7 行開(kāi)始的所有內(nèi)容,如下:

$ tail -n +7 tiap.txt
The Big Four
The Mystery of the Blue Train
The Seven Dials Mystery
The Murder at the Vicarage
Giant's Bread
The Floating Admiral
The Sittaford Mystery
Peril at End House
Lord Edgware Dies
Murder on the Orient Express
Unfinished Portrait
Why Didn't They Ask Evans?
Three Act Tragedy
Death in the Clouds

3,使用 tail 命令同時(shí)查看多個(gè)文件

使用 tail 命令可以同時(shí)查看多個(gè)文件。其語(yǔ)法結(jié)構(gòu)如下所示:

tail -n N <file1> <file2> <file3>

與 head 命令類(lèi)似,查看的各個(gè)文件名會(huì)顯示在輸出中。比如,我們想要查看 tiap.txt 和  sherlock.txt 的最后 3 行,如下所示:

$ tail -n3 sherlock.txt tiap.txt
==> sherlock.txt <==
The Adventure of the Noble Bachelor
The Adventure of the Beryl Coronet
The Adventure of the Copper Beeches
==> tiap.txt <==
Why Didn't They Ask Evans?
Three Act Tragedy
Death in the Clouds

提示,使用 -q 選項(xiàng)可以在輸出中不顯示文件名信息。

4,使用 tail 命令實(shí)時(shí)監(jiān)視文件

假設(shè)我們有一個(gè)文件,其內(nèi)容會(huì)實(shí)時(shí)增加(比如日志文件),tail 命令可以幫助我們實(shí)時(shí)查看新添加到文件中的內(nèi)容。這個(gè)功能可通過(guò)使用 -f 選項(xiàng)來(lái)實(shí)現(xiàn):

tail -f <log-file>

上述命令會(huì)首先顯示文件的最后 10 行內(nèi)容,然后當(dāng)文件有新的內(nèi)容增加時(shí),也會(huì)將新的內(nèi)容實(shí)時(shí)輸出到終端中。這個(gè)功能被廣泛用于讀取日志文件,這也可能是 tail 命令最常見(jiàn)的用法。

提示:如果使用 -F 選項(xiàng)代替 -f 選項(xiàng),tail 命令會(huì)等待輸入文件創(chuàng)建(如果不存在)后,在實(shí)時(shí)顯示文件的內(nèi)容。

5,在管道重定向中使用 tail 命令

tail 命令還可以結(jié)合??管道重定向???來(lái)使用。比如,假如在一個(gè)目錄中有許多文件,我們只想查看最后 3 個(gè)修改過(guò)的文件,那么可以按如下方式:

ls -ltr | tail -n3

上述命令中,ls -lrt 按時(shí)間順序倒敘列出所有文件,然后通過(guò)管道重定向?qū)⑤敵鼋o到 tail 命令,tail 命令解析這個(gè)輸出,只顯示其最后的 3 行,這 3 行內(nèi)容就是我們所需要的最近 3 個(gè)修改的文件列表。

6,在 tail 命令的輸出中顯示行號(hào)

我們?cè)诓榭茨硞€(gè)文檔的時(shí)候,如果文檔中可以顯示行號(hào),那么對(duì)于我們來(lái)說(shuō)是非常友好的,比如我們想要查看當(dāng)前正在閱讀的在多少行,整個(gè)文件有多少行內(nèi)容,等等。

不過(guò),tail 命令并沒(méi)有內(nèi)置的顯示行號(hào)的功能。不過(guò)我們可以通過(guò)其他方式來(lái)實(shí)現(xiàn)這一功能,那就是通過(guò)使用 nl 命令以及管道重定向結(jié)合 tail 命令來(lái)實(shí)現(xiàn)。

nl 命令可以在文件內(nèi)容中顯示行號(hào),將其輸出通過(guò)管道重定向到 tail 命令中,就可以滿足我們的需求。如下所示:

nl <filename> | tail -3
責(zé)任編輯:龐桂玉 來(lái)源: TIAP
相關(guān)推薦

2023-03-23 21:08:59

head命令

2014-03-17 17:27:51

Linux mvLinux 命令

2023-02-02 14:06:00

history命令技巧

2016-09-26 14:40:25

Windows內(nèi)網(wǎng)實(shí)用命令

2020-07-22 13:50:39

shell命令前端

2022-11-02 08:32:46

find 命令Linux

2018-08-03 11:07:52

dd命令備份Linux系統(tǒng)

2022-11-09 19:02:10

Linux

2020-02-17 11:54:18

網(wǎng)絡(luò)路由器命令

2009-10-13 14:33:00

2023-05-04 12:39:27

GDB命令程序

2017-04-10 18:45:47

2015-07-14 10:13:29

2018-02-25 10:45:08

Linux命令uptime

2018-02-24 14:00:42

TensorFlow數(shù)學(xué)計(jì)算機(jī)器學(xué)習(xí)

2010-07-14 16:09:52

Telnet命令例子

2020-04-14 09:59:46

新基建數(shù)據(jù)中心5G網(wǎng)絡(luò)

2023-04-20 13:59:01

Pythonwhile循環(huán)的

2024-07-29 14:56:56

2022-05-07 09:30:08

watchtailLinux 系統(tǒng)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)