Linux
基础知识
01搭建Centos7系统
02软件管理仓库
03Shell
04查看帮助命令
05系统工作命令
06系统状态检测命令
07工作目录切换命令
08文本文件编辑命令
09文件目录管理
10打包压缩与搜索命令
11输入输出重定向
12管道符与通配符
13Vim编辑器
14Shell脚本
15shell流程控制语句
16用户管理操作
17文件权限与归属
18文件隐藏属性
19文件访问控制列表
20su与sudo
21存储结构
22物理设备命名规则
23文件系统
24挂载硬件设备
25虚拟机添加硬盘设备
26系统中操作添加硬盘设备
27添加交换分区
28磁盘容器配额
29软硬方式链接
30RAID
31LVM
32程序安装与管理
33计划任务
34日志文件分析
35测试网络联通性
36FTP服务
37操作系统基本符号
38操作系统三剑客
39shell基础
40shell语法
41DNS
42Apache概述
43Nginx概述
44集群概述
Vmware安装Ubuntu
更换源
-
+
首页
08文本文件编辑命令
# 文本文件编辑命令 Linux 系统中“一切都是文件”,而对服务程序进行配置自然也就是编辑程序的配置文件。 ## cat cat 命令用于查看纯文本文件(内容较少的),格式为“cat [选项] [文件]”。 Linux 系统中有多个用于查看文本内容的命令,每个命令都有自己的特点,比如这个 cat命令就是用于查看内容较少的纯文本文件的。 加上参数 -n,可以显示行号。 ```linux [root@bogon test]# cat /tmp/anaconda.log 22:43:36,505 DEBUG anaconda: setting locale to: zh_CN.UTF-8 22:43:36,719 WARN anaconda: /usr/lib/python2.7/site-packages/meh/ui/gui.py:23: PyGIWarning: Gtk was imported without specifying a version first. Use gi.require_version('Gtk', '3.0') before import to ensure that the right version gets loaded. from gi.repository import Gtk 22:43:37,349 DEBUG anaconda: Detected primary monitor: 1280x768 95dpix 96dpiy 22:43:37,365 DEBUG anaconda: Entered hub: InitialSetupMainHub 22:43:37,519 DEBUG anaconda: Dump missing interfaces in NetworkControlBox initialize method 22:43:37,825 DEBUG anaconda: network: GUI, device configuration added: connection 754631f2-1fb0-4781-bf1e-d0f9ae274034 device ens33 22:43:37,838 DEBUG anaconda: network: GUI, not adding connection 754631f2-1fb0-4781-bf1e-d0f9ae274034, already in list 22:43:38,031 ERR anaconda: Initialization controller for hub InitialSetupMainHub expected but missing. 22:43:48,848 DEBUG anaconda: network: GUI, not adding virbr0 22:43:49,199 DEBUG anaconda: network: GUI, not adding virbr0 22:43:49,237 DEBUG anaconda: network: can't find connection with uuid 8dc5662e-3671-4304-8adc-70ff92901273 22:43:49,251 DEBUG anaconda: network: can't find connection with uuid 8dc5662e-3671-4304-8adc-70ff92901273 22:43:49,265 DEBUG anaconda: network: can't find connection with uuid 8dc5662e-3671-4304-8adc-70ff92901273 22:43:49,283 DEBUG anaconda: network: can't find connection with uuid 8dc5662e-3671-4304-8adc-70ff92901273 22:43:49,301 DEBUG anaconda: network: can't find connection with uuid 8dc5662e-3671-4304-8adc-70ff92901273 22:43:49,301 DEBUG anaconda: network: GUI, not adding connection 8dc5662e-3671-4304-8adc-70ff92901273 of unsupported type 22:45:04,718 DEBUG anaconda: Entered spoke: EULAspoke 22:45:34,702 DEBUG anaconda: Left spoke: EULAspoke [root@bogon test]# cat /tmp/anaconda.log -n 1 22:43:36,505 DEBUG anaconda: setting locale to: zh_CN.UTF-8 2 22:43:36,719 WARN anaconda: /usr/lib/python2.7/site-packages/meh/ui/gui.py:23: PyGIWarning: Gtk was imported without specifying a version first. Use gi.require_version('Gtk', '3.0') before import to ensure that the right version gets loaded. 3 from gi.repository import Gtk 4 5 22:43:37,349 DEBUG anaconda: Detected primary monitor: 1280x768 95dpix 96dpiy 6 22:43:37,365 DEBUG anaconda: Entered hub: InitialSetupMainHub 7 22:43:37,519 DEBUG anaconda: Dump missing interfaces in NetworkControlBox initialize method 8 22:43:37,825 DEBUG anaconda: network: GUI, device configuration added: connection 754631f2-1fb0-4781-bf1e-d0f9ae274034 device ens33 9 22:43:37,838 DEBUG anaconda: network: GUI, not adding connection 754631f2-1fb0-4781-bf1e-d0f9ae274034, already in list 10 22:43:38,031 ERR anaconda: Initialization controller for hub InitialSetupMainHub expected but missing. 11 22:43:48,848 DEBUG anaconda: network: GUI, not adding virbr0 12 22:43:49,199 DEBUG anaconda: network: GUI, not adding virbr0 13 22:43:49,237 DEBUG anaconda: network: can't find connection with uuid 8dc5662e-3671-4304-8adc-70ff92901273 14 22:43:49,251 DEBUG anaconda: network: can't find connection with uuid 8dc5662e-3671-4304-8adc-70ff92901273 15 22:43:49,265 DEBUG anaconda: network: can't find connection with uuid 8dc5662e-3671-4304-8adc-70ff92901273 16 22:43:49,283 DEBUG anaconda: network: can't find connection with uuid 8dc5662e-3671-4304-8adc-70ff92901273 17 22:43:49,301 DEBUG anaconda: network: can't find connection with uuid 8dc5662e-3671-4304-8adc-70ff92901273 18 22:43:49,301 DEBUG anaconda: network: GUI, not adding connection 8dc5662e-3671-4304-8adc-70ff92901273 of unsupported type 19 22:45:04,718 DEBUG anaconda: Entered spoke: EULAspoke 20 22:45:34,702 DEBUG anaconda: Left spoke: EULAspoke [root@bogon test]# ``` ## more more 命令用于查看纯文本文件(内容较多的),格式为“more [选项]文件”。 因此对于长篇的文本内容,推荐使用 more 命令来查看。more命令会在最下面使用百分比的形式来提示您已经阅读了多少内容。您还可以使用空格键或回车键向下翻页 ```linux [root@bogon test]# more /tmp/anaconda.log 22:43:36,505 DEBUG anaconda: setting locale to: zh_CN.UTF-8 22:43:36,719 WARN anaconda: /usr/lib/python2.7/site-packages/meh/ui/gui.py:23: PyGIWarning: Gtk was imported without specifying a version first. Use gi.require_version('Gtk', '3.0') before import to ensure that the right version gets loaded. from gi.repository import Gtk 22:43:37,349 DEBUG anaconda: Detected primary monitor: 1280x768 95dpix 96dpiy 22:43:37,365 DEBUG anaconda: Entered hub: InitialSetupMainHub 22:43:37,519 DEBUG anaconda: Dump missing interfaces in NetworkControlBox initialize method 22:43:37,825 DEBUG anaconda: network: GUI, device configuration added: connection 754631f2-1fb0-4781-bf1e-d0f9ae274034 device ens33 22:43:37,838 DEBUG anaconda: network: GUI, not adding connection 754631f2-1fb0-4781-bf1e-d0f9ae274034, already in list 22:43:38,031 ERR anaconda: Initialization controller for hub InitialSetupMainHub expected but missing. 22:43:48,848 DEBUG anaconda: network: GUI, not adding virbr0 22:43:49,199 DEBUG anaconda: network: GUI, not adding virbr0 22:43:49,237 DEBUG anaconda: network: can't find connection with uuid 8dc5662e-3671-4304-8adc-70ff92901273 22:43:49,251 DEBUG anaconda: network: can't find connection with uuid 8dc5662e-3671-4304-8adc-70ff92901273 22:43:49,265 DEBUG anaconda: network: can't find connection with uuid 8dc5662e-3671-4304-8adc-70ff92901273 22:43:49,283 DEBUG anaconda: network: can't find connection with uuid 8dc5662e-3671-4304-8adc-70ff92901273 22:43:49,301 DEBUG anaconda: network: can't find connection with uuid 8dc5662e-3671-4304-8adc-70ff92901273 22:43:49,301 DEBUG anaconda: network: GUI, not adding connection 8dc5662e-3671-4304-8adc-70ff92901273 of unsupported type 22:45:04,718 DEBUG anaconda: Entered spoke: EULAspoke ``` ## head head 命令用于查看纯文本文档的前 N 行,格式为“head [选项] [文件]”。 在阅读文本内容时,谁也难以保证会按照从头到尾的顺序往下看完整个文件。 ```linux [root@bogon test]# head -n 4 /tmp/anaconda.log 22:43:36,505 DEBUG anaconda: setting locale to: zh_CN.UTF-8 22:43:36,719 WARN anaconda: /usr/lib/python2.7/site-packages/meh/ui/gui.py:23: PyGIWarning: Gtk was imported without specifying a version first. Use gi.require_version('Gtk', '3.0') before import to ensure that the right version gets loaded. from gi.repository import Gtk [root@bogon test]# ``` ## tail tail 命令用于查看纯文本文档的后 N 行或持续刷新内容,格式为“tail [选项] [文件]”。 tail 命令最强悍的功能是可以持续刷新一个文件的内容,当想要实时查看最新日志文件时,这特别有用,此时的命令格式为“tail -f 文件名” ```linux [root@bogon test]# tail -n 1 /tmp/anaconda.log 22:45:34,702 DEBUG anaconda: Left spoke: EULAspoke [root@bogon test]# ``` ## wc wc 命令用于统计指定文本的行数、字数、字节数,格式为“wc [参数] 文本”。 常用参数: * l:只显示行数 * w:只显示单词数 * c:只显示字节数 ```linux [root@bogon test]# wc /tmp/anaconda.log 20 187 1805 /tmp/anaconda.log [root@bogon test]# wc /tmp/anaconda.log -l 20 /tmp/anaconda.log [root@bogon test]# wc /tmp/anaconda.log -w 187 /tmp/anaconda.log [root@bogon test]# wc /tmp/anaconda.log -c 1805 /tmp/anaconda.log [root@bogon test]# ``` ## stat stat 命令用于查看文件的具体存储信息和时间等信息,格式为“stat 文件名称”。 stat 命令可以用于查看文件的存储信息和时间等信息,命令 stat filename 会显示出文件的三种时间状态:Access、Modify、Change。 ```linux [root@bogon test]# stat /tmp/anaconda.log 文件:"/tmp/anaconda.log" 大小:1805 块:8 IO 块:4096 普通文件 设备:fd00h/64768d Inode:17736546 硬链接:1 权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root) 环境:system_u:object_r:tmp_t:s0 最近访问:2024-12-28 22:47:45.313989842 +0800 最近更改:2024-12-27 22:45:34.701999126 +0800 最近改动:2024-12-27 22:45:34.701999126 +0800 创建时间:- [root@bogon test]# ``` ## cut cut 命令用于按“列”提取文本字符,格式为“cut [参数] 文本”。 在 Linux 系统中,如何准确地提取出最想要的数据,一般而言,按基于“行”的方式来提取数据是比较简单的,只需要设置好要搜索的关键词即可。但是如果按列搜索,不仅要使用-f 参数来设置需要看的列数,还需要使用-d 参数来设置间隔符号。 passwd 在保存用户数据信息时,用户信息的每一项值之间是采用冒号来间隔的,接下来我们使用下述命令尝试提取出 passwd 文件中的用户名信息,即提取以冒号(:)为间隔符号的第一列内容 ```linux [root@bogon test]# 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 [root@bogon test]# cut -d : -f 1 /etc/passwd root bin daemon adm lp ``` ## diff diff 命令用于比较多个文本文件的差异,格式为“diff [参数] 文件”。 在使用 diff 命令时,不仅可以使用--brief 参数来确认两个文件是否不同,还可以使用-c参数来详细比较出多个文件的差异之处, ```linux [root@bogon test]# cat ./diff_a.txt xbxaq.com hackeredu 666 x b x a q . c o m [root@bogon test]# cat ./diff_b.txt hackersrc and hackeredu [root@bogon test]# diff --brief diff_a.txt diff_b.txt 文件 diff_a.txt 和 diff_b.txt 不同 [root@bogon test]# diff -c diff_a.txt diff_b.txt *** diff_a.txt 2024-12-28 23:01:34.388984282 +0800 --- diff_b.txt 2024-12-28 23:02:03.129984090 +0800 *************** *** 1,10 **** ! xbxaq.com hackeredu 666 ! x ! b ! x ! a ! q ! . ! c ! o ! m --- 1 ---- ! hackersrc and hackeredu [root@bogon test]# ```
毛林
2025年9月12日 20:44
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
PDF文档(打印)
分享
链接
类型
密码
更新密码