news 2026/6/15 20:03:25

Linux文件操作与系统管理全指南:从文本处理到高效搜索与压缩

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Linux文件操作与系统管理全指南:从文本处理到高效搜索与压缩
一、Linux文件操作实战:文本查看与编辑工具
1.1 简易文本编辑器:nano

通过前面的学习,我么可以通过echo重定向向文件中写入相应的字符串,但是这种方式还是比较慢的,我们可以通过nano 来快速的通过键盘向一个文件做写入操作——

  • nano 文件名 ——向文件做写入操作

代码语言:javascript

AI代码解释

[root@VM-0-16-centos lesson4]# nano test.txt

向test.txt文件做写入操作

若完成写入操作,按ctrl+x,此时会显示是否保存你写入的东西,y:保存,n:不保存,ctrl + c:取消——

按 y 保存之后,按enter键完成写入操作!!!

那我们怎么查看这个test.txt文件中的内容呢?

ok,在前面的学习中我们简单了解了 cat 指令,现在我们正式来学习一下——

1.2 按原样阅读:cat 正序查看文件

语法:cat [选项] [文件]

功能:正序查看目标文件中的内容(将查看到的内容正序写入到显示器上)

常用选项:

  • -b 对非空输出行编号,空行不做编号
  • -n 对输出的所有行编号
  • -s 不输出多行空行(也就是没有连续的空行)

应用场景:查看一些小文件(小算法、小配置文件、很短的代码……)

代码语言:javascript

AI代码解释

cat 文件名(正序查看文件中的内容,将文件中的内容写入显示器上) [root@VM-0-16-centos lesson4]# cat test.txt hello carrot hellp bit hello world hello hi cat -b 文件名(正序查看文件中的内容,对输出的非空行编号,非空行不编号) [root@VM-0-16-centos lesson4]# cat -b test.txt 1 hello carrot 2 hellp bit 3 hello world 4 hello hi cat -n 文件名(正序查看文件中的内容,对输出的所有行编号) [root@VM-0-16-centos lesson4]# cat -n test.txt 1 hello carrot 2 hellp bit 3 hello world 4 hello hi 5 [root@VM-0-16-centos lesson4]# cat -n test.txt 1 hello carrot 2 hellp bit 3 hello world 4 hello hi 5 6 7 hello test.txt文件中有多行空行 cat -s 文件名(正序查看文件中的内容,输出结果不会出现连续的空行) [root@VM-0-16-centos lesson4]# cat -s test.txt hello carrot hellp bit hello world hello hi hello 选项可以叠加使用 [root@VM-0-16-centos lesson4]# cat -ns test.txt 1 hello carrot 2 hellp bit 3 hello world 4 hello hi 5 6 hello

注意:这里的 cat 文件名 和我们之前学习的 cat < 文件名 是不一样的(后面会说原因)!!!

那既然我们可以正序查看文件中的内容,那是不是也可以倒序查看文件的内容——

1.3 文件的倒序输出:tac 命令
  • tac 文件名 ——将文件中的内容倒序写入显示器上

代码语言:javascript

AI代码解释

[root@VM-0-16-centos lesson4]# cat test.txt hello carrot hellp bit hello world hello hi hello [root@VM-0-16-centos lesson4]# tac test.txt hello hello hi hello world hellp bit hello carrot

tac和cat是一样的,只能查看小文件中的内容,按如果系统中有大文件,还用cat来查看吗?ok,此时cat就不适合了

那此时我们就要使用新的指令的——more或者less

1.4 more&less:分页查看大文件
1.4.1 more:只能前进不能后退

功能:类似于cat,但是可以分页查看,而且还可以直接指定查看的行数

语法:more [选项] 文件名

常用选项:

  • -n 指定输出行数
  • q 退出more
  • enter 向下查看(不能向上查看)

代码语言:javascript

AI代码解释

[root@VM-0-16-centos lesson4]# i=0; while [ $i -le 2000 ]; do echo "hello $i"; let i++; done > temp.txt more 文件名 按enter逐行查看 [root@VM-0-16-centos lesson4]# more temp.txt hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9 hello 10 hello 11 hello 12 hello 13 hello 14 hello 15 hello 16 hello 17 hello 18 hello 19 hello 20 hello 21 hello 22 hello 23 hello 24 hello 25 hello 26 hello 27 hello 28 more -n 文件名 直接查看前n行 [root@VM-0-16-centos lesson4]# more -10 temp.txt hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9
  • 在more指令的结果中的搜索——/xxxx

会输出多行结果,其中一行为xxxx

代码语言:javascript

AI代码解释

/66 ...skipping hello 64 hello 65 hello 66 hello 67 hello 68 hello 69 hello 70 hello 71 hello 72
1.4.2 less:可进可退,更实用

功能:

  • less和more类似,但是less可以随意浏览文件(上下控制),而more只能向下移动,不能向上移动,less在查看时不会加载整个文件

语法:

  • less [选项] 文件

常用选项:

  • -i 忽略搜索时的大小写
  • -N 显示每行的行号
  • /字符串:向下搜索“字符串”的功能
  • ?字符串:向上搜索“字符串”的功能
  • n:重复前一个搜索(与/或者?有关)
  • N:反向重复前一个搜索(与/或者?有关)
  • q:退出

代码语言:javascript

AI代码解释

[root@VM-0-16-centos lesson4]# less temp.txt hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9 hello 10 hello 11 hello 12 hello 13 hello 14 hello 15 hello 16 hello 17 hello 18 hello 19 hello 20 hello 21 hello 22 hello 23 hello 24 hello 25 hello 26 hello 27 hello 28 -N 输出行号 [root@VM-0-16-centos lesson4]# less -N temp.txt 1 hello 0 2 hello 1 3 hello 2 1 hello 0 2 hello 1 3 hello 2 4 hello 3 5 hello 4 6 hello 5 7 hello 6 8 hello 7 9 hello 8 10 hello 9

那如果我只是想查看大文件的开头和结尾的若干行的呢?

ok,那这时就该用到head和tail——

1.5 查看文件开头与结尾:head 和 tail
1.5.1 head:查看文件开头若干行

功能:

  • head用来显示文件的开头到标准输出中,head默认查看大文件的开头前10行(不加选项)

语法:

  • head [选项] 文件名

选项:

  • -n<行数> 显示文件开头n行(head -n10 文件名 显示文件的前10行)

代码语言:javascript

AI代码解释

head默认输出前10行 [root@VM-0-16-centos lesson4]# head temp.txt hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9 head -n4 temp.txt 输出前4行 [root@VM-0-16-centos lesson4]# head -n4 temp.txt hello 0 hello 1 hello 2 hello 3 选项的n可以省略,直接表明输出的行数 head -5 temp.txt [root@VM-0-16-centos lesson4]# head -5 temp.txt hello 0 hello 1 hello 2 hello 3 hello 4
1.5.2 tail:查看文件末尾若干行

功能:

  • tail用来显示文件的末尾到标准输出中,head默认查看大文件的末尾后10行(不加选项)

语法:

  • tail [选项] 文件名

选项:

  • -n<行数> 显示文件的最后n行(tail -n10 文件名 显示文件的最后10行)

https://www.dongchedi.com/article/7592746589790290456
https://www.dongchedi.com/article/7592746855097221656
https://www.dongchedi.com/article/7592746675454820926
https://www.dongchedi.com/article/7592748365323354648
https://www.dongchedi.com/article/7592746719646204478
https://www.dongchedi.com/article/7592747640958272062
https://www.dongchedi.com/article/7592749678932918808
https://www.dongchedi.com/article/7592746867159925273
https://www.dongchedi.com/article/7592745331981730329
https://www.dongchedi.com/article/7592745446175719961
https://www.dongchedi.com/article/7592753637567431230
https://www.dongchedi.com/article/7592749105718608446
https://www.dongchedi.com/article/7592747850602168894
https://www.dongchedi.com/article/7592751089959928344
https://www.dongchedi.com/article/7592749677783630398
https://www.dongchedi.com/article/7592748937338487358
https://www.dongchedi.com/article/7592751861838037528
https://www.dongchedi.com/article/7592748390052692542
https://www.dongchedi.com/article/7592748902508937790
https://www.dongchedi.com/article/7592752692859273753
https://www.dongchedi.com/article/7592752407277486616
https://www.dongchedi.com/article/7592751306877010494
https://www.dongchedi.com/article/7592751583336104473
https://www.dongchedi.com/article/7592753001002254873
https://www.dongchedi.com/article/7592742783815827993
https://www.dongchedi.com/article/7592746107613708825
https://www.dongchedi.com/article/7592745316932600382
https://www.dongchedi.com/article/7592742556312748569
https://www.dongchedi.com/article/7592744391702266392
https://www.dongchedi.com/article/7592744697026953752
https://www.dongchedi.com/article/7592743124330529304
https://www.dongchedi.com/article/7592746088361165337
https://www.dongchedi.com/article/7592741588309656089
https://www.dongchedi.com/article/7592744567833870873
https://www.dongchedi.com/article/7592745982770004504
https://www.dongchedi.com/article/7592739924609647166
https://www.dongchedi.com/article/7592743902273389081
https://www.dongchedi.com/article/7592744958071702040
https://www.dongchedi.com/article/7592743141112365593
https://www.dongchedi.com/article/7592743666985484824
https://www.dongchedi.com/article/7592744049409851928
https://www.dongchedi.com/article/7592744024860574270
https://www.dongchedi.com/article/7592742696129806873
https://www.dongchedi.com/article/7592743608927715865
https://www.dongchedi.com/article/7592742856737964606
https://www.dongchedi.com/article/7592691833533465112
https://www.dongchedi.com/article/7592690193359340056
https://www.dongchedi.com/article/7592693300055228990
https://www.dongchedi.com/article/7592684971291460121
https://www.dongchedi.com/article/7592679390245388825
https://www.dongchedi.com/article/7592685439086559768
https://www.dongchedi.com/article/7592678022445531673
https://www.dongchedi.com/article/7592684522227958334
https://www.dongchedi.com/article/7592724981638054424
https://www.dongchedi.com/article/7592729300793819673
https://www.dongchedi.com/article/7592685814329967166
https://www.dongchedi.com/article/7592727491421504062
https://www.dongchedi.com/article/7592728878532035134
https://www.dongchedi.com/article/7592726559455560216
https://www.dongchedi.com/article/7592728806767657496
https://www.dongchedi.com/article/7592724487658242584
https://www.dongchedi.com/article/7592723518032101912
https://www.dongchedi.com/article/7592753001002254873

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/15 14:08:39

Sunshine游戏串流革命:揭秘多设备无缝游戏体验

Sunshine游戏串流革命&#xff1a;揭秘多设备无缝游戏体验 【免费下载链接】Sunshine Sunshine: Sunshine是一个自托管的游戏流媒体服务器&#xff0c;支持通过Moonlight在各种设备上进行低延迟的游戏串流。 项目地址: https://gitcode.com/GitHub_Trending/su/Sunshine …

作者头像 李华
网站建设 2026/6/15 13:06:52

光学材料数据库完整使用指南:免费获取3000+材料光学参数

光学材料数据库完整使用指南&#xff1a;免费获取3000材料光学参数 【免费下载链接】refractiveindex.info-database Database of optical constants 项目地址: https://gitcode.com/gh_mirrors/re/refractiveindex.info-database 还在为光学设计项目寻找准确的折射率数…

作者头像 李华
网站建设 2026/6/15 14:07:40

Revelation光影包终极指南:从安装配置到专业级渲染

Revelation光影包终极指南&#xff1a;从安装配置到专业级渲染 【免费下载链接】Revelation A realistic shaderpack for Minecraft: Java Edition 项目地址: https://gitcode.com/gh_mirrors/re/Revelation 为什么你的Minecraft需要这款光影包&#xff1f; 你是否曾觉…

作者头像 李华
网站建设 2026/6/15 14:01:31

魔兽争霸III插件优化完全指南:WarcraftHelper功能详解与配置教程

魔兽争霸III插件优化完全指南&#xff1a;WarcraftHelper功能详解与配置教程 【免费下载链接】WarcraftHelper Warcraft III Helper , support 1.20e, 1.24e, 1.26a, 1.27a, 1.27b 项目地址: https://gitcode.com/gh_mirrors/wa/WarcraftHelper 还在为魔兽争霸III在现代…

作者头像 李华
网站建设 2026/6/15 12:54:55

地理编码新选择:MGeo开源模型支持多场景中文地址识别

地理编码新选择&#xff1a;MGeo开源模型支持多场景中文地址识别 在地理信息处理、位置服务和城市计算等应用中&#xff0c;中文地址的标准化与相似度匹配一直是技术落地的关键挑战。由于中文地址存在表述多样、层级复杂、缩写习惯广泛等特点&#xff0c;传统基于规则或关键词…

作者头像 李华
网站建设 2026/6/15 13:32:29

AMD Ryzen调优终极指南:从新手到专家的完整实战手册

AMD Ryzen调优终极指南&#xff1a;从新手到专家的完整实战手册 【免费下载链接】SMUDebugTool A dedicated tool to help write/read various parameters of Ryzen-based systems, such as manual overclock, SMU, PCI, CPUID, MSR and Power Table. 项目地址: https://gitc…

作者头像 李华