Linux常用命令_tail命令

Linux常用命令_tail命令

tail 命令

tail命令和head 命令非常相似,只不过它是打印文件的尾部内容的

参数说明

参数参数说明
-n根据文件行数进行打印
-c根据文件字节进行输出打印
-v总是显示文件名的文件头
-q不显示文件名的文件头
-f循环读取文件内容
-s与-f合用,表示在每次反复的间隔休眠S秒
–pid与-f合用,表示在进程ID,PID死掉之后结束
[root@localhost home]# tail --help
Usage: tail [OPTION]... [FILE]...
Print the last 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -c, --bytes=K            output the last K bytes; alternatively, use -c +K
                           to output bytes starting with the Kth of each file
  -f, --follow[={name|descriptor}]
                           output appended data as the file grows;
                           -f, --follow, and --follow=descriptor are
                           equivalent
  -F                       same as --follow=name --retry
  -n, --lines=K            output the last K lines, instead of the last 10;
                           or use -n +K to output lines starting with the Kth
      --max-unchanged-stats=N
                           with --follow=name, reopen a FILE which has not
                           changed size after N (default 5) iterations
                           to see if it has been unlinked or renamed
                           (this is the usual case of rotated log files).
                           With inotify, this option is rarely useful.
      --pid=PID            with -f, terminate after process ID, PID dies
  -q, --quiet, --silent    never output headers giving file names
      --retry              keep trying to open a file even when it is or
                             becomes inaccessible; useful when following by
                             name, i.e., with --follow=name
  -s, --sleep-interval=N   with -f, sleep for approximately N seconds
                             (default 1.0) between iterations.
                           With inotify and --pid=P, check process P at
                           least once every N seconds.
  -v, --verbose            always output headers giving file names
      --help     display this help and exit
      --version  output version information and exit

If the first character of K (the number of bytes or lines) is a `+',
print beginning with the Kth item from the start of each file, otherwise,
print the last K items in the file.  K may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

With --follow (-f), tail defaults to following the file descriptor, which
means that even if a tail'ed file is renamed, tail will continue to track
its end.  This default behavior is not desirable when you really want to
track the actual name of the file, not the file descriptor (e.g., log
rotation).  Use --follow=name in that case.  That causes tail to track the
named file in a way that accommodates renaming, removal and creation.

Report tail bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'tail invocation'
tail默认参数

显示文件最后10行内容

[root@localhost home]# tail file.txt # 默认显示最后10行内容
# 等同于  tail -n 10 file.txt
# 等同于  tail -10 file.txt
[root@localhost home]# cat file.txt 
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
[root@localhost home]# tail file.txt # 默认显示最后10行内容
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
[root@localhost home]# tail -n 10 file.txt 
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
[root@localhost home]# tail -10 file.txt 
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
tail -c参数

根据文件字节进行输出打印

[root@localhost home]# tail -c 5 file1.txt  # 打印file1.txt中的最后5 bytes (数字时一字节,字母是两字节)
[root@localhost home]# tail -c +5 file1.txt  # 打印file1.txt文件的所有内容除了前面的4个字节,即从第五个字节开始显示
[root@localhost home]# cat file1.txt 
1a
2b
3c
4a
[root@localhost home]# tail -c 5 file1.txt  # 打印file1.txt中的最后5 bytes (数字时一字节,字母是两字节)
c
4a
[root@localhost home]# tail -c +5 file1.txt  # 打印file1.txt文件的所有内容除了前面的4个字节,即从第五个字节开始显示
b
3c
4a
tail -n参数

根据文件行数进行打印

[root@localhost home]# tail -n 3 file1.txt  # 打印file1.txt最后的3行内容
[root@localhost home]# tail -n +3 file1.txt  # 打印file1.txt从3行开始的内容
[root@localhost home]# tail -n 3 file1.txt  # 打印file1.txt最后的3行内容
2b
3c
4a
[root@localhost home]# tail -n +3 file1.txt  # 打印file1.txt从3行开始的内容
3c
4a
tail -q参数

不打印文件名称信息

[root@localhost home]# tail -q file1.txt  # 不打印文件名称信息
# 等同于  tail --quiet file1.txt 
# 等同于  tail --silent file1.txt 
[root@localhost home]# tail -q file1.txt  # 不打印文件名称信息
1a
2b
3c
4a
[root@localhost home]# tail --quiet file1.txt 
1a
2b
3c
4a
[root@localhost home]# tail --silent file1.txt 
1a
2b
3c
4a
tail -v参数

打印文件名称信息

[root@localhost home]# tail -v file1.txt  # 打印文件名称信息
# 等同于  tail --verbose file1.txt 
[root@localhost home]# tail -v file1.txt  # 打印文件名称信息
==> file1.txt <==
1a
2b
3c
4a
[root@localhost home]# tail --verbose file1.txt 
==> file1.txt <==
1a
2b
3c
4a
tail -f参数

循环读取文件内容

[root@localhost home]# tail -f file1.txt  # 循环读取文件内容
#另一个窗口给文件加内容,tail -f会显示添加的内容
[root@localhost home]# echo 5e >> file1.txt 
You have new mail in /var/spool/mail/root
[root@localhost home]# tail -f file1.txt 
1a
2b
3c
4a
5e
tail -s参数

与-f合用,表示在每次反复的间隔休眠S秒 (默认1秒)

[root@localhost home]# tail -f -s 5 file1.txt  # 每隔5秒查看一次file1.txt的内容是否更新
1a
2b
3c
4a
5e
tail --pid参数

与-f合用,表示在进程ID,PID死掉之后结束。

[root@localhost home]# ping 127.0.0.1 > test.log &
[1] 3152
[root@localhost home]# tail -f --pid=3152 test.log  # 当杀掉进程后,tail会停止
64 bytes from 127.0.0.1: icmp_seq=121 ttl=64 time=0.038 ms
64 bytes from 127.0.0.1: icmp_seq=122 ttl=64 time=0.068 ms
[1]+  Killed                  ping 127.0.0.1 > test.log
tail --version

显示版本信息并退出

[root@localhost home]# tail --version  # 显示版本信息并退出
tail (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Paul Rubin, David MacKenzie, Ian Lance Taylor,
and Jim Meyering.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值