linux 命令详解

本文主要内容来自Linux man 手册

命令名称:

head 输出文件的开头部分
tai  输出文件的结尾部分

命令用法:

head/tail [选项]... [文件]...            []表示可选参数

命令概述:

将每个文件的前10行(或后10行)打印到标准输出,如果文件多于一个,则显示每个文件的文件名。
如果没有加文件参数,或者文件参数为”-“,则从标准输入读取内容。
带参数的长选项对应的短选项同样需要带参数(-c和-n需要带参数)。

命令参数:

head和tail共有的选项参数:

-c,--bytes=[-/+]NUM  head为-,tail为+打印文件的前(后)NUM个字节,如果head命令的NUM前面加了'-',打印文件中除了后NUM字节外的全部内容,如果tail命令的NUM前面加了‘+’,打印文件中除了前NUM字节外的全部内容。-n,--lines=[-/+]NUM head为-,tail为+打印前(后)NUM行的内容,而不是前(后)10行,如果head命令的NUM前面加了'-',打印文件中除了后NUM行外的全部内容,如果tail命令的NUM前面加了‘+’,打印文件中除了前NUM行外的全部内容。NUM可以有一个乘法后缀:b 512,kB 1000,K 1024,MB 1000*1000,M 1024*1024,GB 1000*1000*1000,G1024*1024*1024,T、P、E、Z、Y也类似。-q,--quiet,--slient不打印文件的名字(多个文件时)-v,--verbose总是打印文件的名字-z,--zero-terminated行的分隔符为NUL,而不是回车--help 显示帮助信息--version 显示版本信息

tail独有的选项参数:

-f,--follow=[name|descriptor]随着文件的增长,追加数据到标准输出,使用--follow(-f),tail默认跟随文件描述符,这意味着即使重命名了文件,tail也将继续跟踪其结尾。如果确实希望跟踪文件的实际名称,而不是文件描述符(例如,旋转日志),则不需要此默认行为。在这种情况下使用--follow=name。这使得tail以一种适应重命名、删除和创建的方式跟踪命名文件。--max-unchanged-stats=N和--follow=name一起使用,重新打开尚未打开的文件,在N次(默认为5次)迭代后更改大小,以查看是否已取消链接或重命名(这是旋转日志文件的常见情况)。对于inotify,此选项作用很小。--pid=PID和-f一起使用,当PID死亡时终止tail--retry如果文件无法访问,继续尝试打开-s,--sleep-interval=N和-f一起使用时,在迭代之间睡眠大约N秒(默认值为1.0);和inotify和--pid=P使用时,至少每N秒检查一次进程P

示例:

1. head/tail [文件]…

不带任何选项,打印文件的前(后)10行

xiaohui@ubuntu:~/work/head_tail_learn$ cat test.c
1aaaaaaaaaaaaaa
2bbbbbbbbbbbbbb
3aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
bbbbbbbbbbbbbb
bbbbbbbbbbbbbb
bbbbbbbbbbbbbb
10bbbbbbbbbbbbbb
11jjijijobb
12abababababab
xiaohui@ubuntu:~/work/head_tail_learn$ head test.c
1aaaaaaaaaaaaaa
2bbbbbbbbbbbbbb
3aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
bbbbbbbbbbbbbb
bbbbbbbbbbbbbb
bbbbbbbbbbbbbb
10bbbbbbbbbbbbbb
xiaohui@ubuntu:~/work/head_tail_learn$ tail test.c
3aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
bbbbbbbbbbbbbb
bbbbbbbbbbbbbb
bbbbbbbbbbbbbb
10bbbbbbbbbbbbbb
11jjijijobb
12abababababab
xiaohui@ubuntu:~/work/head_tail_learn$ 

2. head/tail -c [-/+]NUM] [文件]…

打印前(后)NUM个字节。

xiaohui@ubuntu:~/work/head_tail_learn$ cat test_2.c
abcdefg
1234567
ABCDEFG
87654321
xiaohui@ubuntu:~/work/head_tail_learn$ head -c 3 test_2.c
abcxiaohui@ubuntu:~/work/head_tail_learn$ tail -c 3 test_2.c
21
xiaohui@ubuntu:~/work/head_tail_learn$ tail -c 5 test_2.c
4321
xiaohui@ubuntu:~/work/head_tail_learn$ 

head -NUM:打印除去后NUM字节外的全部内容。

xiaohui@ubuntu:~/work/head_tail_learn$ cat test_2.c
abcdefg
1234567
ABCDEFG
87654321
xiaohui@ubuntu:~/work/head_tail_learn$ head -c -5 test_2.c
abcdefg
1234567
ABCDEFG
8765xiaohui@ubuntu:~/work/head_tail_learn$ 

tail +NUM:打印除去前NUM字节外的全部内容。

xiaohui@ubuntu:~/work/head_tail_learn$ cat test_2.c
abcdefg
1234567
ABCDEFG
87654321
xiaohui@ubuntu:~/work/head_tail_learn$ tail -c +5 test_2.c
efg
1234567
ABCDEFG
87654321
xiaohui@ubuntu:~/work/head_tail_learn$

3. head/tail -n [-/+]NUM] [文件]…

打印前(后)NUM行。

xiaohui@ubuntu:~/work/head_tail_learn$ cat test_3.c
11111111
22222222
33333333
44444444
xiaohui@ubuntu:~/work/head_tail_learn$ head -n 2 test_3.c
11111111
22222222
xiaohui@ubuntu:~/work/head_tail_learn$ tail -n 3 test_3.c
22222222
33333333
44444444
xiaohui@ubuntu:~/work/head_tail_learn$ 

head -NUM:打印除去后NUM行外的全部内容。

xiaohui@ubuntu:~/work/head_tail_learn$ cat test_3.c
11111111
22222222
33333333
44444444
xiaohui@ubuntu:~/work/head_tail_learn$ head -n -2 test_3.c
11111111
22222222
xiaohui@ubuntu:~/work/head_tail_learn$ 

tail +NUM:打印除去前NUM行外的全部内容。(实测多显示一行)

xiaohui@ubuntu:~/work/head_tail_learn$ cat test_3.c
11111111
22222222
33333333
44444444
xiaohui@ubuntu:~/work/head_tail_learn$ tail -n +3 test_3.c
33333333
44444444
xiaohui@ubuntu:~/work/head_tail_learn$ tail -n +2 test_3.c
22222222
33333333
44444444

4. head/tail -q [文件]…

总是不显示文件名

xiaohui@ubuntu:~/work/head_tail_learn$ head test_3.c test_2.c
==> test_3.c <==
11111111
22222222
33333333
44444444==> test_2.c <==
abcdefg
1234567
ABCDEFG
87654321
xiaohui@ubuntu:~/work/head_tail_learn$ head test_3.c test_2.c -q
11111111
22222222
33333333
44444444
abcdefg
1234567
ABCDEFG
87654321
xiaohui@ubuntu:~/work/head_tail_learn$ 

5. head/tail -v [文件]…

总是显示文件名

xiaohui@ubuntu:~/work/head_tail_learn$ tail test_3.c
11111111
22222222
33333333
44444444
xiaohui@ubuntu:~/work/head_tail_learn$ tail test_3.c -v
==> test_3.c <==
11111111
22222222
33333333
44444444
xiaohui@ubuntu:~/work/head_tail_learn$ 

6. head/tail -z [文件]…

将行的分隔符视为NUL,而不是回车

由于文件尾才为空(NUL),所以一个文件被当成只有一行,使用-n -1除去第一行时,head命令无输出。

xiaohui@ubuntu:~/work/head_tail_learn$ head  test_3.c -z
11111111
22222222
33333333
44444444
xiaohui@ubuntu:~/work/head_tail_learn$ head  test_3.c -z -n -1
xiaohui@ubuntu:~/work/head_tail_learn$ 

7. tail–follow=[name/descriptor| 文件…

“跟踪”文件,同-f,但-f貌似不能加参数。

使用默认的参数descriptor,tail会根据文件描述符,进行跟踪,所以在使用时不能修改文件的描述符(如进行文件关闭操作),echo命令可以在命令行不修改进程中文件的描述符而修改文件内容。

xiaohui@ubuntu:~/work/head_tail_learn$ tail -f test_3.c &
[1] 16779
xiaohui@ubuntu:~/work/head_tail_learn$ 11111111
22222222
33333333
44444444xiaohui@ubuntu:~/work/head_tail_learn$ echo 555 >> test_3.c
xiaohui@ubuntu:~/work/head_tail_learn$ 555xiaohui@ubuntu:~/work/head_tail_learn$ echo 666 >> test_3.c
666
xiaohui@ubuntu:~/work/head_tail_learn$ echo 777 >> test_3.c
777
xiaohui@ubuntu:~/work/head_tail_learn$ fg
tail -f test_3.c
^C
xiaohui@ubuntu:~/work/head_tail_learn$ 

–follow=name是根据文件的名字“跟踪”文件,就算将文件进行截断(覆盖所有内容)或替换,tail都会继续follow。

xiaohui@ubuntu:~/work/head_tail_learn$ tail --follow=name test_3.c &
[1] 16861
xiaohui@ubuntu:~/work/head_tail_learn$ 11111111
22222222
33333333
44444444xiaohui@ubuntu:~/work/head_tail_learn$ echo 5555 >> test_3.c
5555
xiaohui@ubuntu:~/work/head_tail_learn$ echo 1234567 > test_3.c
tail: test_3.c:文件已截断
1234567
xiaohui@ubuntu:~/work/head_tail_learn$ echo 22222222 >> test_3.c
22222222
xiaohui@ubuntu:~/work/head_tail_learn$ echo "new file" > test_4.c
xiaohui@ubuntu:~/work/head_tail_learn$ mv test_4.c test_3.c
tail: 'test_3.c' has been replaced;  following new file
new file
xiaohui@ubuntu:~/work/head_tail_learn$ echo "follow name" >> test_3.c
follow name
xiaohui@ubuntu:~/work/head_tail_learn$ 

8. tail --pid=PID --follow[descriptor/name] 文件…

由PID进程来控制tail的结束,只要PID结束,tail就停止跟踪文件。

xiaohui@ubuntu:~/work/head_tail_learn$ ../test.out &
[1] 17268
xiaohui@ubuntu:~/work/head_tail_learn$ helle worldxiaohui@ubuntu:~/work/head_tail_learn$ tail --follow=name test_3.c --pid=17268 &
xiaohui@ubuntu:~/work/head_tail_learn$ 111111
222222xiaohui@ubuntu:~/work/head_tail_learn$ echo 333333 >> test_3.c
333333
xiaohui@ubuntu:~/work/head_tail_learn$ kill 17268
xiaohui@ubuntu:~/work/head_tail_learn$ psPID TTY          TIME CMD17206 pts/9    00:00:00 bash17327 pts/9    00:00:00 ps
[1]-  已终止               ../test.out
[2]+  已完成               tail --follow=name test_3.c --pid=17268
xiaohui@ubuntu:~/work/head_tail_learn$ 

9. tail --retry --follow=name 文件…

–retry可以在文件无法访问时,不让tail自己退出,等到检测到文件存在时,继续跟踪文件。

xiaohui@ubuntu:~/work/head_tail_learn$ tail --follow=name test_3.c --retry&
[1] 17379
xiaohui@ubuntu:~/work/head_tail_learn$ tail: 无法打开'test_3.c' 读取数据: 没有那个文件或目录xiaohui@ubuntu:~/work/head_tail_learn$ touch test_3.c
xiaohui@ubuntu:~/work/head_tail_learn$ tail: 'test_3.c' has appeared;  following new filexiaohui@ubuntu:~/work/head_tail_learn$ echo 123456 > test_3.c
xiaohui@ubuntu:~/work/head_tail_learn$ 123456
xiaohui@ubuntu:~/work/head_tail_learn$ echo 111111 > test_3.c
xiaohui@ubuntu:~/work/head_tail_learn$ rm test_3.c
xiaohui@ubuntu:~/work/head_tail_learn$ tail: 'test_3.c' 已不可访问: 没有那个文件或目录xiaohui@ubuntu:~/work/head_tail_learn$ psPID TTY          TIME CMD17206 pts/9    00:00:00 bash17379 pts/9    00:00:00 tail17430 pts/9    00:00:00 ps
xiaohui@ubuntu:~/work/head_tail_learn$ echo "new test_3" > test_3.c
xiaohui@ubuntu:~/work/head_tail_learn$ tail: 'test_3.c' has appeared;  following new file
new test_3xiaohui@ubuntu:~/work/head_tail_learn$ 

man手册

以下为 head命令手册原文:

HEAD(1)                          User Commands                         HEAD(1)NAMEhead - output the first part of filesSYNOPSIShead [OPTION]... [FILE]...DESCRIPTIONPrint  the  first  10 lines of each FILE to standard output.  With morethan 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  optionstoo.-c, --bytes=[-]NUMprint  the  first  NUM bytes of each file; with the leading '-',print all but the last NUM bytes of each file-n, --lines=[-]NUMprint the first NUM lines instead of  the  first  10;  with  theleading '-', print all but the last NUM lines of each file-q, --quiet, --silentnever print headers giving file names-v, --verbosealways print headers giving file names-z, --zero-terminatedline delimiter is NUL, not newline--help display this help and exit--versionoutput version information and exitNUM 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.AUTHORWritten by David MacKenzie and Jim Meyering.REPORTING BUGSGNU coreutils online help: <http://www.gnu.org/software/coreutils/>Report head translation bugs to <http://translationproject.org/team/>COPYRIGHTCopyright  ©  2016  Free Software Foundation, Inc.  License GPLv3+: GNUGPL 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.SEE ALSOtail(1)Full documentation at: <http://www.gnu.org/software/coreutils/head>or available locally via: info '(coreutils) head invocation'GNU coreutils 8.25               February 2017                         HEAD(1)

以下为 tail 命令手册原文:

TAIL(1)                          User Commands                         TAIL(1)NAMEtail - output the last part of filesSYNOPSIStail [OPTION]... [FILE]...DESCRIPTIONPrint  the  last  10  lines of each FILE to standard output.  With morethan 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  optionstoo.-c, --bytes=[+]NUMoutput  the  last  NUM  bytes; or use -c +NUM to output startingwith byte NUM of each file-f, --follow[={name|descriptor}]output appended data as the file grows;an absent option argument means 'descriptor'-F     same as --follow=name --retry-n, --lines=[+]NUMoutput the last NUM lines, instead of the last  10;  or  use  -n+NUM to output starting with line NUM--max-unchanged-stats=Nwith --follow=name, reopen a FILE which has notchanged  size  after  N  (default 5) iterations to see if it hasbeen unlinked or renamed (this is the usual case of rotated  logfiles); with inotify, this option is rarely useful--pid=PIDwith -f, terminate after process ID, PID dies-q, --quiet, --silentnever output headers giving file names--retrykeep trying to open a file if it is inaccessible-s, --sleep-interval=Nwith -f, sleep for approximately N seconds (default 1.0) betweeniterations; with inotify and --pid=P, check process P  at  leastonce every N seconds-v, --verbosealways output headers giving file names-z, --zero-terminatedline delimiter is NUL, not newline--help display this help and exit--versionoutput version information and exitNUM 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  continueto  track  its  end.   This  default behavior is not desirable when youreally want to track the actual name of the file, not the file descrip‐tor (e.g., log rotation).  Use --follow=name in that case.  That causestail to track the named file  in  a  way  that  accommodates  renaming,removal and creation.AUTHORWritten  by Paul Rubin, David MacKenzie, Ian Lance Taylor, and Jim Mey‐ering.REPORTING BUGSGNU coreutils online help: <http://www.gnu.org/software/coreutils/>Report tail translation bugs to <http://translationproject.org/team/>COPYRIGHTCopyright © 2016 Free Software Foundation, Inc.   License  GPLv3+:  GNUGPL 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.SEE ALSOhead(1)Full documentation at: <http://www.gnu.org/software/coreutils/tail>or available locally via: info '(coreutils) tail invocation'GNU coreutils 8.25               February 2017                         TAIL(1)

Linux命令详解之 head和tail相关推荐

  1. c linux time微秒_学习linux,看这篇1.5w多字的linux命令详解(6小时讲明白Linux)

    用心分享,共同成长 没有什么比每天进步一点点更重要了 本篇文章主要讲解了一些linux常用命令,主要讲解模式是,命令介绍.命令参数格式.命令参数.命令常用参数示例.由于linux命令较多,我还特意选了 ...

  2. 《Linux命令详解手册》——Linux畅销书作家又一力作

    关注IT,更要关心IT人,让系统管理员以及程序员工作得更加轻松和快乐.鉴于此, 图灵公司引进了国外知名出版社John Wiley and Sons出版的Fedora Linux Toolbox: 10 ...

  3. linux下载命令 scp,linux命令详解之scp命令

    作用 scp命令常用于linux之间复制文件和目录. scp是secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令. 格式 从本地复制到远程 复制文件 sc ...

  4. linux中date使用方法,linux命令详解date使用方法(计算母亲节和父亲节日期脚本示例)...

    linux命令详解date使用方法(计算母亲节和父亲节日期脚本示例) 发布于 2016-02-07 15:58:40 | 108 次阅读 | 评论: 0 | 来源: 网友投递 LinuxLinux是一 ...

  5. RAR for Linux 命令详解

    RAR for Linux 命令详解 用法:  rar <命令>-<开关 1> -<开关 N> <压缩文件> <文件...> <@列表 ...

  6. Linux命令详解之 ls

    linux 命令详解 本文主要内容来自Linux man 手册 命令名称: ls ( list files / list directory contents )列举目录内容 命令用法: ls [选项 ...

  7. Linux命令详解之 mv

    linux 命令详解 本文主要内容来自Linux man 手册 命令名称: mv(move)移动/重命名文件 命令用法: mv [选项]... [-T] 源文件 目标文件 mv [选项]... 源文件 ...

  8. Linux命令详解之w命令

    Linux命令详解之w命令 1.命令详解 ··· NAMEw - Show who is logged on and what they are doing. w命令就是用来展示谁在登录,以及他们在做 ...

  9. Linux命令详解:md5sum 命令

    Linux命令详解:md5sum 命令 一.md5 算法介绍 二.md5sum 命令使用说明 三.md5sum 命令帮助 四.md5sum 命令选项.参数 语法 选项 参数 五.md5sum 命令实战 ...

最新文章

  1. Golang 微框架 Gin 简介
  2. 写给那些正奔三的80后[转]
  3. 双级减速器优化matlab,基于MATLAB的双级齿轮减速器优化设计
  4. Pytorch教程(十五):element-wise、Broadcasting
  5. C# 网络编程之webBrowser获取网页url和下载网页中图片
  6. B1007 素数对猜想
  7. 源码包安装mysql5.7.25_centos源码安装mysql5.7.25-boost
  8. RTT学习笔记2-线程
  9. 机器学习九大挑战(转载)
  10. Linux Shell脚本 删除一个字符串中的部分字符
  11. 点对点信道互连以太网实验_轩辕实验室┃SOTIF:汽车以太网容错能力测试(1)...
  12. ecg 幅度_精确心电图(ECG)信号处理方案
  13. 汇编语言中sbb是什么意思_汇编语言的所有指令
  14. 视频教程-js+ajax+jquery+easyui从入门到精通(项目实战)-JavaScript
  15. PSFTP工具的使用教程
  16. 创意视觉应用︱基于深度学习的CVaaS计算机视觉即服务案例(Computer Vision as a Service)
  17. 【Visual C++】游戏开发笔记三十五 站在巨人的肩膀上 游戏引擎导论
  18. 模式识别与机器学习第六章有监督学习方法
  19. 第二章 2.群中的等价关系 -- 陪集,共轭,正规子群与商群
  20. win10系统禁用笔记本自带键盘的方法

热门文章

  1. network 东西流量和南北流量分别是什么
  2. 【LeetCode击败99%+】二叉树的最大深度
  3. Zjh游戏(十八)可以下注的处理
  4. 买了房,还未过户就被法院查封,房屋应该归谁
  5. 金九银十正确打开方式!请谈下Android消息机制,赶紧收藏!
  6. arch系列安装腾讯会议linux版本
  7. ubuntu 在命令行中显示当前路径
  8. 【Cpython的GIL详细了解一下?】
  9. 项目经理年终总结报告模板
  10. do-exercise-淘宝网店