head和tail都是以行为单位来读取数据,head为选读取文件的前几行,tail为读取文件的后几行。
指令语法为:
head [-n number] number为你想读取的行数,-n 表示显示前几行。不添加-n参数则默认显示前10行。N为正数时表示显示文件的前n行,当n为负数时,表示显示第n行以前的所有行。
[root@localhost ~]# head install.log Installing libgcc-4.4.7-18.el6.x86_64 warning: libgcc-4.4.7-18.el6.x86_64: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY Installing fontpackages-filesystem-1.41-1.1.el6.noarch Installing liberation-fonts-common-1.05.1.20090721-5.el6.noarch Installing xml-common-0.6.3-33.el6.noarch Installing iso-codes-3.16-2.el6.noarch Installing setup-2.8.14-23.el6.noarch Installing filesystem-2.4.30-3.el6.x86_64 Installing dejavu-fonts-common-2.33-1.el6.noarch Installing xkeyboard-config-2.16-1.el6.noarch [root@localhost ~]# head -n 5 install.log Installing libgcc-4.4.7-18.el6.x86_64 warning: libgcc-4.4.7-18.el6.x86_64: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY Installing fontpackages-filesystem-1.41-1.1.el6.noarch Installing liberation-fonts-common-1.05.1.20090721-5.el6.noarch Installing xml-common-0.6.3-33.el6.noarch [root@localhost ~]# head -n -930 install.log Installing libgcc-4.4.7-18.el6.x86_64 warning: libgcc-4.4.7-18.el6.x86_64: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY Installing fontpackages-filesystem-1.41-1.1.el6.noarch Installing liberation-fonts-common-1.05.1.20090721-5.el6.noarch Installing xml-common-0.6.3-33.el6.noarch
当给出的负值大于文件总行数则不会返回信息。
[root@localhost ~]# head -n -990 install.log [root@localhost ~]#
tail [-n number] number为你想读取的行数,-n表示显示后几行。不填加-n参数则默认显示后10行。N不加符号时表示显示文件的后n行,但n前加上+符号则表示显示第n-1行以后的所有行。
[root@localhost ~]# tail install.log Installing iwl4965-firmware-228.61.2.24-2.1.el6.noarch Installing rt61pci-firmware-1.2-7.el6.noarch Installing iwl3945-firmware-15.32.2.9-4.el6.noarch Installing ql2200-firmware-2.02.08-3.1.el6.noarch Installing rt73usb-firmware-1.8-7.el6.noarch Installing ipw2100-firmware-1.3-11.el6.noarch Installing ql23xx-firmware-3.03.27-3.1.el6.noarch Installing ipw2200-firmware-3.1-4.el6.noarch Installing rootfiles-8.1-6.1.el6.noarch *** FINISHED INSTALLING PACKAGES ***[root@localhost ~]# [root@localhost ~]# tail -n 5 install.log Installing ipw2100-firmware-1.3-11.el6.noarch Installing ql23xx-firmware-3.03.27-3.1.el6.noarch Installing ipw2200-firmware-3.1-4.el6.noarch Installing rootfiles-8.1-6.1.el6.noarch *** FINISHED INSTALLING PACKAGES ***[root@localhost ~]# [root@localhost ~]# tail -n +930 install.log Installing rt73usb-firmware-1.8-7.el6.noarch Installing ipw2100-firmware-1.3-11.el6.noarch Installing ql23xx-firmware-3.03.27-3.1.el6.noarch Installing ipw2200-firmware-3.1-4.el6.noarch Installing rootfiles-8.1-6.1.el6.noarch *** FINISHED INSTALLING PACKAGES ***[root@localhost ~]#
tail -f的表示实时的显示出文件内容,因为文件内容可能随时会被修改,添加-f参数,他会一直检测文件有没有数据写入到这个文件,可以让文件数据在写入时就立刻显示出来。直到按下ctrl+c才会退出tail的检测。
[root@localhost ~]# tail -f install.log Installing iwl4965-firmware-228.61.2.24-2.1.el6.noarch Installing rt61pci-firmware-1.2-7.el6.noarch Installing iwl3945-firmware-15.32.2.9-4.el6.noarch Installing ql2200-firmware-2.02.08-3.1.el6.noarch Installing rt73usb-firmware-1.8-7.el6.noarch Installing ipw2100-firmware-1.3-11.el6.noarch Installing ql23xx-firmware-3.03.27-3.1.el6.noarch Installing ipw2200-firmware-3.1-4.el6.noarch Installing rootfiles-8.1-6.1.el6.noarch *** FINISHED INSTALLING PACKAGES *** ^C [root@localhost ~]#
当给出的带+符号的数值大于文件总行数则不会返回信息。
[root@localhost ~]# tail -n +990 install.log [root@localhost ~]#
head与tial结合使用可灵活的读出文件中间部分的数据行。例如想要查看文件中的第11-20行。(|为管道符号,其作用是将前面指令的输出结果作为后面指令的输入)
[root@localhost ~]# head -n 20 install.log | tail -n 10 Installing mozilla-filesystem-1.9-5.1.el6.x86_64 Installing control-center-filesystem-2.28.1-41.el6.x86_64 Installing paktype-fonts-common-2.0-8.el6.noarch Installing mesa-dri-filesystem-11.0.7-4.el6.x86_64 Installing tzdata-2016j-1.el6.noarch Installing foomatic-db-filesystem-4.0-8.20091126.el6.noarch Installing basesystem-10.0-4.el6.noarch Installing khmeros-fonts-common-5.0-9.el6.noarch Installing smc-fonts-common-04.2-11.el6.noarch Installing thai-scalable-fonts-common-0.4.12-2.1.el6.noarch