
linux上tail命令

The Linux tail
command displays data from the end of a file. It can even display updates that are added to a file in real-time. We show you how to use it.
Linux tail
命令從文件末尾顯示數據。 它甚至可以實時顯示添加到文件中的更新。 我們向您展示如何使用它。
系統殺死了尾巴嗎? (Did systemd Kill tail?)
The tail
command shows you data from the end of a file. Usually, new data is added to the end of a file, so the tail
command is a quick and easy way to see the most recent additions to a file. It can also monitor a file and display each new text entry to that file as they occur. This makes?it a great tool to monitor log files.
tail
命令向您顯示文件末尾的數據。 通常,新數據會添加到文件的末尾,因此tail
命令是查看文件最新添加的快速簡便的方法。 它還可以監視文件,并在文件出現時顯示該文件的每個新文本條目。 這使其成為監視日志文件的好工具。
Many modern Linux distributions have adopted the?systemd
system and service manager. This is the first process executed, it has process ID 1, and it is the parent of all other processes. This role used to be handled by the older init
system.
許多現代Linux發行版都采用systemd
系統和服務管理器。 這是第一個執行的進程,其進程ID為1 ,并且是所有其他進程的父級。 該角色曾經由較早的init
系統處理。
Along with this change came a new format for system log files. No longer created in plain text, under systemd
?they are recorded in a binary format. To read these log files, you must use the journactl
utility.?The tail
command works with plain text formats. It does not read binary files. So does this mean the tail
command is a solution in search of a problem? Does it still have anything to offer?
隨之而來的是系統日志文件的新格式。 不再以純文本創建,而是在systemd
下以二進制格式記錄。 要讀取這些日志文件,必須使用journactl
實用程序。 tail
命令適用于純文本格式。 它不讀取二進制文件。 那么,這是否意味著tail
命令是尋找問題的解決方案? 它還有什么可提供的嗎?
There’s more to the tail
command than showing updates in real-time. And for that matter, there are still plenty of log files that are not system generated and are still created as plain text files. For example, log files generated by applications haven’t changed their format.
tail
命令比實時顯示更新要多。 因此,仍然有許多不是系統生成的日志文件,而是仍然創建為純文本文件。 例如,應用程序生成的日志文件沒有更改其格式。
使用尾巴 (Using tail)
Pass the name of a file to tail
and it will show you the last ten lines from that file. The example files we’re using contain lists of sorted words. Each line is numbered, so it should be easy to follow the examples and see what effect the various options have.
將文件名傳遞到tail
,它將顯示該文件的最后十行。 我們正在使用的示例文件包含排序單詞列表。 每行都有編號,因此應該容易遵循示例并了解各種選項的作用。
tail word-list.txt

To see a different number of lines, use the -n
(number of lines) option:
要查看不同的行數,請使用-n
(行數)選項:
tail -n 15 word-list.txt

Actually, you can dispense with the “-n”, and just use a hyphen “-” and the number. Make sure there are no spaces between them. Technically, this is an obsolete command form, but it is still in the man page, and it still works.
實際上,您可以省去“ -n”,而只需使用連字符“-”和數字。 確保它們之間沒有空格。 從技術上講,這是一種過時的命令格式,但仍在手冊頁中,并且仍然有效。
tail -12 word-list.txt

將尾巴與多個文件一起使用 (Using tail With Multiple Files)
You can have tail
work with multiple files at once. Just pass the filenames on the command line:
您可以一次處理多個文件的tail
處理。 只需在命令行中傳遞文件名即可:
tail -n 4 list-1.txt?list-2.txt?list-3.txt

A small header is shown for each file so that you know which file the lines belong to.
每個文件都顯示一個小標題,以便您知道行所屬的文件。
從文件開始顯示行 (Displaying Lines from the Start of a FIle)
The +
(count from the start) modifier makes tail
?display lines from the start of a file, beginning at a specific line number. If your file is very long and you pick a line close to the start of the file, you’re going to get a lot of output sent to the terminal window. If that’s the case, it makes sense to pipe the output from tail
?into less
.
+
(從開始算起)修飾符使tail
顯示從文件開頭開始的行,從特定行號開始。 如果您的文件很長,并且在文件的開頭附近選擇了一行,則將有很多輸出發送到終端窗口。 如果是這種情況,就可以將tail
的輸出傳遞給less
。
tail +440 list-1.txt

You can page through the text in a controlled fashion.
您可以以受控方式翻閱文本。

Because there happen to be 20,445 lines in this file, this command is the equivalent of using the “-6” option:
因為此文件中恰好有20,445行,所以此命令等效于使用“ -6”選項:
tail +20440 list-1.txt

使用帶尾字節 (Using Bytes With tail)
You can tell tail
to use offsets in bytes instead of lines by using the -c
(bytes) option. This could be useful if you have a file of text that was formatted into regular-sized records. Note that a newline character counts as one byte. This command will display the last 93 bytes in the file:
您可以通過-c
(bytes)選項告訴tail
使用字節而不是行的偏移量。 如果您有一個格式為常規大小的記錄的文本文件,這可能會很有用。 請注意,換行符算作一個字節。 此命令將顯示文件中的最后93個字節:
tail -c 93 list-2.txt

You can combine the -c
(bytes) option with the +
(count from the start of the file) modifier, and specify an offset in bytes counted from the start of the file:
可以將-c
(字節)選項與+
(從文件開頭算起)修飾符結合使用,并指定從文件開始算起的字節偏移量:
tail -c +351053 list-e.txt

尾部配管 (Piping Into tail)
Earlier, we piped the output from tail
into less
. We can also pipe the output from other commands into tail
.
之前,我們將tail
的輸出通過管道傳遞到less
。 我們還可以將其他命令的輸出通過管道傳遞給tail
。
To identify the five files or folders with the oldest modification times, use the -t
(sort by modification time) option with ls
, and pipe the output into tail
.
要標識修改時間最久的五個文件或文件夾,請在ls
使用-t
(按修改時間排序)選項,并將輸出通過管道傳遞到tail
。
ls -tl | tail -5

The head
command lists lines of text from the start of a file. We can combine this with tail
to extract a section of the file.? Here, we’re using the head
command to extract the first 200 lines from a file. This is being piped into tail
, which is extracting the last ten lines. This gives us lines 191 through to line 200. That is, the last ten lines of the first 200 lines:
head
命令從文件開頭列出文本行。 我們可以將其與tail
結合使用以提取文件的一部分。 在這里,我們使用head
命令從文件中提取前200行。 這是通過管道傳遞給tail
,它提取了最后十行。 這使我們從191行到200行。也就是說,前200行中的最后十行:
head -n 200 list-1.txt | tail -10

This command lists the five most memory-hungry processes.
此命令列出了五個最需要內存的進程。
ps aux | sort -nk +4 | tail -5

Let’s break that down.
讓我們分解一下。
The ps
command displays information about running processes. The options used are:
ps
命令顯示有關正在運行的進程的信息。 使用的選項是:
a: List all processes, not just for the current user.
a :列出所有進程,而不僅限于當前用戶。
u: Display a user-oriented output.
u :顯示面向用戶的輸出。
x: List all processes, including those not running inside a TTY.
x :列出所有進程,包括不在TTY中運行的進程。
The sort
command sorts the output from ps
. The options we’re using with sort
are:
sort
命令對ps
的輸出進行sort
。 我們在sort
中使用的選項是:
n:? Sort numerically.
n :按數字排序。
k +4: Sort on the fourth column.
k +4 :在第四列上排序。
The tail -5
command displays the last five processes from the sorted output. These are the five most memory-hungry processes.
tail -5
命令顯示排序后的輸出中的最后五個進程。 這是五個最需要內存的過程。
使用尾巴實時跟蹤文件 (Using tail to Track Files in Real-Time)
Tracking new text entries arriving in a file—usually a log file—is easy with tail
. Pass the filename on the command line and use the -f
(follow) option.
使用tail
可以很容易地跟蹤到達文件(通常是日志文件)中的新文本條目。 在命令行上傳遞文件名,然后使用-f
(跟隨)選項。
tail -f geek-1.log

As each new log entry is added to the log file, tail updates its display in the terminal window.
在將每個新日志條目添加到日志文件時,tail會更新其在終端窗口中的顯示。

You can refine the output to include only lines of particular relevance or interest. Here, we’re using grep
to only show lines that include the word “average”:
您可以優化輸出以僅包括具有特定相關性或興趣的行。 在這里,我們使用grep
僅顯示包含單詞“ average”的行:
tail -f geek-1.log | grep average

To follow the changes to two or more files, pass the filenames on the command line:
要對兩個或多個文件進行更改,請在命令行中傳遞文件名:
tail -f -n 5 geek-1.log geek-2.log

Each entry is tagged with a header that shows which file the text came from.
每個條目都標有標題,該標題顯示文本來自哪個文件。

The display is updated each time a new entry arrives in a followed file. To specify the update period, use the -s
(sleep period) option. This tells tail
?to wait a number of seconds, five in this example,? between file checks.
每當新條目進入后續文件時,顯示內容都會更新。 要指定更新時間,請使用-s
(睡眠時間)選項。 這告訴tail
在文件檢查之間等待幾秒鐘,在本示例中為五秒鐘。
tail -f -s 5 geek-1.log

Admittedly, you can’t tell by looking at a screenshot, but the updates to the file are happening once every two seconds. The new file entries are being displayed in the terminal window?once every five seconds.
不可否認,您無法通過查看屏幕截圖來判斷,但是文件更新每兩秒鐘發生一次。 每五秒鐘在終端窗口中顯示一次新文件條目。

When you are following the text additions to more than one file, you can suppress the headers that indicate which log file the text comes from. Use the -q
(quiet) option to do this:
在將文本添加到多個文件中時,可以取消顯示這些文本來自哪個日志文件的標題。 使用-q
(安靜)選項執行以下操作:
tail -f -q geek-1.log geek-2.log

The output from the files is displayed in a seamless blend of text. There is no indication which log file each entry came from.
文件的輸出以無縫的文本混合顯示。 沒有指示每個條目來自哪個日志文件。

尾巴仍然有價值 (tail Still Has Value)
Although access to the system log files is now provided by journalctl
, tail
?still has plenty to offer. This is especially true when it is used in conjunction with other commands, by piping into or out of tail
.
盡管journalctl
現在提供了對系統日志文件的journalctl
,但是tail
仍然可以提供很多功能。 當它與其他命令配合使用時,尤其是通過將管道插入或拖出tail
時,尤其如此。
systemd
might have changed the landscape, but there’s still a place for traditional utilities that conform to the?Unix philosophy of doing one thing and doing it well.
systemd
可能已經改變了現狀,但是傳統實用程序仍然有一個地方,符合Unix哲學的一件事,就是做好。
翻譯自: https://www.howtogeek.com/481766/how-to-use-the-tail-command-on-linux/
linux上tail命令