简介
tail命令默认将每个文件的最后10行打印到标准输出。对于多个文件,在每个文件前面加上一个给出文件名的头。如果没有文件,或者文件为**-**
,则读取标准输入。
命令格式
tail [OPTION]… [FILE]…
常用参数
-n Num | 显示末尾指定的 Num 行 |
---|---|
-n +Num | 显示从 Num 行开始到文件末尾的内容 |
-f | 跟踪显示文件新追加的内容 Ctrl + c 可以终止 |
-c Num | 输出文件尾部的 Num 个字节内容 |
示例
[root@localhost ~]# cut -d ":" -f 1 /etc/passwd | head -n 11
[root@localhost ~]# cat -n test.txt
1 root
2 bin
3 daemon
4 adm
5 lp
6 sync
7 shutdown
8 halt
9 mail
10 operator
11 games
## 默认打印 10 行
[root@localhost ~]# tail test.txt | cat -n
1 bin
2 daemon
3 adm
4 lp
5 sync
6 shutdown
7 halt
8 mail
9 operator
10 games
## 显示指定的 2 行
[root@localhost ~]# tail -n 2 test.txt
operator
games
## 显示第 5 行到文件末尾的内容
[root@localhost ~]# cat -n test.txt | tail -n +5
5 lp
6 sync
7 shutdown
8 halt
9 mail
10 operator
11 games
## 查看多个文件
[root@localhost ~]# tail -n 2 /etc/passwd /etc/shadow
==> /etc/passwd <==
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
gkdaxue:x:1000:1000::/home/gkdaxue:/bin/bash
==> /etc/shadow <==
apache:!!:18737::::::
gkdaxue:!!:18746:0:99999:7:::
## 显示最后的 6 个字节
[root@localhost ~]# tail -c 6 test.txt
games
## -f 命令
[root@localhost ~]# echo aa > filea.txt
[root@localhost ~]# cat filea.txt
aa
[root@localhost ~]# tail -f filea.txt
aa
<== 会在这里卡着
## 然后我们打开第二个终端
[root@localhost ~]# echo bbb >> filea.txt
## 然后在回到第一个终端
[root@localhost ~]# tail -f filea.txt
aa
bbb <== 输出了最新的内容
<== 在这卡着了, ctrl + c 可以终止
^C
## - 的使用, 只显示最后两行
[root@localhost ~]# tail -n 2 -
aaa
bbb
ccc
ddd
<== 在此处按 ctrl + d 键,既可以得到下面结果
ccc
ddd