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
44444444
xiaohui@ubuntu:~/work/head_tail_learn$ echo 555 >> test_3.c
xiaohui@ubuntu:~/work/head_tail_learn$ 555
xiaohui@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
44444444
xiaohui@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 world
xiaohui@ubuntu:~/work/head_tail_learn$ tail --follow=name test_3.c --pid=17268 &
xiaohui@ubuntu:~/work/head_tail_learn$ 111111
222222
xiaohui@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$ ps
PID TTY TIME CMD
17206 pts/9 00:00:00 bash
17327 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 file
xiaohui@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$ ps
PID TTY TIME CMD
17206 pts/9 00:00:00 bash
17379 pts/9 00:00:00 tail
17430 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_3
xiaohui@ubuntu:~/work/head_tail_learn$
空行
man手册
以下为 head命令手册原文:
HEAD(1) User Commands HEAD(1)
NAME
head - output the first part of files
SYNOPSIS
head [OPTION]... [FILE]...
DESCRIPTION
Print the first 10 lines of each FILE to standard output. With more
than 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 options
too.
-c, --bytes=[-]NUM
print the first NUM bytes of each file; with the leading '-',
print all but the last NUM bytes of each file
-n, --lines=[-]NUM
print the first NUM lines instead of the first 10; with the
leading '-', print all but the last NUM lines of each file
-q, --quiet, --silent
never print headers giving file names
-v, --verbose
always print headers giving file names
-z, --zero-terminated
line delimiter is NUL, not newline
--help display this help and exit
--version
output version information and exit
NUM 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.
AUTHOR
Written by David MacKenzie and Jim Meyering.
REPORTING BUGS
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Report head translation bugs to <http://translationproject.org/team/>
COPYRIGHT
Copyright ? 2016 Free Software Foundation, Inc. License GPLv3+: GNU
GPL 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 ALSO
tail(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)
NAME
tail - output the last part of files
SYNOPSIS
tail [OPTION]... [FILE]...
DESCRIPTION
Print the last 10 lines of each FILE to standard output. With more
than 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 options
too.
-c, --bytes=[+]NUM
output the last NUM bytes; or use -c +NUM to output starting
with 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=[+]NUM
output the last NUM lines, instead of the last 10; or use -n
+NUM to output starting with line NUM
--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 if it is inaccessible
-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
-z, --zero-terminated
line delimiter is NUL, not newline
--help display this help and exit
--version
output version information and exit
NUM 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 continue
to track its end. This default behavior is not desirable when you
really 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 causes
tail to track the named file in a way that accommodates renaming,
removal and creation.
AUTHOR
Written by Paul Rubin, David MacKenzie, Ian Lance Taylor, and Jim Mey‐
ering.
REPORTING BUGS
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Report tail translation bugs to <http://translationproject.org/team/>
COPYRIGHT
Copyright ? 2016 Free Software Foundation, Inc. License GPLv3+: GNU
GPL 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 ALSO
head(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)
|