查看文件汇总cat、head、tail、more、less

一、查看文件命令简述


linux命令中cat、more、less、tail、head均可用来查看文件内容,主要区别有:

cat 用于显示文件的的整个内容,适用于文件内容少的情况,内容多的情况下查阅起来不方便;
headtail分别显示文件的前几行和后几行内容,不加参数默认10行。
moreless一般用于显示文件内容超过一屏的内容,并且有翻页的功能。


二、cat

用于显示整个文档的内容,除了用来查看,重定向也经常用。
cat f1 f2 ls.log >>f5

1,查看文件 cat f1
2,重定向(覆盖) cat f1 > f2
3,重定向(追加)cat f1 > >f2

详见 cat说明查看文件cat


三、head

head用于显示文件的开头部分内容。
默认情况下head指令显示文件的前10行内容。

-c, --bytes=[-]K         print the first K bytes of each file;
                             #显示字节
                             K bytes of each file
  -n, --lines=[-]K         print the first K lines instead of the first 10;
                             #显示行数
  -q, --quiet, --silent    never print headers giving file names
                             #隐藏文件
  -v, --verbose            always print headers giving file names                        

例1:显示前3行

-- 第一种写法
[root@centos7 ~]#head -3 /etc/passwd     
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
-- 第二种写法
[root@centos7 ~]#head -n 3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

例2:显示前8个字节

root@centos7 ~]#head -c 8 /etc/passwd
root:x:0[root@centos7 ~]#

例3:重定向,前3行定位到别的文件

[root@centos7 ~]#cat f1
word
1234
aaaaa
bbbbb
[root@centos7 ~]#head -3 /etc/passwd > f1
[root@centos7 ~]#cat f1
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@centos7 ~]#


四、tail

tail 用来显示文件的最后几行内容,当文件内容有更新时,tail会自己主动刷新,确保一直显示最新的文件内容。
tail命令默认后10行,它的用法可以参照head命令。

  -c, --bytes=K            output the last K bytes; 
                            #显示字节
  -f, --follow[={name|descriptor}]
                            #循环读取
  -F                       same as --follow=name --retry
  -n, --lines=K            output the last K lines, instead of the last 10;
                            #显示行数
  -q, --quiet, --silent    never output headers giving file names
                            #不处理信息

例1:显示后3行

[root@centos7 ~]#tail -n 3 /etc/passwd
wangwang:x:3002:3004::/home/wangwang:/bin/bash
wangcai:x:3003:3003::/home/wangcai:/bin/bash
user01:x:3004:3006::/home/user01:/bin/bash

[root@centos7 ~]#tail -3 /etc/passwd
wangwang:x:3002:3004::/home/wangwang:/bin/bash
wangcai:x:3003:3003::/home/wangcai:/bin/bash
user01:x:3004:3006::/home/user01:/bin/bash

例2:显示后 3 行内容,自动更新

[root@centos7 ~]#tail  -f -n 3  /etc/passwd
user01:x:3004:3006::/home/user01:/bin/bash
user02:x:3005:3007::/home/user02:/bin/bash
user03:x:3006:3008::/home/user03:/bin/bash

例3:显示两个文件,默认显示文件名

[root@centos7 ~]#tail -n3 /etc/passwd /etc/group
==> /etc/passwd <==
user01:x:3004:3006::/home/user01:/bin/bash
user02:x:3005:3007::/home/user02:/bin/bash
user03:x:3006:3008::/home/user03:/bin/bash

==> /etc/group <==
user01:x:3006:
user02:x:3007:
user03:x:3008:


五、more

常用操作

more 命令会一页一页的显示,方便逐页阅读。
空格键(space)往下一页显示。
b 键就会往回(back)一页显示。
回车(Enter)向下一行 。
q(Enter)退出 。

more 命令从前向后读取文件,因此在启动时就加载整个文件。

  -d        display help instead of ring bell #响铃功能
  -f        count logical, rather than screen lines
  -l        suppress pause after form feed
  -p        do not scroll, clean screen and display text
            #通过清除窗口而不是滚屏来对文件进行换页,与-c 选项相似
  -c        do not scroll, display text and clean line ends
            #从顶部清屏,然后显示
  -u        suppress underlining  #把文件内容中的下画线去掉
  -s        squeeze multiple blank lines into one
  -NUM      specify the number of lines per screenful
            #定义屏幕大小为 n 行  n为数字
  +NUM      display file beginning from line number NUM
            #从笫 n 行开始显示   n为数字
  +/STRING  display file beginning from search string match
  -V        output version information and exit


例1:从第3行开始显示 +3

[root@centos7 ~]#more +3 f1
3
4
5
6

例2:定义显示3行 -3

[root@centos7 ~]#more -3 f1
1
2
3
--More--(6%)

例3:查找第一个出现"21"字符串的行,并从前两行开始显示输出

[root@centos7 ~]#more +/21 f1

...skipping
19
20
21
22

例4:常用组合命令使用 ll 、 more

[root@centos7 ~]#ll / |more -3
total 28
drwxr-xr-x.   2 root root    6 Dec 14 18:46 a
drwxr-xr-x.   4 root root  261 Dec 23 18:44 app

列举根,并且每次显示三行。

六、less

less 与 more 类似,less 的用法比起 more 更加灵活。

常用操作

符号描述
/字符串向下搜索“字符串”的功能
?字符串向上搜索“字符串”的功能
n重复前一个搜索,结合搜索使用
N反向重复前一个搜索,结合搜索使用
b向前翻一页
d向后翻半页
q退出
G移动到最后一行
g移动到第一行
空格键向后翻一页
向上键向上翻动一行
向下键向下翻动一行

常用参数

参数描述
-f强迫打开特殊文件,如外围设备代号、目录和二进制文件
-i忽略搜索时的大小写
-m显示类似more命令的百分比
-M显示读取文件的百分比、行号及总行数
-N在每行前显示行号
-s显示连续空行为一行
-e当文件显示结束后,自动离开

例1:带行号显示内容

[root@centos7 ~]#less -N /etc/passwd
      1 root:x:0:0:root:/root:/bin/bash
      2 bin:x:1:1:bin:/bin:/sbin/nologin
      3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
      4 adm:x:3:4:adm:/var/adm:/sbin/nologin
      5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
      6 sync:x:5:0:sync:/sbin:/bin/sync

例2:搜索指定字符

在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值