目录
23. shell命令--tail
功能说明
tail 是 Linux 和其他 Unix-like 系统中一个非常有用的命令,用于显示文件的最后部分。它通常用于查看日志文件的内容,因为日志文件通常很大,但你可能只对最近的条目感兴趣。
tail命令 用于输入文件中的尾部内容。
- 默认在屏幕上显示指定文件的末尾10行。
- 处理多个文件时会在各个文件之前附加含有文件名的行。
- 如果没有指定文件或者文件名为-,则读取标准输入。
- 如果表示字节或行数的NUM值之前有一个+号,则从文件开头的第NUM项开始显示,而不是显示文件的最后NUM项。
- NUM值后面可以有后缀:
- b : 512
- kB : 1000
- k : 1024
- MB : 1000 * 1000
- M : 1024 * 1024
- GB : 1000 * 1000 * 1000
- G : 1024 * 1024 * 1024
- T、P、E、Z、Y等以此类推。
语法格式
tail [参数] [文件]
SYNOPSIS
tail [OPTION]... [FILE]...
选项说明
-c, --bytes=NUM 输出文件尾部的NUM(NUM为整数)个字节内容。
-f, --follow[={name|descript}] 显示文件最新追加的内容。“name”表示以文件名的方式监视文件的变化。
-F 与 “--follow=name --retry” 功能相同。
-n, --line=NUM 输出文件的尾部NUM(NUM位数字)行内容。
--pid=<进程号> 与“-f”选项连用,当指定的进程号的进程终止后,自动退出tail命令。
-q, --quiet, --silent 当有多个文件参数时,不输出各个文件名。
--retry 即是在tail命令启动时,文件不可访问或者文件稍后变得不可访问,都始终尝试打开文件。使用此选项时需要与选项“--follow=name”连用。
-s, --sleep-interal=<秒数> 与“-f”选项连用,指定监视文件变化时间隔的秒数。
-v, --verbose 当有多个文件参数时,总是输出各个文件名。
--help 显示指令的帮助信息。
--version 显示指令的版本信息。
实践操作
1. 默认显示指定文件尾部的后10行内容
cat -n /etc/passwd
tail /etc/passwd
2. 指定显示指定文件尾部的后N行内容
cat -n /etc/passwd
tail -n +20 /etc/passwd
tail -n 5 /etc/passwd
tail -n3 /etc/passwd
tail -2 /etc/passwd
3. 指定显示指定文件尾部的后N个字节
tail -c 50 /etc/passwd
tail -c 100 /etc/passwd
4. 持续刷新显示指定文件尾部的后10行内容
4.1 终端1执行
touch file.txt
tail -F file.txt #等同于--follow=name --retry,根据文件名进行追踪,并保持重试,即该文件被删除或改名后,如果再次创建相同的文件名,会继续追踪
4.2 终端2执行
echo a >>file.txt #再次开启一个终端
echo b >>file.txt
echo c >>file.txt
seq 20 >>file.txt
rm -rf file.txt
echo test >file.txt
4.3 终端1执行
Ctrl + C 终止
tail -f file.txt #等同于--follow=descriptor,根据文件描述符进行追踪,当文件改名或被删除,追踪停止
4.4 终端2执行
echo 111 >>file.txt
echo 111 >file.txt
seq 10 >file.txt
rm -rf file.txt
echo 111 >file.txt
seq 10 >file.txt
cat file.txt
5. 多个文件同时显示
tail -2 ~/.bashrc
tail -2 ~/.bash_profile
tail -n 2 ~/.bashrc ~/.bash_profile
命令示例:1. 默认显示指定文件尾部的后10行内容
seq 20 >file1
tail file1
输出结果:
[root@MineGi ~]# seq 20 >file1
[root@MineGi ~]# tail file1
11
12
13
14
15
16
17
18
19
20
[root@MineGi ~]#
命令示例:2. 指定显示指定文件尾部的后N行内容
tail -n +20 file1
tail -n 5 file1
tail -n3 file1
tail -2 file1
输出结果:
[root@MineGi ~]# tail -n +20 file1
20
[root@MineGi ~]# tail -n 5 file1
16
17
18
19
20
[root@MineGi ~]# tail -n3 file1
18
19
20
[root@MineGi ~]# tail -2 file1
19
20
[root@MineGi ~]#
命令示例:3. 指定显示指定文件尾部的后N个字节
tail -c 50 /etc/passwd
tail -c 100 /etc/passwd
输出结果:
[root@MineGi ~]# tail -c 50 /etc/passwd
in
testuser:x:1000:1000::/home/testuser:/bin/bash
[root@MineGi ~]# tail -c 100 /etc/passwd
hrony:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
testuser:x:1000:1000::/home/testuser:/bin/bash
[root@MineGi ~]#
命令示例:4. 持续刷新显示指定文件尾部的后10行内容
4.1 终端1执行
touch file.txt
tail -F file.txt
4.2 终端2执行
echo a >>file.txt
echo b >>file.txt
echo c >>file.txt
seq 5 >>file.txt
rm -rf file.txt
echo test >file.txt
输出结果:
[root@MineGi ~]# touch file.txt
[root@MineGi ~]# tail -F file.txt
a
b
c
1
2
3
4
5
tail: "file.txt" 已不可访问: 没有那个文件或目录
test
^C
[root@MineGi ~]#
[root@MineGi ~]# echo a >>file.txt
[root@MineGi ~]# echo b >>file.txt
[root@MineGi ~]# echo c >>file.txt
[root@MineGi ~]# seq 5 >>file.txt
[root@MineGi ~]# rm -rf file.txt
[root@MineGi ~]# echo test >file.txt
[root@MineGi ~]#
命令示例:5. 多个文件同时显示
tail -2 ~/.bashrc
tail -2 ~/.bash_profile
tail -n 2 ~/.bashrc ~/.bash_profile
输出结果:
[root@MineGi ~]# tail -2 ~/.bashrc
. /etc/bashrc
fi
[root@MineGi ~]# tail -2 ~/.bash_profile
export PATH
[root@MineGi ~]# tail -n 2 ~/.bashrc ~/.bash_profile
==> /root/.bashrc <==
. /etc/bashrc
fi
==> /root/.bash_profile <==
export PATH
[root@MineGi ~]#