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/omd/h.txt
	==>head -n 3 == head -3    可以直接跟行数
cat h.txt | grep -v "hello"     过滤掉特定字符串,效率低,因为有管道
	==>grep -v "hello" h.txt  可以直接跟文件名,效率快
tail: 显示文件最后几行,默认10行
tail -10 /home/omd/h.txt    显示最后10行
tail -f /home/omd/h.txt     实时跟踪文件, 如果文件不存在,则终止 
tail -F /home/omd/h.txt     如果文件不存在,会继续尝试
head -30 /home/omd/h.txt | tail -11 /home/omd/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差不多
posted @ 2018-07-02 21:56  小a玖拾柒  阅读(5007)  评论(0编辑  收藏  举报