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
Linux中head和tail的区别,linux head和tail命令详解相关推荐
- Linux 中 3 个文件打包上传和下载相关命令详解
tar 命令 通过 SSH 访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar 命令就是必不可少的一个功能强大的工具.Linux 中最流行的tar是麻雀虽小,五脏俱全,功能强大. 使用t ...
- Linux系统与网络、磁盘参数和日志监控等命令详解二
创作人QQ:851301776,邮箱:lfr890207@163.com, 欢迎大家一起技术交流,本博客主要是自己学习的心得体会,只为每天进步一点点! 个人座右铭: 1.没有横空出世,只要厚积一定发. ...
- linux中s权限大小写的区别,Linux中的权限
1.文件与目录权限 1)UID与GID ØUID UID是一个数值,是Linux系统中唯一的用户标识,用于区别不同的用户.在系统内部管理进程和文件保护时使用UID字段.在Linux系统中,注册名和UI ...
- linux中fopen和open的区别,Linux下open与fopen的区别
int open(const char *path, int access,int mode) path 要打开的文件路径和名称 access 访问模式,宏定义和含义如下: O_RDONLY ...
- linux设置环境变量_Linux怎么设置系统环境变量之export命令详解
请关注本头条号,每天坚持更新原创干货技术文章. 如需学习视频,请在微信搜索公众号"智传网优"直接开始自助视频学习 1. Linux export命令简介 Linux export命 ...
- Linux(6)RedHat7 基本命令五-hwclock(clock)命令详解
导言 hwclock命令 作用 描述 用法 详细全选项参数解释 常用选项 -r选项show -w选项 实践 - 验证clock和hwclock是一个命令 实践二-查看采用的时区和时间格式 总结 导言 ...
- linux打印机添加命令,Linux Shell脚本系列教程(二):终端打印命令详解
终端打印 终端是交互式工具,用户可以通过它与shell环境进行交互.在终端中打印文本是大多数shell脚本和工具日常需要执行的基本任务.通过终端打印,人们可以知道系统的运行状态,这对用户来说是至关重要 ...
- Linux 中 sudo和su的区别
Linux 中 sudo和su的区别 Linux小知识:sudo su和su的区别 1.内容不同: su命令:用来切换用户,substitute英文含义"代替"的意思. sudo命 ...
- linux常用命令详解
文章目录 一.Linux文档目录结构 二.常用系统工作命令 1. echo 用于在终端输出字符串或变量提取后的值 2. date 用于显示及设置系统的时间或日期 3. reboot 用于重启系统 4. ...
- Linux命令之top命令详解
目录 一.数据详解 1.1 系统统计数据 1.1.1 第一行 1.1.2 第二行 1.1.3 第三行 1.1.4 第四行 1.1.5 第五行 1.2 进程相关数据 二.使用详解 2.1 top命令参数 ...
最新文章
- 3060显卡,送一块!
- 《代码大全》阅读笔记-26-代码调整技术
- sublime3(anaconda) 无法自动补全第三方模块
- C# 7中函数多值返回_转自InfoQ
- myeclipse使用太卡问题
- C语言删除链表的算法(附完整源码)
- Twitch如何实现转码器比FFmepg性能提升65%?(上)
- Java 中的异常和处理详解
- java.util -- List接口
- 什么叫返回路径平面上的间隙_苏州平面设计培训:平面设计师工作流程
- Java持久性锁定初学者指南
- java integer最大值_五分钟学会java中的基础类型封装类
- html中选择日期怎么实现,JavaScript+HTML5实现的日期比较功能示例
- 比特币挖矿成本表_用算力趋势预测减半后比特币挖矿成本价
- opencv 图像平移、缩放、旋转、翻转 图像仿射变换
- pycharm 怎么快速生成文件夹结构_Pycharm配置Qt工具(ubuntu18.04)
- H5分享到微信朋友圈与好友实现思路
- 全面理解搜索Query:当你在搜索引擎中敲下回车后,发生了什么?
- kettle执行sql语句错误总结
- 数据统计并制作韦恩图