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

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

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    //将连续的空行压缩成一个空行
111
111
1111          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
复制代码


网络异常,图片无法展示
|


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行的内容


相关文章
|
4天前
|
Linux 应用服务中间件 nginx
linux小技巧: 可以补全命令 别名永久有效
linux小技巧: 可以补全命令 别名永久有效
|
4天前
|
缓存 关系型数据库 MySQL
linux 基本知识与命令
linux 基本知识与命令
|
4天前
|
网络协议 Linux 数据库
蓝易云 - Linux常用命令dhcpd命令
在使用dhcpd命令之前,需要确保已经正确配置了dhcpd.conf文件,该文件定义了DHCP服务器的行为和要分配的IP地址范围等信息。
10 0
|
4天前
|
Linux Docker 容器
蓝易云 - 【Linux】如何在linux系统重启或启动时执行命令或脚本(也支持docker容器内部)
以上就是在Linux系统和Docker容器中设置启动时运行命令或脚本的方法。希望对你有所帮助。
18 0
|
5天前
|
Linux 开发工具
蓝易云 - Linux虚拟机常用命令
以上就是一些常用的Linux命令,希望对你有所帮助。
17 0
|
6天前
|
存储 Linux 开发工具
Linux 基础(从环境搭建到基础命令)
Linux 基础(从环境搭建到基础命令)
|
8天前
|
存储 Linux 网络安全
在 Linux 中通过 SSH 执行远程命令时,无法自动加载环境变量(已解决)
SSH远程执行命令时遇到“命令未找到”问题,原因是Linux登录方式不同导致环境变量加载差异。解决方案:将环境变量写入`/etc/profile.d/`下的文件,或手动在命令前加载环境变量,如`source /etc/profile`。
|
19天前
|
监控 Linux Perl
【专栏】Linux 命令小技巧:显示文件指定行内容的方法,包括使用`head`和`tail`命令显示文件头尾部分
【4月更文挑战第28天】本文介绍了Linux中显示文件指定行内容的方法,包括使用`head`和`tail`命令显示文件头尾部分,利用`sed`的行号指定功能以及`awk`处理文本数据。文章还列举了在代码审查、日志分析和文本处理中的应用场景,并提醒注意文件编码、行号准确性及命令组合使用。通过练习和实践,可以提升Linux文本文件处理的效率。
|
19天前
|
监控 Linux
linux(十六)文件和目录相关命令-查看文件头部内容head与查看文件尾部内容tail
linux(十六)文件和目录相关命令-查看文件头部内容head与查看文件尾部内容tail
68 0
|
19天前
|
Linux 监控 Shell
Linux 终端命令之文件浏览(4) head, tail
Linux 终端命令之文件浏览(4) head, tail
32 0
Linux 终端命令之文件浏览(4) head, tail