Linux查看文件内容命令:cat、more、less、head、tail、grep、wc

931 阅读12分钟

1 查看文件内容

1.1 cat命令

cat( 英文全拼:concatenate )命令的用途是连接文件或标准输入并打印。这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用。

命令格式:

cat [选项] 文件名

选项说明:

选项效果
-n显示行号包括空行
-b跳过空白行编号
-s将所有的连续的多个空行替换为一个空行(压缩成一个空行)
-A显示隐藏字符

示例:

1)cat查看系统版本

[root@localhost ky15]# cat /etc/centos-release   //查看系统内核版本
[root@localhost ky15]# cat /proc/version        //查看系统内核版本
Linux version 3.10.0-693.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Tue Aug 22 21:09:27 UTC 2017

2)cat加选项,处理空行

[root@localhost data]# cat -b test.txt     //显示行号,空行不编号
     1  111
​
     2  111
​
​
     3  1111          1111
[root@localhost data]# cat -s test.txt    //将连续的空行压缩成一个空行
1111111111          1111

3)同时查看多个文件,按文件的排列顺序依次显示内容。

[root@test1 opt]# echo "1" > 1.txt
[root@test1 opt]# echo "2" > 2.txt
[root@test1 opt]# cat 1.txt 2.txt     //查看多个文件,按文件的排列顺序依次显示内容
1
2
[root@test1 opt]# cat 2.txt 1.txt     //先显示2.txt内容,后显示1.txt
2
1

4) tac 从最后一行倒序显示内容,并将所有内容输出

[root@localhost ~]# cat 1.txt     //从第一行开始显示内容
111
333
[root@localhost ~]# tac 1.txt     //从最后一行倒序显示内容
333
111

5)hexdump 查看非文本文件内容

-C:输出规范的十六进制和ASCII码

-n length:格式化输出文件的前length个字节

[root@localhost ~]# hexdump -C -n 10 /dev/sda   //格式化输出文件的前10个字节,并以16进制显示
00000000  eb 63 90 10 8e d0 bc 00  b0 b8                    |.c........|
0000000a

1.2 分页查看文件内容——more命令

命令格式:

more [选项] 文件名

操作说明:

交互指令功能
h 或 ?显示 more 命令交互命令帮助。
q 或 Q退出 more。
v在当前行启动一个编辑器。
:f显示当前文件的文件名和行号。
!<命令> 或 :!<命令>在子Shell中执行指定命令。
回车键向下移动一行。
空格键向下移动一页。
Ctrl+l刷新屏幕。
=显示当前行的行号。
'转到上一次搜索开始的地方。
Ctrf+f向下滚动一页。
.重复上次输入的命令。
/ 字符串搜索指定的字符串。
d向下移动半页。
b向上移动一页。

示例:

[root@localhost ~]# ls -lh /etc/*.conf | more    //命令显示过多可以使用管道加more查看,more可以向下翻页,不支持向上翻页
-rw-r--r--. 1 root root   55 3月   1 2017 /etc/asound.conf
-rw-r--r--. 1 root root  15K 8月   4 2017 /etc/autofs.conf
-rw-------. 1 root root  232 8月   4 2017 /etc/autofs_ldap_auth.conf
-rw-r--r--. 1 root root  22K 8月   7 2017 /etc/brltty.conf
-rw-r--r--. 1 root root  676 8月   3 2017 /etc/cgconfig.conf
-rw-r--r--. 1 root root  265 6月  18 23:09 /etc/cgrules.conf
-rw-r--r--. 1 root root  131 8月   3 2017 /etc/cgsnapshot_blacklist.conf
-rw-r--r--. 1 root root 1.1K 1月  31 2017 /etc/chrony.conf
-rw-r--r--. 1 root root 1.2K 8月   3 2017 /etc/dleyna-server-service.conf
-rw-r--r--. 1 root root  27K 8月   3 2017 /etc/dnsmasq.conf
-rw-r--r--. 1 root root 1.3K 8月   5 2017 /etc/dracut.conf
-rw-r--r--. 1 root root  112 3月  16 2017 /etc/e2fsck.conf
-rw-r--r--. 1 root root   20 6月  24 2014 /etc/fprintd.conf
-rw-r--r--. 1 root root   38 5月   2 2017 /etc/fuse.conf
-rw-r--r--. 1 root root  842 11月  6 2016 /etc/GeoIP.conf
-rw-r--r--. 1 root root    9 6月   7 2013 /etc/host.conf
-rw-r--r--. 1 root root 4.8K 8月   2 2017 /etc/idmapd.conf
-rw-r--r--. 1 root root 2.1K 8月   5 2017 /etc/ipsec.conf
-rw-r--r--. 1 root root 7.1K 6月  18 23:08 /etc/kdump.conf
-rw-r--r--. 1 root root  590 4月  29 2017 /etc/krb5.conf
-rw-r--r--. 1 root root  478 6月  13 2017 /etc/ksmtuned.conf
-rw-r--r--. 1 root root   28 2月  28 2013 /etc/ld.so.conf
-rw-r-----. 1 root root  191 4月  19 2017 /etc/libaudit.conf
-rw-r--r--. 1 root root 2.4K 10月 13 2013 /etc/libuser.conf
-rw-r--r--. 1 root root   19 6月  18 23:12 /etc/locale.conf
-rw-r--r--. 1 root root  662 7月  31 2013 /etc/logrotate.conf
-rw-r--r--. 1 root root 5.1K 6月  10 2014 /etc/man_db.conf
-rw-r--r--. 1 root root  936 8月   3 2017 /etc/mke2fs.conf
--More--

1.3 分页查看文件内容——less命令

命令格式:

less [选项] 文件名

操作说明

  • /字符串:向下搜索“字符串”的功能;

    ?字符串:向上搜索“字符串”的功能 ;

    n:向下查找,重复前一个搜索(与 / 或 ? 有关);

    N:向上查找,反向重复前一个搜索(与 / 或 ? 有关)。

  • Page Up 向上翻页,Page Down 向下翻页

  • 其余参考more

示例:

[root@localhost ~]# ls -lh /etc/*.conf | less     //命令显示过多可以使用管道加less查看,向下或向上查看都可以
-rw-r--r--. 1 root root   55 3月   1 2017 /etc/asound.conf
-rw-r--r--. 1 root root  15K 8月   4 2017 /etc/autofs.conf
-rw-------. 1 root root  232 8月   4 2017 /etc/autofs_ldap_auth.conf
-rw-r--r--. 1 root root  22K 8月   7 2017 /etc/brltty.conf
-rw-r--r--. 1 root root  676 8月   3 2017 /etc/cgconfig.conf
-rw-r--r--. 1 root root  265 6月  18 23:09 /etc/cgrules.conf
-rw-r--r--. 1 root root  131 8月   3 2017 /etc/cgsnapshot_blacklist.conf
-rw-r--r--. 1 root root 1.1K 1月  31 2017 /etc/chrony.conf
-rw-r--r--. 1 root root 1.2K 8月   3 2017 /etc/dleyna-server-service.conf
-rw-r--r--. 1 root root  27K 8月   3 2017 /etc/dnsmasq.conf
-rw-r--r--. 1 root root 1.3K 8月   5 2017 /etc/dracut.conf
-rw-r--r--. 1 root root  112 3月  16 2017 /etc/e2fsck.conf
-rw-r--r--. 1 root root   20 6月  24 2014 /etc/fprintd.conf
-rw-r--r--. 1 root root   38 5月   2 2017 /etc/fuse.conf
-rw-r--r--. 1 root root  842 11月  6 2016 /etc/GeoIP.conf
-rw-r--r--. 1 root root    9 6月   7 2013 /etc/host.conf
-rw-r--r--. 1 root root 4.8K 8月   2 2017 /etc/idmapd.conf
-rw-r--r--. 1 root root 2.1K 8月   5 2017 /etc/ipsec.conf
-rw-r--r--. 1 root root 7.1K 6月  18 23:08 /etc/kdump.conf
-rw-r--r--. 1 root root  590 4月  29 2017 /etc/krb5.conf
-rw-r--r--. 1 root root  478 6月  13 2017 /etc/ksmtuned.conf
-rw-r--r--. 1 root root   28 2月  28 2013 /etc/ld.so.conf
-rw-r-----. 1 root root  191 4月  19 2017 /etc/libaudit.conf
-rw-r--r--. 1 root root 2.4K 10月 13 2013 /etc/libuser.conf
-rw-r--r--. 1 root root   19 6月  18 23:12 /etc/locale.conf
-rw-r--r--. 1 root root  662 7月  31 2013 /etc/logrotate.conf
-rw-r--r--. 1 root root 5.1K 6月  10 2014 /etc/man_db.conf
-rw-r--r--. 1 root root  936 8月   3 2017 /etc/mke2fs.conf
:

1.4 查看头部和尾部内容——head/tail命令

命令格式:

head -n <行数> 文件名 (不加行数默认开头十行)

tail -n<行数> 文件名 (不加行数默认结尾十行)

tail -f 文件名:实时跟踪最后十行

示例:

1)加入相同数字,比较head和tail的区别

[root@localhost ~]# head -n +3 3.txt    //+3 只显示前三行
1
2
3
[root@localhost ~]# tail -n +3 3.txt    //+3 从第三行开始
3
4
5
6
7
​
[root@localhost ~]# head -n -3 3.txt     //-3 不显示最后3行
1
2
3
4
5
[root@localhost ~]# tail -n -3 3.txt    //-3 只显示最后三行
6
7
8

1644398170247.png

2)通过管道符,head和tail结合使用

[root@localhost ~]# ifconfig |head -2|tail -1     //只看第二行
        inet 192.168.91.100  netmask 255.255.255.0  broadcast 192.168.91.255
        
[root@localhost ~]#cat /dev/urandom |tr -dc '[:alnum:]' |head -c12    //生成随机密码
513sHzLASljv

3)实时追踪最后十行

[root@localhost ~]# tail -f /etc/passwd         //实时追踪最后十行
gnome-initial-setup:x:991:986::/run/gnome-initial-setup/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
user01:x:1000:1000:user01:/home/user01:/bin/bash
user02:x:1001:1001::/home/user02:/bin/bash
alice:x:1002:1003::/home/alice:/bin/bash
jack:x:1003:1004::/home/jack:/bin/bash
zhangsan:x:1004:1005::/home/zhangsan:/bin/bash

1.5 检索、过滤文件内容——grep命令

grep(global regular expression print,全局正则表达式输出),用于查找文件里符合条件的字符串。

命令格式

grep [选项]… 查找条件 目标文件

选项说明

选项作用
-i查找时忽略大小写
-v反向查找,输出与查找条件不相符的行
-o只显示匹配项
-w精确匹配。例如-w local,那么localhost不会被匹配,只精确匹配local
-f对比两个文件的相同行
-l列出文件内容符合指定样式的文件名称
-L列出文件内容不符合指定样式的文件名称
-R在目录内递归查找
-n显示匹配行及行号
-c计算符合样式的行数
-e实现可多个查找条件的匹配,逻辑or(或)关系
-A 5显示匹配行和后5行的内容
-B 5显示匹配行和前5行的内容
-C 5显示匹配行和前后各5行的内容

基本格式

  • 要查找的字符串以双引号括起来,单引号也可以
  • “^……”表示以……开头,“……$”表示以……结尾
  • “^$”表示空行 示例:

1)在/etc/passwd文件中,检索出含有“alice"的行

[root@localhost ~]# grep alice /etc/passwd
alice:x:1002:1003::/home/alice:/bin/bash

微信图片_20220210212505.png

2)在/etc/passwd文件中检索“alice",只输出“alice"

[root@localhost ~]# grep -o alice /etc/passwd
alice
alice

微信图片_20220210212550.png

3)在多个文件中检索”alice"

[root@localhost ~]# grep alice /etc/passwd /etc/group /etc/fstab
/etc/passwd:alice:x:1002:1003::/home/alice:/bin/bash
/etc/group:alice:x:1003:

微信图片_20220210212710.png

4)使用 -l 参数列出包含指定样式的文件的文件名

[root@localhost ~]# grep -l alice /etc/passwd /etc/group /etc/fstab
/etc/passwd
/etc/group

微信图片_20220210212755.png

5) 使用-n参数,在文件中查找指定样式并显示匹配行的行号

[root@localhost ~]# grep -n bash /etc/passwd    //检索出包含“bash"的行并显示行号
1:root:x:0:0:root:/root:/bin/bash
41:yuji:x:1000:1000:yuji:/home/yuji:/bin/bash
42:user01:x:1001:1001::/home/user01:/bin/bash
43:alice:x:1002:1003::/home/alice:/bin/bash
44:jack:x:1003:1004::/home/jack:/bin/bash
45:zhangsan:x:1004:1005::/home/zhangsan:/bin/bash

微信图片_20220210212957.png

6)使用-v反向查找,输出与查找条件不相符的行

[root@localhost ~]# grep -v nologin /etc/passwd    //检索出不含”nologin"的行
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
yuji:x:1000:1000:yuji:/home/yuji:/bin/bash
user01:x:1001:1001::/home/user01:/bin/bash
alice:x:1002:1003::/home/alice:/bin/bash
jack:x:1003:1004::/home/jack:/bin/bash
zhangsan:x:1004:1005::/home/zhangsan:/bin/bash

7)使用-i "^a",检索出以a开头的行,且不区分大小写

[root@localhost ~]# grep -i "^a" /etc/passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
alice:x:1002:1003::/home/alice:/bin/bash
Amy:x:1005:1006::/home/Amy:/bin/bash

微信图片_20220210213102.png

8)使用"h$",检索出以h结尾的行

[root@localhost ~]# grep "h$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
yuji:x:1000:1000:yuji:/home/yuji:/bin/bash
user01:x:1001:1001::/home/user01:/bin/bash
alice:x:1002:1003::/home/alice:/bin/bash
jack:x:1003:1004::/home/jack:/bin/bash
zhangsan:x:1004:1005::/home/zhangsan:/bin/bash
Amy:x:1005:1006::/home/Amy:/bin/bash

微信图片_20220210213152.png

9)使用-v "^$" 过滤出非空行

[root@localhost test]# cat 123.txt     //查看123.txt文件内容
111222
aaa
[root@localhost test]# grep -v "^$" 123.txt    //过滤出非空行
111
222
aaa
[root@localhost ~]# cat 123.txt |grep -v '^$' >test.txt  //将非空行写入到test.txt文件
[root@localhost test]# cat test.txt        //查看test.txt
111
222
aaa

10)使用通配符和管道符,过滤出IP地址

[root@localhost ~]#ifconfig ens33 |grep netmask|grep -o "[0-9][0-9][0-9].[0-9][0-9][0-9].[0-9][0-9].[0-9][0-9][0-9]"|head -n 1     //过滤出IP地址
192.168.91.100
[root@localhost ~]# ifconfig ens33 |grep -o "[0-9]+.[0-9]+.[0-9]+.[0-9]+"   //过滤出IP地址、子网掩码、广播地址
192.168.91.100
255.255.255.0
192.168.91.255
[root@localhost ~]# ifconfig ens33 |grep -o "[0-9]+.[0-9]+.[0-9]+.[0-9]+"|head -1  //过滤出IP地址
192.168.91.100

11)-f 对比两个文件的相同行

[root@localhost ~]# cat 1.txt
111
333
[root@localhost ~]# cat 2.txt
111
222
3
[root@localhost ~]# grep -f 1.txt 2.txt   //过滤出两个文件的相同行
111

12)-e 多条件”或“的查询

[root@localhost test]# cat file01.txt
11
33
111222
[root@localhost test]# grep -e 1 -e 2 file01.txt  //检索出包含1或2的行
11
111222

13) 使用 -E 'pattern1.*pattern2',多条件”且“的查询

[root@localhost test]# grep -E "1.*2" file01.txt   //检索出包含1且包含2的行
111222

14)在指定目录内检索出含有指定内容的文件名:

 # 列出/home目录下(包括其子目录内)含有“root"内容的文件名,要求精确匹配。
 [root@localhost ~]# grep -R -w -l "root" /home    //-R在目录内递归查找,-w表示精确匹配,-l列出文件内容符合指定样式的文件名称
 /home/yuji2/.cache/imsettings/log
 /home/yuji2/.cache/tracker/meta.db
 /home/yuji2/.cache/tracker/meta.db-wal
 ​
 # 列出/home目录下(包括其子目录内)含有“root"内容的文件名,不要求精确匹配。
 [root@localhost ~]# grep -R -l "root" /home
 /home/yuji2/.cache/imsettings/log
 /home/yuji2/.cache/tracker/meta.db
 /home/yuji2/.cache/tracker/meta.db-wal
 /home/yuji2/.cache/tracker/ontologies.gvdb
 /home/yuji2/.local/share/tracker/data/tracker-store.ontology.journal

微信图片_20220405012136.png

2 统计文件

命令wc(word count)

命令格式:

wc [选项]... 目标文件.. (不加选项,则默认统计行数、单词个数、字节数三者)

统计列数,wc本身就是参数

  • -l:统计行数
  • -w:统计单词个数
  • -c:统计字节数
[root@localhost test]# cat 1.txt     //查看文件内容
111222
aaa
[root@localhost test]# wc -l 1.txt    //统计行数,注意空行也会被统计
4 1.txt
[root@localhost test]# wc -w 1.txt    //统计单词数
3 1.txt
[root@localhost test]# wc -c 1.txt    //统计字节数,注意换行符也会被统计
13 1.txt
[root@localhost test]# wc 1.txt       //不指定选项,默认lwc都显示
 4  3 13 1.txt