head&&tail

//參考《Linux shell脚本攻略 第2版》

1,head

a)打印前10行:

ubuntu@VM-62-13-ubuntu:~$ head file


b)打印前5行:

ubuntu@VM-62-13-ubuntu:~$ head-n 5 file 或者ubuntu@VM-62-13-ubuntu:~$ head-5 file 


c)打印除了最后M行之外的全部行:

ubuntu@VM-62-13-ubuntu:~$ head-n -M file


2,tail

a)打印文件的最后10行:

ubuntu@VM-62-13-ubuntu:~$tail file


b)打印最后5行:

ubuntu@VM-62-13-ubuntu:~$tail-n 5 file 或者ubuntu@VM-62-13-ubuntu:~$tail -5 file 


c)打印除了前M行之外的全部行:

ubuntu@VM-62-13-ubuntu:~$tail -n +(M+1) file

d) tail -f 能够密切关注文件里新添加的内容,并随着数据的添加持续保持更新,当中能够加上一个睡眠间隔 -s。这样我们能够设置监视文件的时间间隔(假设进行Fun一直向监控的文件追加数据,则tail -f 就会一直运行知道进程Fun结束):

pgrep Fun | xargs tail -f  file -s 20 --pid

pidof Fun | xargs tail -f file -s 20 --pid

原文地址:https://www.cnblogs.com/wzzkaifa/p/7256977.html