Linux 命令(文件和目录管理 - tail)

来源:互联网 发布:黑莓q5软件下载 编辑:程序博客网 时间:2024/06/13 09:49

简述

有 head ( 头 ) 自然就有 tail ( 尾巴 ) - 首尾呼应,tail 的用法和 head 差不多类似。只不过是显示后面几行,预设也是显示 10 行,若要显示非 10 行,使用 -n 选项即可。

  • 简述
  • 命令介绍
  • 命令选项
  • 使用范例

命令介绍

  • 命令名称
    tail

  • 基本语法
    tail [选项]… [文件]…

  • 功能描述
    将每个文件的最后 10 行打印到标准输出。对于多个文件,每个前面都有一个给出文件名的标题。如果没有指定文件或者文件名为“-”,读取标准输入。

命令选项

选项 说明 -c, --bytes=K 输出最后 K 个字节 -f, --follow[={name|descriptor}] 当文件增长时,输出后续添加的数据 -F--follow=name --retry 一样 -n, --lines=K 输出最后 K 行,而非默认的最后 10 行 --max-unchanged-stats=N--follow=name 合用, --pid=PID-f 合用,表示在进程 ID、PID 死掉之后结束。 -q, --quiet, --silent 从不输出给出文件名的首部 --retry 即使 tail 开始时就不能访问,或者在 tail 运行后不能访问,也仍然不停地尝试打开文件,-- 只与 -f 合用时有用。 -s, --sleep-interval=N-f 合用,表示在每次反复的间隔休眠 N 秒 -v, --verbose 总是输出给出文件名的首部 --help 显示帮助信息后退出 --version 输出版本信息后退出

如果 K(字节或者行数)的第一个字符为‘+’, 那么从每个文件的开头算起的第 K 项开始显示,否则,显示该文件的最后 K 项。K 可以有一个倍数后缀: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 同样适用。

如果希望即时追查一个文件的有效名称而非描述内容(例如循环日志),默认的程序动作并不如你所愿。在这种场合可以使用 --follow=name 选项,它会使 tail 定期追踪打开给定名称的文件,以确认它是否被删除或被其它某些程序重新创建过。

使用范例

1.不使用任何选项执行 tail

默认情况下,将文件的最后 10 行打印到标准输出。

[root@localhost Python-3.5.2]# tail README Copyright (c) 1991-1995 Stichting Mathematisch Centrum.  All rights reserved.See the file "LICENSE" for information on the history of this software,terms & conditions for usage, and a DISCLAIMER OF ALL WARRANTIES.This Python distribution contains *no* GNU General Public License (GPL) code,so it may be used in proprietary projects.  There are interfaces to some GNUcode but these are entirely optional.All trademarks referenced herein are property of their respective holders.[root@localhost Python-3.5.2]# 

2.输出最后 K 行的内容

如果要输出最后 K(这里为 5)行的内容,而非默认的最后 10 行,使用 -n 选项。

[root@localhost Python-3.5.2]# tail -n 5 README This Python distribution contains *no* GNU General Public License (GPL) code,so it may be used in proprietary projects.  There are interfaces to some GNUcode but these are entirely optional.All trademarks referenced herein are property of their respective holders.[root@localhost Python-3.5.2]# 

3.从第 K 行开始显示文件的内容

从第 K(这里为 218)行开始显示文件的内容,直至文件末尾。

# 查看文件 README 的内容[root@localhost Python-3.5.2]# cat -n README      1  This is Python version 3.5.2     2  ============================     3       4  Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,     5  2012, 2013, 2014, 2015, 2016 Python Software Foundation.  All rights reserved.   # 部分显示结果省略...   215  See the file "LICENSE" for information on the history of this software,   216  terms & conditions for usage, and a DISCLAIMER OF ALL WARRANTIES.   217     218  This Python distribution contains *no* GNU General Public License (GPL) code,   219  so it may be used in proprietary projects.  There are interfaces to some GNU   220  code but these are entirely optional.   221     222  All trademarks referenced herein are property of their respective holders.# 从第 218 行开始显示文件 README 的内容,直至文件末尾。[root@localhost Python-3.5.2]# tail -n +218 README This Python distribution contains *no* GNU General Public License (GPL) code,so it may be used in proprietary projects.  There are interfaces to some GNUcode but these are entirely optional.All trademarks referenced herein are property of their respective holders.[root@localhost Python-3.5.2]# 

4.输出最后 K 个字节的内容

使用 -c 选项,可以指定显示文件的最后 K(这里为 5)个字节的内容。

[root@localhost Python-3.5.2]# tail -c 5 README ers.[root@localhost Python-3.5.2]# 

5.当文件增长时,输出后续添加的数据

由于 /var/log/message.log 随时会有资料写入,想要让该档案在有资料写入时就立刻显示到屏幕上,就利用 -f 选项,可以一直侦测该档案,新加入的资料都会被显示到屏幕上,直到按下 Ctrl + C 才会离开。

[root@localhost Python-3.5.2]# tail -f /var/log/message.log

6.和其他命令组合使用

列出一个目录下的文件,如果内容过多,可以用 tail 来过滤显示,需要和管道符 | 结合起来。

[root@localhost Python-3.5.2]# ls -l总用量 34676-rw-r--r--.  1 wang wang     8464 626 05:38 aclocal.m4drwxr-xr-x.  5 root root       79 9月   7 20:30 build-rwxr-xr-x.  1 wang wang    42856 626 05:38 config.guess-rw-r--r--.  1 root root   835023 97 15:06 config.log-rwxr-xr-x.  1 root root    40031 97 15:06 config.status-rwxr-xr-x.  1 wang wang    35740 626 05:38 config.sub-rwxr-xr-x.  1 wang wang   474932 626 05:38 configure-rw-r--r--.  1 wang wang   155069 626 05:38 configure.acdrwxrwxr-x. 18 wang wang     4096 6月  26 05:47 Docdrwxrwxr-x.  2 wang wang       20 6月  26 05:38 Grammardrwxrwxr-x.  2 wang wang     4096 6月  26 05:38 Include-rwxr-xr-x.  1 wang wang     7122 626 05:38 install-shdrwxrwxr-x. 47 wang wang     8192 9月   7 20:29 Lib-rw-r--r--.  1 root root 21053416 97 20:29 libpython3.5m.a-rw-r--r--.  1 wang wang    12767 626 05:38 LICENSEdrwxrwxr-x.  8 wang wang     4096 6月  26 05:38 Mac-rw-r--r--.  1 root root    66393 97 15:06 Makefile-rw-r--r--.  1 root root    58147 97 15:06 Makefile.pre-rw-r--r--.  1 wang wang    58449 626 05:38 Makefile.pre.indrwxrwxr-x.  2 wang wang     4096 9月   7 15:06 Miscdrwxrwxr-x. 11 wang wang     8192 9月   7 20:29 Modulesdrwxrwxr-x.  4 wang wang     4096 9月   7 20:28 Objectsdrwxrwxr-x.  2 wang wang     4096 9月   7 20:28 Parserdrwxrwxr-x.  4 wang wang     4096 6月  26 05:38 PCdrwxrwxr-x.  2 wang wang     4096 6月  26 05:38 PCbuilddrwxrwxr-x.  2 wang wang     4096 9月   7 20:30 Programs-rw-r--r--.  1 root root       26 97 20:29 pybuilddir.txt-rw-r--r--.  1 root root    43899 97 15:06 pyconfig.h-rw-r--r--.  1 wang wang    41897 626 05:38 pyconfig.h.in-rwxr-xr-x.  1 root root 12284727 97 20:29 pythondrwxrwxr-x.  3 wang wang     4096 9月   7 20:29 Python-rw-r--r--.  1 root root     3080 97 15:14 python-config-rw-r--r--.  1 root root     2042 97 15:14 python-config.py-rw-r--r--.  1 root root    61170 97 15:14 python-gdb.py-rw-r--r--.  1 wang wang     8060 626 05:38 README-rw-r--r--.  1 wang wang    99778 626 05:38 setup.pydrwxrwxr-x. 22 wang wang     4096 6月  26 05:38 Tools# 列出最后 5 个文件[root@localhost Python-3.5.2]# ls -l | tail -5-rw-r--r--.  1 root root     2042 97 15:14 python-config.py-rw-r--r--.  1 root root    61170 97 15:14 python-gdb.py-rw-r--r--.  1 wang wang     8060 626 05:38 README-rw-r--r--.  1 wang wang    99778 626 05:38 setup.pydrwxrwxr-x. 22 wang wang     4096 6月  26 05:38 Tools
0 0
原创粉丝点击