head:查看文件的内容,默认显示的是前10行内容。
tail:查看文件的内容,默认显示的是后10行内容。
head和tail如果想显示指定行数的内容,可以用-n选项。
选项说明: head -n number:显示前number行的内容。 head -c number-bytes:显示前面多少个字节的内容。可以使用K等单位。如head -c 5k显示前5K的内容。 head -n -number:显示除了后面的number行之外的所有行。
tail -n number:显示后面number行的内容。 tail -c number-bytes:显示后面多少个字节的内容。 tail -n +number:从number行以后开始显示所有行的内容。 tail -f filename:持续监视一个文件的内容变化,常用于查看日志文件。 tail -F filename:作用和tail -f一样,但是当一个监视的文件被移走时,tail -F会重试。
示例:
[[email protected] ~]# cat -n /etc/services | head 1 # /etc/services: 2 # $Id: services,v 1.48 2009/11/11 14:32:31 ovasik Exp $ 3 # 4 # Network services, Internet style 5 # IANA services version: last updated 2009-11-10 6 # 7 # Note that it is presently the policy of IANA to assign a single well-known 8 # port number for both TCP and UDP; hence, most entries here have two entries 9 # even if the protocol doesn‘t support UDP operations. 10 # Updated from RFC 1700, ``Assigned Numbers‘‘ (October 1994). Not all ports [[email protected] ~]#
[[email protected] ~]# cat -n /etc/services | tail 10765 nimgtw 48003/udp # Nimbus Gateway 10766 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol 10767 isnetserv 48128/tcp # Image Systems Network Services 10768 isnetserv 48128/udp # Image Systems Network Services 10769 blp5 48129/tcp # Bloomberg locator 10770 blp5 48129/udp # Bloomberg locator 10771 com-bardac-dw 48556/tcp # com-bardac-dw 10772 com-bardac-dw 48556/udp # com-bardac-dw 10773 iqobject 48619/tcp # iqobject 10774 iqobject 48619/udp # iqobject [[email protected] ~]#
head -n -number和tail -n +number的用法: [[email protected] ~]# cat -n /etc/issue | head -n -2 1 CentOS release 6.5 (Final) [[email protected] ~]# [[email protected] ~]# cat -n /etc/issue | tail -n +2 2 Kernel \r on an \m 3 [[email protected] ~]#
时间: 2024-10-24 13:51:25