tail:显示文件内容尾部

xiaohai 2021-06-02 10:52:20 3112人围观 标签: Linux 
简介tail命令用于显示文件内容的尾部,默认显示文件的最后10行
说明

  tail命令用于显示文件内容的尾部,默认显示文件的最后10行

格式

  tail [option] [filename]

常用参数说明
  • -n:指定显示的行数
  • -c:指定显示的字节数
  • -f:事实输出文件变化后追加的数据,如果文件不存在会报错,可以使用-F参数等待文件生成
  • -s:监视文件变化的间隔秒数,默认为1s
  • -q:不显示给定文件的文件头
  • -v:总是显示给定文件的文件头
  • -F:等同于-f —retry
  • —retry:不停的尝试打开文件直到打开为止
示例

1、不使用参数

[root@localhost ~]# tail /etc/passwd
user01:x:1004:1004::/home/user01:/bin/bash
user02:x:1005:1005::/home/user02:/bin/bash
user03:x:1006:1006::/home/user03:/bin/bash
user04:x:1007:1007::/home/user04:/bin/bash
user05:x:1008:1008::/home/user05:/bin/bash
user06:x:1009:1009::/home/user06:/bin/bash
user07:x:1010:1010::/home/user07:/bin/bash
user08:x:1011:1011::/home/user08:/bin/bash
user09:x:1012:1012::/home/user09:/bin/bash
user10:x:1013:1013::/home/user10:/bin/bash

2、使用-n参数,指定行数

[root@localhost ~]# tail -n 3 /etc/passwd
user08:x:1011:1011::/home/user08:/bin/bash
user09:x:1012:1012::/home/user09:/bin/bash
user10:x:1013:1013::/home/user10:/bin/bash

3、指定-c参数,指定字节数

[root@localhost ~]# tail -c 10 /etc/passwd
/bin/bash

4、指定-f参数,监控文件的变化,默认1s刷新一次

[root@localhost ~]# tail -f test.txt 
heill
xxi
ab
def

#从另一个终端向test.txt文件追加内容,查看变化

5、指定-F参数

[root@localhost ~]# tail -f log.txt #-f参数,log.txt文件不存在会提示并退出
tail: cannot open ‘log.txt’ for reading: No such file or directory
tail: no files remaining

[root@localhost ~]# tail -F log.txt #也会提示,但是终端会一直等待,直到文件存在,并输出文件内容
tail: cannot open ‘log.txt’ for reading: No such file or directory
#测试可以使用另一个终端向log.txt文件追加内容,即可看出变化