Linux head和tail命令

简介:

介绍

 head和tail是一组想对应的命令,默认分别显示文件的开头和末尾10行记录。

 

head命令

head命令默认显示头部的前N行

复制代码
Usage: head [OPTION]... [FILE]...
默认输出前10行
当指定多个文件时,在输出前输出各个文件名
当没有指定文件或指定的文件是“-”说明读取标准输入

Mandatory arguments to long options are mandatory for short options too.
  -c, --bytes=[-]K         print the first K bytes of each file;
                             with the leading `-', print all but the last
                             K bytes of each file 显示头部前多少字节,这里的K是单位的标示,具体的单位参考下面列表,当使用-K时显示除了尾部多少字节外的所有内容
  -n, --lines=[-]K         print the first K lines instead of the first 10;
                             with the leading `-', print all but the last
                             K lines of each file显示头部的多少行,默认显示头部10行,例如-n -b代表显示除了尾部512行外的所有行
  -q, --quiet, --silent    never print headers giving file names当指定多个文件时,出现内容中不输出文件名
  -v, --verbose            always print headers giving file names
      --help     display this help and exit
      --version  output version information and exit

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.
复制代码

tail命令

tail命令同head命名正好相反,tail默认输出尾部10行

复制代码
Usage: tail [OPTION]... [FILE]...
默认输出尾部10行
当指定多个文件时,默认在输出内容前显示文件名
当没有指定文件或者指定的文件是“-”时,读取标准输入

Mandatory arguments to long options are mandatory for short options too.对于长选项不可忽略的参数对应短选项也不可忽略
  -c, --bytes=K            output the last K bytes; alternatively, use -c +K显示尾部的字节数,当使用+k参数时表示从k字节开始显示到末尾,例如-c +1b 显示512字节开始到末尾的内容
                           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等同于使用 -f filename --retry操作
  -n, --lines=K            output the last K lines, instead of the last 10;默认显示输出尾部的10行,当使用+k参数时指从第K行开始显示到末尾,例如-n +6显示第6行到末尾的内容
                           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--指定进程id,可以结合-f参数一起使用,当指定的进程终止时显示也终止
  -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当使用-f参数时可以,使用-s显示间隔
                             (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.
复制代码

tail命令参数和head概念差不多,但是要注意二者的+k,-k的各自含义,tail有一个非常有意思的参数是-f参数,使用该参数可以不停的追加显示一个文件最新插入的内容。

1.追加显示文件内存

 

tail -f /tmp/orzdba_mysql.log 

 

当文件有新的内容插入进来时它会不停的显示新插入的内容

2.

tail -f /tmp/orzdba_mysql.log --pid=8770

当进程8770终止时,tail也结束

总结

 tail -f参数经常会用来监控日志文件的显示,结合实际需要可以充分发挥它的作用

 

 

 



本文转自pursuer.chen(陈敏华)博客园博客,原文链接:http://www.cnblogs.com/chenmh/p/5668069.html,如需转载请自行联系原作者

相关文章
|
23天前
|
Linux系统之whereis命令的基本使用
Linux系统之whereis命令的基本使用
57 23
Linux系统之whereis命令的基本使用
|
10天前
|
Linux中yum、rpm、apt-get、wget的区别,yum、rpm、apt-get常用命令,CentOS、Ubuntu中安装wget
通过本文,我们详细了解了 `yum`、`rpm`、`apt-get`和 `wget`的区别、常用命令以及在CentOS和Ubuntu中安装 `wget`的方法。`yum`和 `apt-get`是高层次的包管理器,分别用于RPM系和Debian系发行版,能够自动解决依赖问题;而 `rpm`是低层次的包管理工具,适合处理单个包;`wget`则是一个功能强大的下载工具,适用于各种下载任务。在实际使用中,根据系统类型和任务需求选择合适的工具,可以大大提高工作效率和系统管理的便利性。
72 25
|
8天前
|
Linux查看内存命令
1. free free命令是最常用的查看内存使用情况的命令。它显示系统的总内存、已使用内存、空闲内存和交换内存的总量。 free -h • -h 选项:以易读的格式(如GB、MB)显示内存大小。 输出示例: total used free shared buff/cache available Mem: 15Gi 4.7Gi 4.1Gi 288Mi 6.6Gi 9.9Gi Swap: 2.0Gi 0B 2.0Gi • to
23 2
深入解析:Linux网络配置工具ifconfig与ip命令的全面对比
虽然 `ifconfig`作为一个经典的网络配置工具,简单易用,但其功能已经不能满足现代网络配置的需求。相比之下,`ip`命令不仅功能全面,而且提供了一致且简洁的语法,适用于各种网络配置场景。因此,在实际使用中,推荐逐步过渡到 `ip`命令,以更好地适应现代网络管理需求。
49 11
|
3月前
|
Linux 10 个“who”命令示例
Linux 10 个“who”命令示例
121 14
Linux 10 个“who”命令示例
|
3月前
|
Linux 各发行版安装 ping 命令指南
如何在不同 Linux 发行版(Ubuntu/Debian、CentOS/RHEL/Fedora、Arch Linux、openSUSE、Alpine Linux)上安装 `ping` 命令,详细列出各发行版的安装步骤和验证方法,帮助系统管理员和网络工程师快速排查网络问题。
269 20
|
3月前
|
linux查看目录下的文件夹命令,find查找某个目录,但是不包括这个目录本身?
通过本文的介绍,您应该对如何在 Linux 系统中查看目录下的文件夹以及使用 `find` 命令查找特定目录内容并排除该目录本身有了清晰的理解。掌握这些命令和技巧,可以大大提高日常文件管理和查找操作的效率。 在实际应用中,灵活使用这些命令和参数,可以帮助您快速定位和管理文件和目录,满足各种复杂的文件系统操作需求。
174 8
|
4月前
|
Linux中第一次使用locate命令报错?????
在Linux CentOS7系统中,使用`locate`命令时出现“command not found”错误,原因是缺少`mlocate`包。解决方法是通过`yum install mlocate -y`或`apt-get install mlocate`安装该包,并执行`updatedb`更新数据库以解决后续的“can not stat”错误。
74 9