Linux中head和tail的区别,linux head和tail命令详解

1.head命令

回到顶部

head命令显示文件的开头几行,常用命令格式如下:

[root@initroot ~]# head [-n number] 文件

head命令默认显示文件的前十行,-n选项后接数字,可以指定显示文件的行数。

显示/etc/manpath.config文件的前十行:

[root@initroot ~]# head /etc/manpath.config

显示/etc/manpath.config文件的前二十行:

[root@initroot ~]# head -n 20 /etc/manpath.config

-n选项后面接的数字可以是负数,表示显示文件前面的内容,但是不包括文件最后的n行。例如-n -100表示除了最后的100行内容不显示外,其他内容都显示出来:

[root@initroot ~]# head -n -100 /etc/man_db.conf

以/etc/passwd为例:peter@initroot:~/Desktop$ head /etc/passwd

root:x:0:0:root:/root:/bin/bash

daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin

bin:x:2:2:bin:/bin:/usr/sbin/nologin

sys:x:3:3:sys:/dev:/usr/sbin/nologin

sync:x:4:65534:sync:/bin:/bin/sync

games:x:5:60:games:/usr/games:/usr/sbin/nologin

man:x:6:12:man:/var/cache/man:/usr/sbin/nologin

lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin

mail:x:8:8:mail:/var/mail:/usr/sbin/nologin

news:x:9:9:news:/var/spool/news:/usr/sbin/nologin

peter@initroot:~/Desktop$ head -n 5 /etc/passwd

root:x:0:0:root:/root:/bin/bash

daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin

bin:x:2:2:bin:/bin:/usr/sbin/nologin

sys:x:3:3:sys:/dev:/usr/sbin/nologin

sync:x:4:65534:sync:/bin:/bin/sync

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=[-]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.

GNU coreutils online help: http://www.gnu.org/software/coreutils

Full documentation at: http://www.gnu.org/software/coreutils/head

or available locally via:info '(coreutils) head invocation'

2.tail命令

回到顶部

tail命令和head命令正好相反,显示文件的最后几行。常见的命令格式如下:

[root@initroot ~]# tail [-n number] 文件

选项与参数:

-n :后面接数字,代表显示文件最后几行

-f :表示持续读取并显示文件,直到按下ctrl-c才会结束tail

用tail命令查看/etc/manpath.config文件:

[root@initroot ~]# tail /etc/manpath.config

tail默认显示文件的最后十行,加上-n选项可以指定查看的行数,类似与head -n -xx命令。

用tail命令查看/etc/manpath.config文件的最后20行:

[root@initroot ~]# tail -n 20 /etc/manpath.config

如果不知道/etc/manpath.config文件有几行,却只想列出100行以后的文件内容:

[root@initroot ~]# tail -n +100 /etc/manpath.conf

持续读取显示/var/log/messages文件的最后十行:

[root@initroot ~]# tail -f /var/log/syslog

Jan 8 15:53:12 initroot dbus-daemon[502]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service' requested by ':1.12' (uid=0 pid=554 comm="/usr/sbin/NetworkManager --no-daemon " label="unconfined")

Jan 8 15:53:12 initroot systemd[1]: Starting Network Manager Script Dispatcher Service...

Jan 8 15:53:12 initroot dbus-daemon[502]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'

Jan 8 15:53:12 initroot systemd[1]: Started Network Manager Script Dispatcher Service.

Jan 8 15:53:12 initroot nm-dispatcher: req:1 'connectivity-change': new request (1 scripts)

Jan 8 15:53:12 initroot nm-dispatcher: req:1 'connectivity-change': start running ordered scripts...

Jan 8 16:06:34 initroot gnome-terminal-[1748]: g_menu_insert_item: assertion 'G_IS_MENU_ITEM (item)' failed

Jan 8 16:07:10 initroot gnome-terminal-[1748]: g_menu_insert_item: assertion 'G_IS_MENU_ITEM (item)' failed

Jan 8 16:17:01 initroot CRON[3809]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)

Jan 8 16:33:17 initroot gnome-terminal-[1748]: g_menu_insert_item: assertion 'G_IS_MENU_ITEM (item)' failed

^C

peter@initroot:~$

只有在用户按下crtl-c之后,tail才会退出。-f选项可以持续监听文件的变化,这对于查看一些经常变化的文件非常有帮助,例如本例中系统日志文件。

显示文件/etc/manpath.conf的第11行到第20行:

[root@initroot ~]# head -n 20 /etc/manpath.conf | tail -n 10

在上面的输出中加入行号:

[root@initroot ~]# cat -n /etc/manpath.conf | head -n 20 | tail -n 10

这里用到管道将几个命令串联到一起,这样前一个命令的输出就变成了后一个命令的输入。

关于管道的概念参考:

linux管道

peter@initroot:~/Desktop$ tail /etc/passwd

speech-dispatcher:x:117:29:Speech Dispatcher,,,:/var/run/speech-dispatcher:/bin/false

pulse:x:118:124:PulseAudio daemon,,,:/var/run/pulse:/usr/sbin/nologin

hplip:x:119:7:HPLIP system user,,,:/var/run/hplip:/bin/false

geoclue:x:120:126::/var/lib/geoclue:/usr/sbin/nologin

peter:x:1000:1000:peter,,,:/home/peter:/bin/bash

vboxadd:x:999:1::/var/run/vboxadd:/bin/false

mysql:x:1001:1001::/home/mysql:/sbin/nologin

www:x:1002:1002::/home/www:/sbin/nologin

sshd:x:121:65534::/run/sshd:/usr/sbin/nologin

postfix:x:122:128::/var/spool/postfix:/usr/sbin/nologin

peter@initroot:~/Desktop$ tail -n 5 /etc/passwd

vboxadd:x:999:1::/var/run/vboxadd:/bin/false

mysql:x:1001:1001::/home/mysql:/sbin/nologin

www:x:1002:1002::/home/www:/sbin/nologin

sshd:x:121:65534::/run/sshd:/usr/sbin/nologin

postfix:x:122:128::/var/spool/postfix:/usr/sbin/nologin

initroot编辑整理,转载请注明www.initroot.com

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值