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


相关文章
|
2月前
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
341 1
二、Linux文本处理与文件操作核心命令
|
2月前
|
Linux
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
258 137
|
2月前
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
590 57
|
5月前
|
JSON 自然语言处理 Linux
linux命令—tree
tree是一款强大的Linux命令行工具,用于以树状结构递归展示目录和文件,直观呈现层级关系。支持多种功能,如过滤、排序、权限显示及格式化输出等。安装方法因系统而异常用场景包括:基础用法(显示当前或指定目录结构)、核心参数应用(如层级控制-L、隐藏文件显示-a、完整路径输出-f)以及进阶操作(如磁盘空间分析--du、结合grep过滤内容、生成JSON格式列表-J等)。此外,还可生成网站目录结构图并导出为HTML文件。注意事项:使用Tab键补全路径避免错误;超大目录建议限制遍历层数;脚本中推荐禁用统计信息以优化性能。更多详情可查阅手册mantree。
424 143
linux命令—tree
|
1月前
|
存储 安全 Linux
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
Linux虚拟机遇紧急模式?别慌!多因磁盘挂载失败。本文教你通过日志定位问题,用`xfs_repair`等工具修复文件系统,三步快速恢复。掌握查日志、修磁盘、验重启,轻松应对紧急模式,保障系统稳定运行。
264 2
|
2月前
|
缓存 监控 Linux
Linux内存问题排查命令详解
Linux服务器卡顿?可能是内存问题。掌握free、vmstat、sar三大命令,快速排查内存使用情况。free查看实时内存,vmstat诊断系统整体性能瓶颈,sar实现长期监控,三者结合,高效定位并解决内存问题。
147 0
Linux内存问题排查命令详解
|
2月前
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
199 16
|
4月前
|
监控 Linux 网络安全
Linux命令大全:从入门到精通
日常使用的linux命令整理
753 13
|
5月前
|
Linux 网络安全 数据安全/隐私保护
使用Linux系统的mount命令挂载远程服务器的文件夹。
如此一来,你就完成了一次从你的Linux发车站到远程服务器文件夹的有趣旅行。在这个技术之旅中,你既探索了新地方,也学到了如何桥接不同系统之间的距离。
783 21
|
5月前
|
监控 Linux
Linux系统中使用df命令详解磁盘使用情况。
`df`命令是Linux系统管理员和用户监控和管理磁盘空间使用的重要工具。掌握它的基本使用方法和选项可以帮助在必要时分析和解决空间相关问题。简洁但功能丰富,`df`命令确保了用户可以快速有效地识别和管理文件系统的空间使用情况。
352 13