🔥个人主页:胡萝卜3.0

📖个人专栏:  《C语言》、《数据结构》 、《C++干货分享》、LeetCode&牛客代码强化刷题

《Linux系统编程》

⭐️人生格言:不试试怎么知道自己行不行


🎥胡萝卜3.0🌸的简介:


目录

前情提示

一、Linux文件操作实战:文本查看与编辑工具

1.1 简易文本编辑器:nano

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

1.3 文件的倒序输出:tac 命令

1.4 more&less:分页查看大文件

1.4.1 more:只能前进不能后退

1.4.2 less:可进可退,更实用

1.5 查看文件开头与结尾:head 和 tail

1.5.1 head:查看文件开头若干行

1.5.2 tail:查看文件末尾若干行

1.6 查看文件的中间部分

1.6.1 管道

二. 时间与日历:date & cal

2.1 date:查看与设置系统时间

2.2 cal:查看公历日历

三. 搜索工具:精准定位文件与内容

3.1 which

3.2 高级文件搜索:find 命令

3.3 查找程序文件:whereis 命令 

3.4 文本搜索与过滤:grep

3.5 动态监控系统状态:top

四、文件压缩与解压:zip & unzip

4.1 压缩:zip

4.1.1 压缩普通文件

4.1.2 压缩目录

4.2 解压缩:unzip

4.3 .zip压缩包怎么解决跨平台问题

结尾


前情提示

一、Linux文件操作实战:文本查看与编辑工具

1.1 简易文本编辑器:nano

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

  • nano 文件名  ——向文件做写入操作
[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 不输出多行空行(也就是没有连续的空行)

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

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 文件名 ——将文件中的内容倒序写入显示器上
[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 向下查看(不能向上查看)
[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

/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:退出
[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行)
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行)
tail 文件名 默认查看文件的最后10行至标准输出中
[root@VM-0-16-centos lesson4]# tail temp.txt
hello 1991
hello 1992
hello 1993
hello 1994
hello 1995
hello 1996
hello 1997
hello 1998
hello 1999
hello 2000

加上-n<行数>选项 查看文件最后n行至标注输出中 
[root@VM-0-16-centos lesson4]# tail -n10 temp.txt
hello 1991
hello 1992
hello 1993
hello 1994
hello 1995
hello 1996
hello 1997
hello 1998
hello 1999
hello 2000

-n<行数>中的n可以省略,直接表明查看的行数 
[root@VM-0-16-centos lesson4]# tail -5 temp.txt
hello 1996
hello 1997
hello 1998
hello 1999
hello 2000

1.6 查看文件的中间部分

ok,现在我们可以查看文件的开头和结尾的若干行,那如果此时我想查看文件中间部分的若干行呢?该怎么办

比如我现在想查看文件的[1001,1010]之间的部分——

[root@VM-0-16-centos lesson4]# head -1011 temp.txt > tmp.txt
[root@VM-0-16-centos lesson4]# tail -10 tmp.txt
hello 1001
hello 1002
hello 1003
hello 1004
hello 1005
hello 1006
hello 1007
hello 1008
hello 1009
hello 1010

我们可以先将大文件截断重定向放入临时小文件中,然后用tail查看临时小文件的末尾若干行

但是,这样写不知道有没有uu感觉到有那么一点麻烦,ok,那此时我们就需要补充点理论知识了

1.6.1 管道
[root@VM-0-16-centos lesson4]# head -1011 temp.txt | tail -10
hello 1001
hello 1002
hello 1003
hello 1004
hello 1005
hello 1006
hello 1007
hello 1008
hello 1009
hello 1010

如果我们按照上面这种写法,我们可以得到相同的写法,这是为什么?

😯,既然我们了解了管道,那我们再来看一下这里是怎么在这个管道中传输的——

所以这里就是——

总结一下:

我们将这种单向传递的,具有出口和入口,传递资源的文件叫做管道文件

那此时我就可以通过管道把多条命令级联式的组合起来,流水式的对文件的输出结果进行加工处理!!!

[root@VM-0-16-centos lesson4]# head -1001 temp.txt | tail -20
hello 981
hello 982
hello 983
hello 984
hello 985
hello 986
hello 987
hello 988
hello 989
hello 990
hello 991
hello 992
hello 993
hello 994
hello 995
hello 996
hello 997
hello 998
hello 999
hello 1000
[root@VM-0-16-centos lesson4]# head -1001 temp.txt | tail -20 | wc -l
20

wc :word count ——计数   -l 按行计数

既然有了匿名管道,Linux中就有命名管道,在Linux中我们把以p开头的文件称为管道文件

使用 mkfifo 命令 创建管道文件

语法:mkfifo  管道文件名

命名管道和匿名管道的用法和特点是一样的!!!

二. 时间与日历:date & cal

2.1 date:查看与设置系统时间

指定格式显示时间:

  • date +%Y : %m : %d 

  • 在显示方面,使用者可以设定欲显示的格式,格式设定为一个 + 后接数个标记,其中常用的标记列表如下

  • 在设定时间方面

  • 时间戳

我们可以查看一下时间戳——

[root@VM-0-16-centos lesson4]# date +%s
1766144727

但是这个时间戳的可读性不是很高,我们可以转换成一个可读性高的——

[root@VM-0-16-centos lesson4]# date +%Y:%m:%d:%H:%M:%S -d @0
1970:01:01:08:00:00

那时间戳有什么用处呢?

时间在计算机中很重要,因为大型软件在运行过程中都要携带日志,用日志来纪录这个软件的健康情况,日志必带时间,这就形成了一个闭环~

Linux系统日志一般是在/var/log/下面——

这样我们就可以配合tail指令对日志进行查看,如果一个大型软件出现了问题,我们就可以使用tail去查看日志倒数几行,从而发现问题——

实际案例

# 显示常规时间
[root@VM-4-4-centos lesson4]# date
Mon Oct 13 14:08:32 CST 2025
[root@VM-4-4-centos lesson4]# date +%Y/%m/%d
2025/10/13
[root@VM-4-4-centos lesson4]# date +%Y/%m/%d-%H:%M:%S
2025/10/13-14:09:30

#显示时间戳
[root@VM-4-4-centos lesson4]# date +%s
1760335789

# 时间戳转成可视时间
[root@VM-4-4-centos lesson4]# date +%Y/%m/%d-%H:%M:%S -d @0
1970/01/01-08:00:00
[root@VM-4-4-centos lesson4]# date +%Y/%m/%d-%H:%M:%S -d @100000
1970/01/02-11:46:40

2.2 cal:查看公历日历

cal命令可以用来显示公历(阳历) 日历。公历是现在国际通用的历法,又称格列历,通称阳历。“阳历"又名"太阳历”,系以地球绕行太阳一周为一年,为西方各国所通用,故又名"西历"

功能:用于查看日历等时间信息,如只有一个参数,则表示年份(1-9999),如有两个参数,则表示月份和年份
常用选项

  • -3 显示系统前一个月,当前月,下一个月的月历
  • -j 显示在当年中的第几天(一年日期按天算,从1月1号算起,默认显示当前月在一年中的天数)
  • -y 显示当前年份的日历
# 常规样例
[root@VM-4-4-centos lesson4]# cal
    October 2025    
Su Mo Tu We Th Fr Sa
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31


[root@VM-4-4-centos lesson4]# cal 1949
                               1949                               

       January               February                 March       
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                   1          1  2  3  4  5          1  2  3  4  5
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    6  7  8  9 10 11 12
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   13 14 15 16 17 18 19
16 17 18 19 20 21 22   20 21 22 23 24 25 26   20 21 22 23 24 25 26
23 24 25 26 27 28 29   27 28                  27 28 29 30 31
30 31
        April                   May                   June        
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                1  2    1  2  3  4  5  6  7             1  2  3  4
 3  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 11
10 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 18
17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25
24 25 26 27 28 29 30   29 30 31               26 27 28 29 30

        July                  August                September     
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                1  2       1  2  3  4  5  6                1  2  3
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    4  5  6  7  8  9 10
10 11 12 13 14 15 16   14 15 16 17 18 19 20   11 12 13 14 15 16 17
17 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 24
24 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 30
31
       October               November               December      
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                   1          1  2  3  4  5                1  2  3
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31
30 31

[root@VM-4-4-centos lesson4]# cal -3
   September 2025         October 2025          November 2025   
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6            1  2  3  4                     1
 7  8  9 10 11 12 13   5  6  7  8  9 10 11   2  3  4  5  6  7  8
14 15 16 17 18 19 20  12 13 14 15 16 17 18   9 10 11 12 13 14 15
21 22 23 24 25 26 27  19 20 21 22 23 24 25  16 17 18 19 20 21 22
28 29 30              26 27 28 29 30 31     23 24 25 26 27 28 29
                                            30   

三. 搜索工具:精准定位文件与内容

3.1 which

which 只能在系统的指定的路径下去搜索命令,一般在/usr/bin/下面搜索。

前面已经介绍过which,这里就不过于介绍了!

但是如果我想在系统当中全局式的查找对应的文件,用什么命令呢?那么我们就可以使用find命令

3.2 高级文件搜索:find 命令

功能:

  • Linux下find命令在目录结构中搜索文件,并执行指定的操作
  • 用于在文件树中查找文件,并做出相应的处理

语法:

  • find pathname -options

常用选项:

  • -name 按照文件名查找文件 
  • 其余选项需要的时候自行查找
find pathname -name name

在家目录中搜索文件名为test.txt的文件
[root@VM-0-16-centos lesson4]# find ~ -name test.txt
/root/118/lesson4/test.txt

3.3 查找程序文件:whereis 命令 

功能:

  • 在系统指定的路径下面查找,只会查找命令所在的路径,命令的安装包,软件安装所对应的文档(手册)

语法:

  • whereis xxxx
[root@VM-0-16-centos lesson4]# whereis pwd
pwd: /usr/bin/pwd /usr/include/pwd.h /usr/share/man/mann/pwd.n.gz /usr/share/man/man1/pwd.1.gz /usr/share/man/man1p/pwd.1p.gz

3.4 文本搜索与过滤:grep

语法:

  • grep [选项] 搜寻字符串 文件

功能:

  • 在文件中搜索字符串,将找到的行打印出来

常用选项:

  • -i :忽略大小的不同,将大小写视为相同
  • -n :顺便输出行号(输出的行号为输出的行在原始文件中所对应的第几行)
  • -v :反向选择,现实出没有“搜寻字符串”关键字内的那一行(没有包含关键词的行保留下来)

注意:如果grep 不加-i 选项,默认是不忽略大小写,严格匹配!!!

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

hello 
grep 后面不加-i 默认不忽略大小写,严格匹配
[root@VM-0-16-centos lesson4]# grep 'bit' test.txt
hellp bit
hello bit
加了-i 忽略大小写
[root@VM-0-16-centos lesson4]# grep -i 'bit' test.txt
hellp bit
hello bit
hello BIT
hello Bit
hello BIt
hello bIT
-n 顺便输出在源文件中对应的行号
[root@VM-0-16-centos lesson4]# grep -n 'bit' test.txt
2:hellp bit
5:hello bit
-v 反向输出
[root@VM-0-16-centos lesson4]# grep -v 'bit' test.txt
hello carrot
hello world
hello hi
hello BIT
hello Bit
hello BIt
hello bIT

hello 

[root@VM-0-16-centos lesson4]# grep -ni 'bit' test.txt
2:hellp bit
5:hello bit
6:hello BIT
7:hello Bit
8:hello BIt
9:hello bIT

grep 是一个对文本内容进行行过滤工具,将包含关键词的行写入到显示器文件中,其余的过滤!!

有了grep,我们就可以实现下面的操作——

[root@VM-0-16-centos lesson4]# cat temp.txt | grep '99'
hello 99
hello 199
hello 299
hello 399
hello 499
hello 599
hello 699
hello 799
hello 899
hello 990

但是这个是不是有点杀鸡用牛刀!

其实使用上面的操作,我们就可以对日志进行监控,查看系统的进程错误,分析文件信息!!!

3.5 动态监控系统状态:top

top就类似于windows中的任务管理器~

top -d 1 -n 5
-d: 刷新的时间间隔
-n: 刷新的次数
q:退出

四、文件压缩与解压:zip & unzip

再看这块内容之前,我们先来看一下打包和压缩——

打包和压缩是不同的两个动作:

  • 打包是多个文件变成一个文件,而压缩是文件大变小的过程(由算法实现的)

但是在这里我们把打包和压缩看成一套动作

那为什么我们要打包和压缩呢?

这就和网络场景有关了,在网络环境中,如果不进行打包和压缩,我们就会出现:

  • 丢失文件
  • 效率的问题

打包将文件多变一,可以防止文件丢失,要么都传,要么都丢失

压缩可以提高效率,用算法来换时间

打包压的方式有很多种,我们可以通过看压缩包的后缀来看压缩的方式(这里介绍两种打包压缩的方式)——

  • 压缩:.zip
  • 打包:.tgz(== . tar.gz)

ok,我们先来介绍压缩,下一篇博客中会介绍打包——

4.1 压缩:zip

语法:

  • zip 压缩文件.zip  目录或者文件

功能:

  • 将目录或者文件压缩成zip格式
4.1.1 压缩普通文件

语法:

  • zip 压缩文件.zip  普通文件  (.zip不能丢!!!)
[root@VM-0-16-centos lesson4]# zip test.zip test.txt

我们将test.txt文件压缩成test.zip压缩包

[root@VM-0-16-centos lesson4]# zip test.zip test.txt
  adding: test.txt (deflated 49%)
[root@VM-0-16-centos lesson4]# ll
total 44
prw-r--r-- 1 root root     0 Dec 19 19:30 fifo
-rw-r--r-- 1 root root 20901 Dec 19 17:35 temp.txt
-rw-r--r-- 1 root root   102 Dec 19 20:18 test.txt
-rw-r--r-- 1 root root   218 Dec 19 20:57 test.zip
-rw-r--r-- 1 root root 10011 Dec 19 18:51 tmp.txt
4.1.2 压缩目录

语法:

  • zip  -r  压缩文件.zip  目录  (-r 选项表示递归式压缩,.zip不能丢!!!)
[root@VM-0-16-centos 118]# zip -r lessom4.zip lesson4

将lesson4目录压缩成lesson4.zip,递归式压缩

压缩完成之后,会在当前所在的路径下面生成一个.zip 的压缩包

总结:

zip [-r] dst.zip src
4.2 解压缩:unzip

解压缩普通文件/目录:

  • unzip xxxx.zip   (这种写法,默认解压到当前路径下(当前所的路径)
  • unzip xxxx.zip  -d  指定路径  (-d  指定路径  指定路径解压压缩包) 
在当前路径解压缩目录
[root@VM-0-16-centos 118]# unzip lesson4.zip
在当前路径解压缩普通文件
[root@VM-0-16-centos lesson4]# unzip test.zip
指定路径解压缩 -d .. 在上级目录中解压缩
[root@VM-0-16-centos lesson4]# unzip test.zip -d ..

注意:zip 和 unzip 不是相同自带的,需要下载!

yum install zip unzip
4.3 .zip压缩包怎么解决跨平台问题

我们先在这里的这块部分解决怎么在windows和Linux中互传压缩包的问题——

Linux中的压缩包传给windows中:

  • 先在Linux中进行压缩操作,然后执行:sz  xx.zip
[root@VM-0-16-centos 118]# sz lesson4.zip

windows中的压缩包传给Linux中:

  1. 可以直接拖拽
  2. rz -E
[root@VM-0-16-centos 118]# rz -E

注意:sz 和 rz 在Linux中默认是没有的,需要安装!!!

yum install -y lrzsz
apt install -y lrzsz

结尾

uu们,本文的内容到这里就全部结束了,再次感谢您的阅读!

结语:希望对学习Linux相关内容的uu有所帮助,不要忘记给博主“一键三连”哦!

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐