专业编程基础技术教程

网站首页 > 基础教程 正文

使用“tail”命令监控一切(监控命令功能)

ccvgpt 2024-07-21 17:39:26 基础教程 18 ℃

更多互联网新鲜资讯、工作奇淫技巧关注【飞鱼在浪屿】(日更新)


介绍

这篇文章是关于如何使用'tail'命令的。下面概述了如何使用“head”和“tail”命令输出文件的不同部分:

使用“tail”命令监控一切(监控命令功能)

Head命令

Tail命令

使用以下命令输出文件/流的前 3 行:

head -n 3 business-tips.txt  #  (POSIX Compatible)
#  OR
head -n +3 business-tips.txt #  (Not POSIX Compatible)

business-tips.txt(绿线将作为输出打印)

1) Buy low, sell high.
2) Invest with a plan.
3) Follow your heart.

4) Find your passion.

5) Believe in yourself.

6) Take risks, be bold.

7) Play it safe, and be considerate.

8) Realize that there are no limits.

9) Realize that you have limits.

10) You can rest when you're dead.

11) Take time to care for yourself.

12) Ignore what negative people say.

13) Listen to constructive criticism.

使用以下命令输出文件/steam 的最后 3 行。

tail -n 3 business-tips.txt   #  (POSIX Compatible)
#  OR
tail -n -3 business-tips.txt  #  (POSIX Compatible)

business-tips.txt(绿线将作为输出打印0)


1) Buy low, sell high.
2) Invest with a plan.
3) Follow your heart.
4) Find your passion.
5) Believe in yourself.
6) Take risks, be bold.
7) Play it safe, and be considerate.
8) Realize that there are no limits.
9) Realize that you have limits.
10) You can rest when you're dead.

11) Take time to care for yourself.
12) Ignore what negative people say.
13) Listen to constructive criticism.

使用以下命令输出文件/流的“头”或开头,省略最后 3 行:

head -n -3 business-tips.txt # (Not POSIX Compatible)

business-tips.txt(绿线将作为输出打印)

1) Buy low, sell high.
2) Invest with a plan.
3) Follow your heart.
4) Find your passion.
5) Believe in yourself.
6) Take risks, be bold.
7) Play it safe, and be considerate.
8) Realize that there are no limits.
9) Realize that you have limits.
10) You can rest when you're dead.

11) Take time to care for yourself.
12) Ignore what negative people say.
13) Listen to constructive criticism.

使用以下命令输出文件/流的“尾”端,从第 3 行开始直到结束:

tail -n +3 business-tips.txt  # (POSIX Compatible)

business-tips.txt(绿线将作为输出打印)

1) Buy low, sell high.
2) Invest with a plan.

3) Follow your heart.
4) Find your passion.
5) Believe in yourself.
6) Take risks, be bold.
7) Play it safe, and be considerate.
8) Realize that there are no limits.
9) Realize that you have limits.
10) You can rest when you're dead.
11) Take time to care for yourself.
12) Ignore what negative people say.
13) Listen to constructive criticism.

上表显示了使用“head”和“tail”命令输出文件开头和结尾的不同方式。您可以通过向这些命令传递正数或负数来获得不同的结果,尽管并非所有组合都符合 POSIX。因此,可能并非所有 head/tail 实现都支持它们。在上面显示的示例中,绿色文本是将被打印的内容,灰色文本是将被省略的部分。


'tail' 命令的最简单用法

您可以使用“tail”命令打印出文件或流的“尾端”。如果我们从以下文件 'business-tips.txt' 开始:

 1)  Buy low, sell high.
 2)  Invest with a plan.
 3)  Follow your heart.
 4)  Find your passion.
 5)  Believe in yourself.
 6)  Take risks, be bold.
 7)  Play it safe, and be considerate.
 8)  Realize that there are no limits.
 9)  Realize that you have limits.
10)  You can rest when you're dead.
11)  Take time to care for yourself.
12)  Ignore what negative people say.
13)  Listen to constructive criticism.

并像这样运行 tail 命令:

tail business-tips.txt

我们将得到以下输出:

 4)  Find your passion.
 5)  Believe in yourself.
 6)  Take risks, be bold.
 7)  Play it safe, and be considerate.
 8)  Realize that there are no limits.
 9)  Realize that you have limits.
10)  You can rest when you're dead.
11)  Take time to care for yourself.
12)  Ignore what negative people say.
13)  Listen to constructive criticism.

它显示了文件中的最后 10 行。要显示的默认行数是 10,但您可以使用“-n”标志来指定不同的行数。例如,这个命令:

tail -n 5 business-tips.txt

将输出以下内容:

 9)  Realize that you have limits.
10)  You can rest when you're dead.
11)  Take time to care for yourself.
12)  Ignore what negative people say.
13)  Listen to constructive criticism.

它显示了文件中的最后 5 行。


Tail 的“跟随”模式

tail 命令最常见的用途之一是使用“-f”标志时获得的“跟随”模式。如果我们运行这样的命令:

tail -f /var/log/kern.log

你会看到文件 '/var/log/kern.log' 的最后几行打印到屏幕上,光标将显示为“挂起”,好像它在等待什么:

在这种情况下,tail 命令正在等待将更多行附加到文件末尾,以便将它们打印到屏幕上。tail 命令将继续监视新行,直到您通过按住 CTRL 键和 'c' 键来中断 'tail'。

使用带有“-f”标志的 tail 命令是监视日志文件的常用方法。默认情况下,即使您使用了 '-f' 标志,'tail' 命令也会输出已经在文件末尾的几行。如果您只对监视发出 tail 命令发生的日志语句感兴趣,则使用“-f”时,加上包含值为 0 的“-n”标志:

tail -f -n 0 /var/log/apache2/access.log

这样,当您发出此 tail 命令时,您就不会被文件中已有的过时信息分心。您还可以使用“tail”命令一次跟踪多个文件,如下所示:

tail -f -n 0 /var/log/apache2/access.log /var/log/apache2/errors.log

发出上述命令后,tail 将输出写入任何文件的任何行。当同时跟踪多个文件时,'tail' 通常会打印出一个 'header',指示相应行来自哪个文件:

==> /var/log/apache2/access.log <==

==> /var/log/apache2/errors.log <==

==> /var/log/apache2/access.log <==
Hello from access log!

==> /var/log/apache2/errors.log <==
Hello from error log!

大多数 shell 还支持通配符,这使得使用“tail”同时指定大量文件变得更加方便:

tail -f -n 0 /var/log/apache2/*

关注需要提升权限的文件

这是一个非常具体但非常常见的场景,可能会导致您对“tail”命令产生一些困惑:如果您想使用通配符来跟踪目录中只有其他用户可读的多个文件,该怎么办?你可能会想“哦,我只会使用 sudo”并执行以下操作:

sudo tail -f -n 0 /var/log/apache2/*

但这不起作用!您仍然会遇到权限问题,因为您的 shell 会在命令的其余部分甚至传递给 sudo 之前尝试扩展通配符(“*”部分)。确切的解决方案将取决于您使用的 shell,但对于 bash shell,以下内容将起作用:

sudo sh -c 'tail -f -n 0 /var/log/apache2/*'

使用“tail”查找最近复制的 4 个文件

假设您正在将一些文件备份到外部硬盘驱动器,并且您想查看最近复制的几个文件的进度。您可以使用以下“ls”命令列出当前目录中的所有文件,最后显示最近修改的文件:

ls -ltr

它输出这样的东西:

...many many lines...
-rw-r--r-- 1 robert robert    8839640 Feb 27 00:30 my-cool-file-30356.dat
-rw-r--r-- 1 robert robert  194326506 Feb 27 00:30 my-cool-file-3093.dat
-rw-r--r-- 1 robert robert  665529770 Feb 27 00:30 my-cool-file-3396.dat
-rw-r--r-- 1 robert robert  629536792 Feb 27 00:31 my-cool-file-5005.dat
-rw-r--r-- 1 robert robert  987631920 Feb 27 00:31 my-cool-file-7551.dat
-rw-r--r-- 1 robert robert  654947606 Feb 27 00:31 my-cool-file-7606.dat
-rw-r--r-- 1 robert robert 1294157424 Feb 27 00:31 my-cool-file-8633.dat
-rw-r--r-- 1 robert robert   29884416 Feb 27 00:31 my-cool-file-8673.dat

但这显示了太多信息。如果我们只能看到 4 个最近修改过的文件,那会更好看。我们可以使用 tail 命令来做到这一点:

ls -ltr | tail -n 4

现在我们得到以下输出:

-rw-r--r-- 1 robert robert  987631920 Feb 27 00:31 my-cool-file-7551.dat
-rw-r--r-- 1 robert robert  654947606 Feb 27 00:31 my-cool-file-7606.dat
-rw-r--r-- 1 robert robert 1294157424 Feb 27 00:31 my-cool-file-8633.dat
-rw-r--r-- 1 robert robert   29884416 Feb 27 00:31 my-cool-file-8673.dat

但是手动一遍遍地运行这个很不方便。我们还可以利用“监视”命令一遍又一遍地运行此命令,以便我们可以看到文件复制的进度:

watch -n 0.1 -d 'ls -latr | tail -n 4'

使用上面的“-d”标志将导致“watch”突出显示差异,“-n”标志将导致命令每 0.1 秒重新运行一次,而不是默认的 2 秒间隔。


从开始而不是结束开始计数

您可能会遇到想要提取文件的“结尾”的情况,而您事先不知道“结尾”会持续多长时间。在这些情况下,'tail' 命令仍然可以帮助您,因为您可以从要开始输出的起点指定偏移量。例如,让我们考虑包含以下内容的文件“some_lines.txt”:

My Favourite Stonks:
GME
AMC
RKT
PLTR

为了清楚起见,我们可以使用带有“-n”标志的“cat”命令来添加一些行号:

cat -n some_lines.txt

产生这个输出:

     1	My Favourite Stonks:
     2	GME
     3	AMC
     4	RKT
     5	PLTR

如果我们只想要最后两行,我们会像这样使用 tail 命令:

cat -n some_lines.txt | tail -n 2

它提供了这个输出:

     4	RKT
     5	PLTR

但是,如果我们在两者前面加上一个加号,就会得到不同的解释:

cat -n some_lines.txt | tail -n +2

这将打印出从第 2 行开始直到结束的文件的“结束”(不管总行数如何):

     2	GME
     3	AMC
     4	RKT
     5	PLTR

如果我们再向这个文件中添加一些数据:

echo "FOO1" >> some_lines.txt
echo "FOO2" >> some_lines.txt

当我们发出相同的命令时,这些行现在也将被输出:

cat -n some_lines.txt | tail -n +2

将输出:

     2	GME
     3	AMC
     4	RKT
     5	PLTR
     6	FOO1
     7	FOO2

使用带有字节而不是行的“tail”

您还可以使用带有“-c”标志的“tail”命令提取最后几个字节而不是最后几行。下面是使用 tail 命令从上一个示例中提取文件的最后一个字节的示例:

cat -n some_lines.txt | tail -c 1

这不会打印任何不可见字符,因此让我们将结果通过管道传输到 xxd 以查看字节是什么:

cat -n some_lines.txt | tail -c 1 | xxd

结果是:

00000000: 0a                                       .

这表明文件中的最后一个字节是一个换行符,正如我们所期望的那样。让我们再次执行此操作并提取最后 4 个字符:

cat -n some_lines.txt | tail -c 4

提供此输出

LTR

如果我们将其通过管道传输到 xxd:

cat -n some_lines.txt | tail -c 4 | xxd

我们将看到以下内容:

00000000: 4c54 520a                                LTR.

寻找 3 本书

在关于 sort 命令的文章中,我们回顾了如何从以下未排序列表中查找 3 本书的示例:

Tropic of Cancer,Henry Miller,1934
Housekeeping,Marilynne Robinson,1981
Deliverance,James Dickey,1970
The Sun Also Rises,Ernest Hemingway,1926
The Great Gatsby,F. Scott Fitzgerald,1925
The Corrections,Jonathan Franzen,2001
The Berlin Stories,Christopher Isherwood,1946
Call It Sleep,Henry Roth,1935
Slaughterhouse-Five,Kurt Vonnegut,1969
Light in August,William Faulkner,1932

我们展示了如何使用 sort 命令根据三个不同的列对文件中的每一行进行排序:

sort -t ',' -k 3,3n -k 2,2 -k 1,1 -s books.txt

产生这个输出:

The Great Gatsby,F. Scott Fitzgerald,1925
The Sun Also Rises,Ernest Hemingway,1926
Light in August,William Faulkner,1932
Tropic of Cancer,Henry Miller,1934
Call It Sleep,Henry Roth,1935
The Berlin Stories,Christopher Isherwood,1946
Slaughterhouse-Five,Kurt Vonnegut,1969
Deliverance,James Dickey,1970
Housekeeping,Marilynne Robinson,1981
The Corrections,Jonathan Franzen,2001

我们可以将其直接传送到“tail”命令中,并告诉它只打印最后 3 行:

sort -t ',' -k 3,3n -k 2,2 -k 1,1 -s books.txt | tail -n 3

产生这个输出:

Deliverance,James Dickey,1970
Housekeeping,Marilynne Robinson,1981
The Corrections,Jonathan Franzen,2001

正如您在上面看到的,我们使用 'sort' 和 'tail' 命令的组合从未排序的列表中找到了 3 本书。


寻找最受欢迎的名字

在有关“uniq”命令的文章中,我们回顾了如何从名称列表中查找最受欢迎名称的示例:

Verity Jayda
Verity Jayda
Verity Jayda
Verity Jayda
Justy Kaiden
Christopher Rene
Christopher Rene
Christopher Rene
Branden McKenna
Branden McKenna

第一步是确保名称列表已排序(作为“uniq”命令的要求):

cat names.txt | sort

然后,名称列表通过管道传送到带有“-c”标志的“uniq”命令中,该标志提供每个名称出现次数的计数:

cat names.txt | sort | uniq -c

其输出是:

      2 Branden McKenna
      3 Christopher Rene
      1 Justy Kaiden
      4 Verity Jayda

现在,我们可以再次使用 'sort' 命令,但这次使用数字排序根据出现次数最多的名称对列表进行排序:

cat names.txt | sort | uniq -c | sort -n

它提供了这个输出:

      1 Justy Kaiden
      2 Branden McKenna
      3 Christopher Rene
      4 Verity Jayda

现在,我们只需要使用 'tail' 命令来挑选输出中的最后一行:

cat names.txt | sort | uniq -c | sort -n | tail -n 1

这个命令的输出是:

      4 Verity Jayda

而上面的名字是榜单中最受欢迎的名字。这个过程让许多名字的未排序列表变成了一个记录,告诉最流行的名字是什么。


潜在的无限内存使用

在某些情况下 ,tail 命令可能会遇到无限的内存需求。当您考虑到 tail 命令必须能够处理事先不知道输入长度的流这一事实时,这一点就变得很明显了。如果你想输出流的最后 N 行,你需要能够在内存中存储至少 N 行,这样当你最终检测到流的结尾时,你将能够输出它之前的内容。

为了给出一些数字,这里有一个人为的例子,它从 /dev/urandom 读取一堆行。然后我们使用 'head' 来抓取前 10,000,000 行并将它们通过管道传输到 'tail' 以获得最后一行。整个管道作为子 shell 通过 /usr/bin/time 运行,因此我们可以获得一些性能数据:

/usr/bin/time -v sh -c 'xxd /dev/urandom | head -n 10000000 | tail -n -1 | wc -l'

它给出了一些包含这些行的输出:

	Elapsed (wall clock) time (h:mm:ss or m:ss): 0:25.64
	Maximum resident set size (kbytes): 2104

如果我们开始增加 tail 应该输出的行数:

/usr/bin/time -v sh -c 'xxd /dev/urandom | head -n 10000000 | tail -n -100000 | wc -l'

最大内存使用量开始增加:

	Elapsed (wall clock) time (h:mm:ss or m:ss): 0:25.01
	Maximum resident set size (kbytes): 8768

并增加:

/usr/bin/time -v sh -c 'xxd /dev/urandom | head -n 10000000 | tail -n -1000000 | wc -l'
	Elapsed (wall clock) time (h:mm:ss or m:ss): 0:25.06
	Maximum resident set size (kbytes): 68656

并增加...

/usr/bin/time -v sh -c 'xxd /dev/urandom | head -n 10000000 | tail -n -10000000 | wc -l'
	Elapsed (wall clock) time (h:mm:ss or m:ss): 0:25.46
	Maximum resident set size (kbytes): 668992

从上面的输出可以看出,在上面的tail命令运行过程中,一次最多需要668992KiB或653.312MiB。显然,10,000,000 行是相当多的,但是对于“tail”命令的参数可能是脚本中任意大的变量的情况,您应该记住这一点。head 命令的非 POSIX 功能也可能出现类似的问题,该功能允许您将负值指定为“-n”。

Tags:

最近发表
标签列表