Linux head/tail命令详解

Posted ftl1012

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux head/tail命令详解相关的知识,希望对你有一定的参考价值。

head命令用于显示文件的开头的内容。在默认情况下,head命令显示文件的头10行内容。

tail命令用于显示文件的结尾的内容。在默认情况下,taild命令显示文件的后10行内容。

head常见命令参数

  -c, --bytes=[-]K         print the first K bytes of each file;
                             with the leading `-‘, print all but the last
                             K bytes of each file
  -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
  -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

tail场景命令

 -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

常用的命令展示

head :显示文件的前几行,默认10行  
head -n 2 /home/omc/h.txt
	==>head -n 3 == head -3    可以直接跟行数
cat h.txt | grep -v "hello"     过滤掉特定字符串,效率低,因为有管道
	==>grep -v "hello" h.txt  可以直接跟文件名,效率快
tail: 显示文件最后几行,默认10行
tail -10 /home/omc/h.txt    显示最后10行
tail -f /home/omc/h.txt     实时跟踪文件, 如果文件不存在,则终止 
tail -F /home/omc/h.txt     如果文件不存在,会继续尝试
head -30 /home/omc/h.txt | tail -11 /home/omc/h.txt   输出一个文件的20-30行显示文件的前n行
head -n 5 log2014.log输出文件除了最后n行的全部内容
head -n -6 log2014.log从第5行开始显示文件
tail -n +5 log2014.log    【显示文件的全部内容】
tail -n 5  log2014.log    【只显示最后的5行】  根more/less差不多

以上是关于Linux head/tail命令详解的主要内容,如果未能解决你的问题,请参考以下文章

初学linux必备命令集!!详解

Linux文件管理命令详解及练习

03-01 linux文件管理命令详解

3-1 Linux文件管理类命令详解

linux中cat more less head tail 命令区别

大数据之Linux:常用命令之cat和head,tail命令结合