linux上tail命令_如何在Linux上使用tail命令

linux上tail命令

linux上tail命令

A terminal window showing a Bash prompt on an Ubuntu-style Linux laptop.
Fatmawati Achmad Zaenuri/ShutterstockFatmawati Achmad Zaenuri / Shutterstock

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
tail word-list.txt?in a terminal window

To see a different number of lines, use the -n (number of lines) option:

要查看不同的行數,請使用-n (行數)選項:

tail -n 15 word-list.txt
tail -n 15 word-list.txt in a terminal window

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
tail -12 word-list.txt in a terminal window

將尾巴與多個文件一起使用 (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
tail -n 4 list-1.txt?list-2.txt?list-3.txt in a terminal window

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
tail +44 list-1.txt in a terminal window

You can page through the text in a controlled fashion.

您可以以受控方式翻閱文本。

Output from tail displayed in less in a terminal window

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
tail +20440 list-1.txt in a terminal window

使用帶尾字節 (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
tail -c 93 list-2.txt in a terminal window

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
tail -c +351053 list-e.txt in a terminal window

尾部配管 (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
ls -lt | tail -5 in a terminal window

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
head -n 200 list-1.txt | tail -10 in a terminal window

This command lists the five most memory-hungry processes.

此命令列出了五個最需要內存的進程。

ps aux | sort -nk +4 | tail -5
ps aux | sort -nk +4 | tail -5 in a terminal window

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
tail -f geek-1.log in a terminal window

As each new log entry is added to the log file, tail updates its display in the terminal window.

在將每個新日志條目添加到日志文件時,tail會更新其在終端窗口中的顯示。

Output from tail -f geek-1.log in a terminal window

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
tail -f geek-1.log | grep average in a terminal window

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
tail -f -n 5 geek-1.log geek-2.log in a terminal window

Each entry is tagged with a header that shows which file the text came from.

每個條目都標有標題,該標題顯示文本來自哪個文件。

Output from tail -f -n 5 geek-1.log geek-2.log

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
tail -f -s 5 geek-1.log in a terminal window

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.

不可否認,您無法通過查看屏幕截圖來判斷,但是文件更新每兩秒鐘發生一次。 每五秒鐘在終端窗口中顯示一次新文件條目。

Output from tail -f -s 5 geek-1.log

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
tail -f -q geek-1.log geek-2.log in a terminal window

The output from the files is displayed in a seamless blend of text. There is no indication which log file each entry came from.

文件的輸出以無縫的文本混合顯示。 沒有指示每個條目來自哪個日志文件。

Output from tail -f -q geek-1.log geek-2.log in a terminal window

尾巴仍然有價值 (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命令

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278925.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278925.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278925.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

初學者萬年歷c語言源代碼,C語言萬年歷的源程序

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓for(j1;j<mon[i];j){cprintf("%3d ",j);/*if((firstj-1)%70)putchar(\n);*/}/*first(firstmon[i])%7;if(first0)first7;*/}}void month5_8(){for(i0;i<2;i){window(2i*w,3,29w*i,11);textbackground(5);clrscr();t…

用imageMagick的composite合并圖片

composite命令可以非常方便的合并兩張圖片 因此用來進行圖像加水印、批量增加邊框等常用的變換 最簡單的用法為&#xff1a; composite -gravity north src.jpg coverback.jpg des.jpg 其中src.jpg為前景圖片 coverback.jpg為背景圖片。 des.jpg為疊加后的結果 -gravity north …

白帽子講web安全——認證與會話管理

在看白帽子講web安全&#xff0c;剛好看到認證與會話管理&#xff1a;也就是我們在平常滲透測試中遇到最多的登錄頁面&#xff0c;也即是用戶名和密碼認證方式&#xff0c;這是最常見的認證方式。 了解兩個概念&#xff1a;認證和授權 1&#xff09;&#xff1a;認證的目的是為…

iphone充電圖_哪些iPhone具有無線充電功能?

iphone充電圖Kevin Parrish凱文帕里什Wireless charging means you can re-energize your phone’s battery without a physical tether. It also prevents possible damage to your phone’s charging port. Unfortunately, not all phones support wireless charging, but we…

關聯分析算法c語言實現,機器學習關聯分析

AI開發平臺ModelArtsModelArts是面向開發者的一站式AI開發平臺&#xff0c;為機器學習與深度學習提供海量數據預處理及半自動化標注、大規模分布式Training、自動化模型生成&#xff0c;及端-邊-云模型按需部署能力&#xff0c;幫助用戶快速創建和部署模型&#xff0c;管理全周…

windows平臺下基于QT和OpenCV搭建圖像處理平臺

在之前的博客中&#xff0c;已經分別比較詳細地闡述了“windows平臺下基于VS和OpenCV”以及“Linux平臺下基于QT和OpenCV"搭建圖像處理框架&#xff0c;并且生成了相應的免費視頻。這篇博客的主要內容&#xff0c;就是基于最新版本的相應工具&#xff0c;在windows平臺下&…

android死鎖解決方案,【線程死鎖】Android多線程死鎖產生的原因以及如何避免

一、死鎖定義1、生活中的列子兩人吃飯&#xff0c;但只有一雙筷子&#xff0c;2人輪流吃(同時擁有2只筷子才能吃)&#xff0c;某個時候一人拿了左筷子&#xff0c;一人拿了右筷子&#xff0c;兩人同時占用一個資源&#xff0c;等待另一個資源&#xff0c;這時候甲等乙吃完并釋放…

前端開發 常用用的靜態服務器

1 運用anywhere 安裝 &#xff1a;npm install anywhere -g想要以某個路徑作為靜態文件服務器的根目錄分享&#xff0c;只需要在該目錄下執行&#xff1a;anywhere 就會默認8000打開網頁&#xff0c; 若文件不是index.html 需要輸入文件名 A: anywhere -p 8000 ## 指定靜態服務…

前端面試題整理

1.HTML5的新特性。 主要講講新增哪些API:地理定位&#xff0c;拖放&#xff0c;web存儲應用緩存&#xff0c;webworkers&#xff0c; sse 。 http://www.w3school.com.cn/html5/html_5_intro.asp 2.CSS3的新特性。 https://segmentfault.com/a/1190000010780991 3、使用嚴格模式…

android mvvm 官方例子,詳解Android的MVVM框架 - 數據綁定

&#xfeff;本教程是跟著 Data Binding Guide學習過程中得出的一些實踐經驗&#xff0c;同時修改了官方教程的一些錯誤&#xff0c;每一個知識點都有對應的源碼&#xff0c;爭取做到實踐與理論相結合。Data Binding 解決了 Android UI 編程中的一個痛點&#xff0c;官方原生支…

VS2015 代碼左縮進

TabShift轉載于:https://www.cnblogs.com/527289276qq/p/8027882.html

mac設置文件權限_如何在Mac上設置文件權限

mac設置文件權限Like all major operating systems, macOS allows you to restrict access to files using a complex set of file permissions. You can set these yourself using the Finder app, or by using the chmod command in your Mac’s terminal. Here’s how. 與所…

Discrete Log Algorithms :Baby-step giant-step

離散對數的求解 1.暴力 2.Baby-step giant-step 3.Pollard’s ρ algorithm …… 下面搬運一下Baby-step giant-step 的做法 這是在 https://ctf-wiki.github.io/ctf-wiki/crypto/asymmetric/discrete-log/discrete-log/ 上看到的&#xff0c;比較容易理解。 而且&#xff0c;…

Android添加item動畫,RecyclerView基礎篇-Item添加動畫

Android_Banner.jpg簡介本節中我們介紹下給RecyclerView中的Item添加動畫。添加的動畫&#xff0c;分為&#xff0c;在打開列表時有Item的展示動畫&#xff0c;當滑動的時候沒有動畫和打開列表滑動時有動畫兩種實現過程實現一個列表效果如下Screenshot_2020-09-01-17-03-35-349…

Oracle數據庫查詢用 where in 查詢的項超過1000條的解決方案

眾所周知&#xff0c;如果我們的用SQL查詢語句時&#xff0c;如果用where in帶的參數超過1000條的話&#xff0c;oracle是會報錯的。 因為項目中遇到這樣的問題&#xff0c;所以找到了接下來我要說的這個辦法。 因為用的地方很多&#xff0c;所以我把這個封裝成了一個方法。 //…

geek_Ask How-To Geek:營救受感染的PC,安裝無膨脹iTunes和馴服瘋狂的觸控板

geekYou’ve got questions and we’ve got answers. Today we highlight how to save your computer if it’s so overrun by viruses and malware you can’t work from within Windows, install iTunes without all the bloat, and tame a hyper-sensitive trackpad. 您有問…

第1課:接口測試和jmeter總結

接口測試 1. 接口的分類&#xff1a;webService和http api接口1&#xff09; webService接口&#xff1a;是按照soap協議通過http傳輸&#xff0c;請求報文和返回報文都是xml格式&#xff0c;一般要借助工具來測試接口&#xff1b;2&#xff09; http api接口&#xff1a;是按照…

android 工作日,如何在Android上重復警報工作日

小編典典請嘗試此代碼。已在我的應用中成功運行if (chk_monday.isChecked()) {forday(2);} else if (chk_tuesday.isChecked()) {forday(3);} else if (chk_wednesday.isChecked()) {forday(4);} else if (chk_thursday.isChecked()) {forday(5);} else if (chk_friday.isCheck…

hdu4419

對于這類面積覆蓋的題&#xff0c;大致就兩點要注意的 1.同一把矩形放在笛卡爾坐標系上做 2.pushup函數要注意下細節:及在統計子區間和之前要先判斷是否有子區間 用sum數組來保存區間被覆蓋的情況&#xff0c;如果遇到多次覆蓋問題&#xff0c;那就開多個sum數組分別保存被覆蓋…

最簡單 - 單例模式

public class Person {// Person 引用private static Person p null;static {if (p null) {p new Person();}}/***單例模式獲取Person對象. * return*/public static Person getInstance(){return p;} ?} 復制代碼轉載自&#xff1a; 簡書 - 低至一折起 文章&#xff1a;w…