基础命令学习目录首页html
原文连接:https://www.cnblogs.com/amosli/p/3496027.htmllinux
当要查看上千行的大文件时,咱们可不会用cat命令把整个文件内容给打印出来,相反,咱们可能只须要看文件的一小部分地内容(例如文件的前十行和后十行),咱们也有可能须要打印出来前n行或后n行,也有可能打印除了前n行或后n行以外的全部行,也有可能须要实时监控log日志的更新,那么怎么实现呢?下面一块儿来看一下linux下使用率极高的head ,tail两个命令。vim
首先,输入head --help查看帮助信息:app
amosli@amosli-pc:~/learn/fd$ head --help Usage: head [OPTION]... [FILE]... 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=[-]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 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.
head [OPTION]... [FILE]...
接下来将在实例中讲解各参数含义及用法:post
新建test.txt,共14行.学习
amosli@amosli-pc:~/learn/fd$ cat -n test.txt 1 a 2 b 3 c 4 d 5 e 6 f 7 g 8 h 9 i 10 j 11 k 12 l 13 m 14 n
使用head命令查看前十行,head命令默认显示文件前十行ui
amosli@amosli-pc:~/learn/fd$ head test.txt a b c d e f g h i j
amosli@amosli-pc:~/learn/fd$ head -n 3 test.txt a b c
英文提示信息:this
-n, --lines=[-]K print the first K lines instead of the first 10;
amosli@amosli-pc:~/learn/fd$ head -n -3 test.txt a b c d e f g h i j k
英文提示信息:url
-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
加上'-',打印全部内容除了最后的K行。spa
amosli@amosli-pc:~/learn/fd$ head -c 2 test.txt a
2个字节就是一个“a”字母。
英文提示信息:
-c, --bytes=[-]K print the first K bytes of each file;
amosli@amosli-pc:~/learn/fd$ head -c -2 test.txt a b c d e f g h i j k l m
英文提示信息:
-c, --bytes=[-]K print the first K bytes of each file; with the leading `-', print all but the last K bytes of each file
amosli@amosli-pc:~/learn/fd$ head -q test.txt a b c d e f g h i j
英文提示信息:
-q, --quiet, --silent never print headers giving file names
其实后面跟上--quiet,--silent都是同样的,都不会显示文件名称,和默认打印是同样的效果。
amosli@amosli-pc:~/learn/fd$ head -v test.txt ==> test.txt <== a b c d e f g h i j
英文提示信息:
-v, --verbose always print headers giving file names
其中,用--verbose和-v显示的是同样的效果
amosli@amosli-pc:~/learn/fd$ head --verbose test.txt ==> test.txt <== a b c d e f g h i j
amosli@amosli-pc:~/learn/fd$ head -n 3 test.txt test2.txt ==> test.txt <== a b c ==> test2.txt <== c d e
2、tail命令详解
tail命令和head 命令很是类似,只不过它是打印文件的尾部内容的,固然也有一些特点之处,下面一块儿来看看吧。
首先,输入tail --help看一下提示信息
amosli@amosli-pc:~/learn/fd$ tail --help Usage: tail [OPTION]... [FILE]... 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=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 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. 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 descriptor (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.
tail [OPTION]... [FILE]...
这里因为head和tail实在比较像,这里为了节省篇幅,对类似之处将尽可能简洁
test2.txt,共有12行内容为从c-n
amosli@amosli-pc:~/learn/fd$ cat -n test2.txt 1 c 2 d 3 e 4 f 5 g 6 h 7 i 8 j 9 k 10 l 11 m 12 n
-c, --bytes=K output the last K bytes; alternatively, use -c +K to output bytes starting with the Kth of each file
打印test2.txt中的最后4 bytes,以下:
amosli@amosli-pc:~/learn/fd$ tail -c 4 test2.txt m n
tail -c +4 test2.txt 加上一个‘+’会是什么效果呢?
amosli@amosli-pc:~/learn/fd$ tail -c +4 test2.txt e f g h i j k l m n
少打印了c d 两个字母,那么 -c +K的意思也就很明了了,即打印文件的全部内容除了前面的K个字节
看一下提示信息:
-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
打印test2.txt最后的3行内容:
amosli@amosli-pc:~/learn/fd$ tail -n 3 test2.txt l m n
从第3行开始输出test2.txt的全部内容:
amosli@amosli-pc:~/learn/fd$ tail -n +3 test2.txt e f g h i j k l m n
3.-q参数,-v参数
-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
不打印文件名称信息:
amosli@amosli-pc:~/learn/fd$ tail -q test2.txt e f g h i j k l m n
打印文件名称信息:
amosli@amosli-pc:~/learn/fd$ tail -v test2.txt ==> test2.txt <== e f g h i j k l m n
tail 命令的一个很重要的用法是从一个内容不断增长的文件中读取数据。新增长的内容部民被添加到文件的尾部,所以当新内容被写入文件的时候,能够用tail将其显示出来。只是简单的使用tail的话,它会读取文件的最后10行,而后退出,这样就不能作到实时监控,加入-f参数就能够作到实时监控文件的更新内容了。
amosli@amosli-pc:~/learn/fd$ tail -f test2.txt g h i j k l m n o p
ctrl+alt+t新开一个终端,而后执行下面的命令:
amosli@amosli-pc:~/learn/fd$ echo 'xyz' >> test2.txt
这个时候你就能够看到前一个终端在里出现了‘xyz’
amosli@amosli-pc:~/learn/fd$ tail -f test2.txt g h i j k l m n o p xyz
这样就能实时监控项目里的log日志了。
若是想设定一个间隔时间去查看文件的更新应该怎么作呢?请看-s参数
-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.
如每隔5秒查看一次test2.txt的内容是否更新
amosli@amosli-pc:~/learn/fd$ tail -f -s 5 test2.txt j k l m n o p xyz
默认是1秒更新一次。
6.--pid参数
tail 具备一个很意思的特性,当某个给定进程结束以后,tail也会随之终结.
假如咱们正在读取一个不断增加的文件,进程Foo一直在向该文件追加数据,那么tail就会一直执行,直到进程Foo的结束.
$PID=$(pidof Foo) $tail -f file --pid $PID #当进程Foo结束以后,tail也会跟着结束
例如用gedit打开test2.txt,不断加入数据,而后在终端里使用tail进行监听,具体为:
amosli@amosli-pc:~/learn/fd$ PID=$(pidof gedit) amosli@amosli-pc:~/learn/fd$ tail -f -s 2 test2.txt --pid $PID h i j k l m n o p xyz
而后不断在gedit中追加入‘yyy’后保存,按理说终端里应该会更新,但个人终端不知为什么没有更新数据,这里就不帖出来了。
关闭gedit后,tail监控也关闭掉了。
head和tail取文件第m行到第n行