tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新,使你看到最新的文件内容
1、命令格式:
tail[必要参数][选择参数][文件]
2、命令功能:
用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理。常用查看日志文件。
3、命令参数:
-c<数目> 显示的字节数
-n<行数> 显示行数
-f 循环读取
4、使用实例:
实例1:显示文件末尾内容
命令:
tail -n 5 /etc/fstab
输出:
[BEGIN] 2016/1/8 16:38:54 [[email protected] ~]# cat -n /etc/fstab 1 2 # 3 # /etc/fstab 4 # Created by anaconda on Thu Dec 31 16:39:19 2015 5 # 6 # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ 7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info 8 # 9 /dev/mapper/vgLegion-root / ext4 defaults,acl 1 1 10 UUID=4bdd95a5-f81b-4cfd-a446-dc4f5070c2aa /boot ext4 defaults 1 2 11 /dev/mapper/vgLegion-data /data ext4 defaults,acl 1 2 12 /dev/mapper/vgLegion-home /home ext4 defaults,acl 1 2 13 /dev/mapper/vgLegion-usr /usr ext4 defaults,acl 1 2 14 /dev/mapper/vgLegion-swap swap swap defaults,acl 0 0 15 tmpfs /dev/shm tmpfs defaults 0 0 16 devpts /dev/pts devpts gid=5,mode=620 0 0 17 sysfs /sys sysfs defaults 0 0 18 proc /proc proc defaults 0 0 [[email protected] ~]# tail -n 5 /etc/fstab /dev/mapper/vgLegion-swap swap swap defaults,acl 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 [END] 2016/1/8 16:39:22
说明:命令tail -n 5 仅显示前五行信息
实例2:跟踪显示文件新追加的内容
命令:
tail -f /tmp/fstab
输出:
[BEGIN] 2016/1/8 16:46:13 [[email protected] ~]# cat -n /etc/fstab # # /etc/fstab # Created by anaconda on Thu Dec 31 16:39:19 2015 # # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/vgLegion-root / ext4 defaults,acl 1 UUID=4bdd95a5-f81b-4cfd-a446-dc4f5070c2aa /boot ext4 defaults 1 2 /dev/mapper/vgLegion-data /data ext4 defaults,acl 1 2 /dev/mapper/vgLegion-home /home ext4 defaults,acl 1 2 /dev/mapper/vgLegion-usr /usr ext4 defaults,acl 1 2 /dev/mapper/vgLegion-swap swap swap defaults,acl 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 [[email protected] ~]# tail -f /tmp/fstab /dev/mapper/vgLegion-root / ext4 defaults,acl 1 1 UUID=4bdd95a5-f81b-4cfd-a446-dc4f5070c2aa /boot ext4 defaults 1 2 /dev/mapper/vgLegion-data /data ext4 defaults,acl 1 2 /dev/mapper/vgLegion-home /home ext4 defaults,acl 1 2 /dev/mapper/vgLegion-usr /usr ext4 defaults,acl 1 2 /dev/mapper/vgLegion-swap swap swap defaults,acl 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 how are you how old are you ^C [END] 2016/1/8 16:47:41
说明:该文件/tmp/fstab内容并没有
how are you how old are you
而是追加进去才显示的,同时打开另一个终端,一下是对该文件下的追加
[BEGIN] 2016/1/8 16:46:23 [[email protected] ~]# vi /tmp/fstab [[email protected] ~]# echo "how are you" >> /tmp/fstab [[email protected] ~]# echo "how old are you" >> /tmp/fstab [END] 2016/1/8 16:47:44
而追加什么内容,另一会话终端都会显示到屏幕上
时间: 2024-12-29 12:50:43