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.mzph.cn/news/278925.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

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…

windows平台下基于QT和OpenCV搭建图像处理平台

在之前的博客中,已经分别比较详细地阐述了“windows平台下基于VS和OpenCV”以及“Linux平台下基于QT和OpenCV"搭建图像处理框架,并且生成了相应的免费视频。这篇博客的主要内容,就是基于最新版本的相应工具,在windows平台下&…

android死锁解决方案,【线程死锁】Android多线程死锁产生的原因以及如何避免

一、死锁定义1、生活中的列子两人吃饭,但只有一双筷子,2人轮流吃(同时拥有2只筷子才能吃),某个时候一人拿了左筷子,一人拿了右筷子,两人同时占用一个资源,等待另一个资源,这时候甲等乙吃完并释放…

android mvvm 官方例子,详解Android的MVVM框架 - 数据绑定

本教程是跟着 Data Binding Guide学习过程中得出的一些实践经验,同时修改了官方教程的一些错误,每一个知识点都有对应的源码,争取做到实践与理论相结合。Data Binding 解决了 Android UI 编程中的一个痛点,官方原生支…

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/ 上看到的,比较容易理解。 而且,…

Android添加item动画,RecyclerView基础篇-Item添加动画

Android_Banner.jpg简介本节中我们介绍下给RecyclerView中的Item添加动画。添加的动画,分为,在打开列表时有Item的展示动画,当滑动的时候没有动画和打开列表滑动时有动画两种实现过程实现一个列表效果如下Screenshot_2020-09-01-17-03-35-349…

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. 接口的分类:webService和http api接口1) webService接口:是按照soap协议通过http传输,请求报文和返回报文都是xml格式,一般要借助工具来测试接口;2) http api接口:是按照…

极客时间和极客学院_极客历史记录的本周:Twitter的诞生,OS X十周年以及太空停留时间最长的时代即将结束...

极客时间和极客学院Every week we bring you interesting trivia and milestones from the archives of Geekdom. Today we’re taking a peek at the birth of Twitter, ten years of Mac OS X, and the longest space stay in history. 每周,我们都会为您带来有趣…

Android风格ppt,Material Design风格的快手PPT

突发奇想,感觉MD风格既然适合 Android 软件的界面,那么在一般PPT 演示中,效果当也是不错。于是在网上去寻了几处制作贴,也简单看了 MD 设计指南的几处要点。先试试一番再说。关于 MD 设计指南和几处制作贴,我会把链接贴…

dropbox链接过期_询问操作方法:“开始”菜单中的Dropbox,了解符号链接和翻录TV系列DVD...

dropbox链接过期This week we take a look at how to incorporate Dropbox into your Windows Start Menu, understanding and using symbolic links, and how to rip your TV series DVDs right to unique and high-quality episode files. 本周,我们来看看如何将D…

android listpreference 自定义,Android – 我的ListPreference中的自定义行布局

在我的Android应用程序中,我实现了从ListPreference扩展的类SubtitleColorListPreference.我需要这个,因为我需要为列表中的每个项目设置自己的布局.一切正常,它看起来像这样:重要的代码是onPrepareDialogBu​​ilder(AlertDialog.Builder builder)中的方法,我在其中…

火狐 增强查找工具栏_在“提示”框中:简单的IE至Firefox同步,轻松的Windows工具栏和识别USB电缆...

火狐 增强查找工具栏() Every week we tip into our mail bag and share great tips from your fellow readers. This week we’re looking at an easy way to sync your bookmarks between IE and Firefox, using simple Windows toolbars, and a clever way to ID USB cables…

Input Director使用一个键盘和鼠标即可控制多台Windows计算机

The problem is having two or more PC’s and having to go back and forth between workstation. Input Director solves the problem by allowing you to control multiple Windows systems with only one keyboard and mouse on the Master PC. 问题是拥有两台或更多台PC…

excel导入csv文件_如何将包含以0开头的列的CSV文件导入Excel

excel导入csv文件Microsoft Excel will automatically convert data columns into the format that it thinks is best when opening comma-separated data files. For those of us that don’t want our data changed, we can change that behavior. Microsoft Excel将在打开…

android 9.0新ui,SystemUI分析(Android9.0)

8种机械键盘轴体对比本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?一、SystemUI组成SystemUI是Android的系统界面,包括状态栏statusbar、锁屏keyboard、任务列表recents等等,都继承于SystemUI这个类&#xf…

chromebook刷机_如何为不支持Chrome操作系统的网站欺骗Chromebook用户代理

chromebook刷机Not all browsers handle websites the same, and if they don’t support your operating system or browser, you could be denied access. Luckily, you can spoof the user agent on Chrome OS to make it look like you use a completely different system.…

cocos android-1,cocos2dx在windows下开发,编译到android上(1)

转自:http://www.2cto.com/kf/201205/130697.html下面我给大家介绍下,用vs2010开发cocos2dx,然后如何使其编译到android上。步骤如下:1、必要条件,你的eclipse能把代码编译到安卓手机或虚拟机上,如果这一步…

中药ppi网络图太杂乱_太杂乱了吗? 这是您的iPhone,iPad,Android或台式机的15张简约壁纸...

中药ppi网络图太杂乱Busy wallpaper images don’t work very well on your iPhone, iPad, or any device where you need to have lots of icons on the screen. Here’s a set of minimalistic wallpaper images that won’t clutter up your desktop. 繁忙的墙纸图像在iPhon…