今天记住了一个热键:CTRL+R,搜索历史指令,分享给大家
目录
一、文件内容查看
1.1 cat 与 tac :正序 / 倒序查看文件:tac反过来就是cat
1.3 more与less 分页查看大文件
1.4 head与tail
1.5 如果只显示中间某部分的内容:数据流转:管道(|)
二、时间与日历:date 与 cal
2.1 date:
2.2 时间戳:
2.3 cal :查看公历
三、搜索工具:
3.1 find :在目录树中遍历搜索文件
3.2 whereis :查找文件,找到文件的源
3.3 grep:在文件中搜索字符串
四、系统监控:top 查看进程动态
五、文件压缩与解压:zip 和 unzip
一、文件内容查看
1.1 cat 与 tac :正序 / 倒序查看文件:tac反过来就是cat
功能:查看目标文件内容(适合查小文件,如果是大文件可以用more和less,下文会说)
常用选项:
- -b 对非空输出行编号,空行不做编号
- -n 对输出的所有行编号
- -s 不输出多行空行
示例
ubuntu@VM-0-4-ubuntu:~$ echo "hello world" >> data.txt ubuntu@VM-0-4-ubuntu:~$ cat data.txt hello world ubuntu@VM-0-4-ubuntu:~$ echo "hello world" >> data.txt ubuntu@VM-0-4-ubuntu:~$ echo "hello" >> data.txt ubuntu@VM-0-4-ubuntu:~$ cat -n data.txt 1 hello world 2 hello world 3 hello ubuntu@VM-0-4-ubuntu:~$ cat data.txt hello world hello world hello world hello ubuntu@VM-0-4-ubuntu:~$ cat -n data.txt #-n 对所以行编号 1 hello world 2 hello world 3 4 5 6 7 8 hello world 9 hello ubuntu@VM-0-4-ubuntu:~$ cat -s data.txt #-s 将多行空行压缩为一行 hello world hello world hello world hello ubuntu@VM-0-4-ubuntu:~$ cat -n data.txt #-s不会对文本造成影响 1 hello world 2 hello world 3 4 5 6 7 8 hello world 9 hello ubuntu@VM-0-4-ubuntu:~$ cat -b data.txt #-b 则对非空行进行编号 1 hello world 2 hello world 3 hello world 4 hello #验证tac指令的功能 ubuntu@VM-0-4-ubuntu:~$ echo "1111" >> data.txt ubuntu@VM-0-4-ubuntu:~$ cat data.txt hello world hello world hello world hello 1111 ubuntu@VM-0-4-ubuntu:~$ tac data.txt 1111 #先打的最后一个!倒序输出的 hello hello world hello world hello world1.2 nano :linux下的记事本
功能:可以打开文件,相当于记事本的功能
语法:nano 文件名
示例:输入如下指令后可以打开data.txt,并且对该文本进行编辑
ubuntu@VM-0-4-ubuntu:~$ nano data.txt在下面的众多选项中^表示ctrl,同时按下CTRL和其他键可以达到不同的效果。这里我加了一些空行在里面,目的是为了验证1.1中的cat的其他扩展的功能。
常用快捷键:
- 保存文件:Ctrl + O(按下后按 Enter 确认保存)。
- 退出编辑器:Ctrl + X(若文件未保存,会提示是否保存,按 Y 确认,N 放弃)按enter退出
- 查找文本:Ctrl + W(输入关键词后按 Enter 查找,按 Alt + W 继续查找下一个)
- 剪切当前行:Ctrl + K
- 粘贴:Ctrl + U
- 撤销:Ctrl + _
1.3 more与less 分页查看大文件
more功能:会分页打印文件的内容,用/内容可以搜索文件内容,但是缺点是只能用回车下翻,不能回到上一页,比较麻烦。优点则是不会刷屏
常用选项:
- -n 指定输出行数
- -q 退出more
- /内容 :搜索(比如/hello)
- Enter 向下搜
示例:
ubuntu@VM-0-4-ubuntu:~$ more -20 data.txt #-20代表每页输出20行 hello world hello world hello world hello 1111 hello Lotso hello Lotso hello Lotso hello Lotso hello Lotso hello Lotso hello Lotso hello Lotso hello Lotso hello Lotso --More--(0%)less功能:相比于more可以上下翻动很好用,在开发中常用less其功能更加全面
常用选项:
i忽略搜索时的大小写-N显示每行的行号/字符串:向下搜索该字符串?字符串:向上搜索该字符串n:重复前一个搜索(与 / 或 ? 有关)N:反向重复前一个搜索(与 / 或 ?有关)q:退出
ubuntu@VM-0-4-ubuntu:~$ less -N temp.txt 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 11 hello 10 12 hello 11 13 hello 12 14 hello 13 15 hello 14 16 hello 15 17 hello 16 18 hello 17 19 hello 18 20 hello 19 21 hello 20 22 hello 21 23 hello 22 24 hello 23 25 hello 24 26 hello 25 27 hello 26 28 hello 27 29 hello 28 30 hello 29 13 hello 12 14 hello 13 15 hello 14 16 hello 15 17 hello 16 18 hello 17 19 hello 18 20 hello 19 21 hello 20 22 hello 21 23 hello 22 24 hello 23 25 hello 24 26 hello 25 27 hello 26 28 hello 27 29 hello 28 30 hello 29 31 hello 30 32 hello 31 33 hello 32 34 hello 33 35 hello 34 36 hello 35 37 hello 36 38 hello 37 39 hello 38 40 hello 39 41 hello 40 42 hello 41 43 hello 42 44 hello 43 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 11 hello 10 12 hello 11 13 hello 12 14 hello 13 15 hello 14 16 hello 15 17 hello 16 18 hello 17 19 hello 18 20 hello 19 21 hello 20 22 hello 21 23 hello 22 24 hello 23 25 hello 24 26 hello 25 27 hello 26 28 hello 27 29 hello 28 30 hello 29 31 hello 30 :注:这里只给出-N的演示,其他的搜索因为输入后消失了故不展示了
1.4 head与tail
head和tail的功能都非常简单,默认分别用于显示文件的前 10 行和后 10 行,示例如下:
ubuntu@VM-0-4-ubuntu:~$ head temp.txt #head默认显示前10行 hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9 ubuntu@VM-0-4-ubuntu:~$ head -n5 temp.txt #head -n5与head -5效果一样 hello 0 hello 1 hello 2 hello 3 hello 4 ubuntu@VM-0-4-ubuntu:~$ head -5 temp.txt hello 0 hello 1 hello 2 hello 3 hello 4 ubuntu@VM-0-4-ubuntu:~$ tail -5 temp.txt hello 1996 hello 1997 hello 1998 hello 1999 hello 2000如上例所展示的,如果想要显示前或后几行,就加上 -n
总结如下:
-n行数显示的行数(n可加可不加)- -f持续监听文件,文件新增内容会实时打印到终端
1.5 如果只显示中间某部分的内容:数据流转:管道(|)
现在比如要显示第200到210行的内容(本例从0开始的),只需要用head提取前210行,重定向到新文件,再用tail提取新文件就可以了:
ubuntu@VM-0-4-ubuntu:~$ head -210 temp.txt >>tempp.txt ubuntu@VM-0-4-ubuntu:~$ tail -11 tempp.txt hello 199 hello 200 hello 201 hello 202 hello 203 hello 204 hello 205 hello 206 hello 207 hello 208 hello 209 ubuntu@VM-0-4-ubuntu:~$这样的思想被总结后产生了管道的概念
管道符号:|
一句话核心:把左边命令的输出,直接当作右边命令的输入语法:
命令1 | 命令2
命令 1 正常会打印内容到屏幕(标准输出 stdout); 加管道|之后,不输出到屏幕,直接传给右边命令,作为右边的标准输入。
那么上面的示例也可以概况为一句命令:
ubuntu@VM-0-4-ubuntu:~$ head -210 temp.txt | tail -11 hello 199 hello 200 hello 201 hello 202 hello 203 hello 204 hello 205 hello 206 hello 207 hello 208 hello 209望文生义,计算机中的管道只能从左向右,同时,管道还可以连接,可以有多个管道,称为管道的级联
ubuntu@VM-0-4-ubuntu:~$ head -210 temp.txt | tail -11 | tac #倒序输出 hello 209 hello 208 hello 207 hello 206 hello 205 hello 204 hello 203 hello 202 hello 201 hello 200 hello 199 ubuntu@VM-0-4-ubuntu:~$ head -210 temp.txt | tail -11 |wc -l #wc -l显示行数 11匿名管道:像上面的没有名字的管道称为匿名管道
特点:
- 数据一旦被读走,缓冲区就清空,不能重复读
- 管道容量有限(一般几 KB),写满之后写操作会阻塞等待读;读空了读操作会阻塞等待写入
- 只能单向:A 写 → B 读;不能同时 A 读 B 写
- 只能父子 / 兄弟进程(亲缘进程)通信
命名管道:命名管道是存在文件系统里,有文件名,用 mkfifo 创建,可以实现一个或多个终端的连接
mkfifo myfifo ls -l myfifo- 有文件节点,
ls能看到 - 无亲缘进程之间也可以通信
- 同样是内存缓冲区,数据不落地磁盘
- 文件只是一个入口标识
二、时间与日历:date 与 cal
2.1 date:
这里直接给出示例:
ubuntu@VM-0-4-ubuntu:~$ date Mon Sep 28 04:50:25 PM CST 2026 #这种是外国的输出方式 ubuntu@VM-0-4-ubuntu:~$ date +%Y:%m:%d #进行了格式化,这里Y要大写,代表年,m和d代表月和日 2026:09:28 ubuntu@VM-0-4-ubuntu:~$ date +%Y/%m/%d #与上一个的区别只有划分的符号不同,这个就比较简单了 2026/09/28 ubuntu@VM-0-4-ubuntu:~$ date +%Y 2026 #也可以只显示年或月或日同时还可以显示时,分,秒
扩展如下:
%Y年份%m月份%d日期%H小时%M分钟%S秒%s时间戳- %F 相当于%Y-%m-%d
- %T 相当于%H:%M:%S
ubuntu@VM-0-4-ubuntu:~$ date +%F-%T # %F和%T间的-可以用其他符号代替但是不能为空 2026-09-28-16:58:09 ubuntu@VM-0-4-ubuntu:~$2.2 时间戳:
概念:从 1970-01-01 00:00:00 UTC(格林威治时间)到当前时刻的总秒数,也叫 Epoch 时间
作用是生成日志,日志可以用来排除错误 。
获取时间戳用 %s
ubuntu@VM-0-4-ubuntu:~$ date +%s 1790586197甚至可以指明时间戳的真实时间:date +"%F %T" -d @你获取的数字
ubuntu@VM-0-4-ubuntu:~$ date +%F-%T -d @1790586197 # 把刚刚拿到的时间戳转回可读时间 2026-09-28-17:03:17 ubuntu@VM-0-4-ubuntu:~$ date +%F-%T -d @0 1970-01-01-08:00:00 #可以看到时间是八点,因为使用了北京的东八区时间,加上了8小时2.3 cal :查看公历
非常简单,只需要输入cal即可
ubuntu@VM-0-4-ubuntu:~$ cal September 2026 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如果没法运行可以按照提示输入相关指令,比如:
sudo apt install ncal常用扩展:
-3显示系统前一个月,当前月,下一个月的月历-j显示在当年中的第几天(一年日期按天算,从1月1号算起,默认显示当前月在一年中的天数)-y显示当前年份的日历
ubuntu@VM-0-4-ubuntu:~$ cal -3 August 2026 September 2026 October 2026 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 #显示本月份每天为本年的第几天 ubuntu@VM-0-4-ubuntu:~$ cal -j September 2026 Su Mo Tu We Th Fr Sa 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 #显示全年日历 ubuntu@VM-0-4-ubuntu:~$ cal -y 2026 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 2 3 1 2 3 4 5 6 7 1 2 3 4 5 6 7 4 5 6 7 8 9 10 8 9 10 11 12 13 14 8 9 10 11 12 13 14 11 12 13 14 15 16 17 15 16 17 18 19 20 21 15 16 17 18 19 20 21 18 19 20 21 22 23 24 22 23 24 25 26 27 28 22 23 24 25 26 27 28 25 26 27 28 29 30 31 29 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 3 4 1 2 1 2 3 4 5 6 5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13 12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20 19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27 26 27 28 29 30 24 25 26 27 28 29 30 28 29 30 31 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 3 4 1 1 2 3 4 5 5 6 7 8 9 10 11 2 3 4 5 6 7 8 6 7 8 9 10 11 12 12 13 14 15 16 17 18 9 10 11 12 13 14 15 13 14 15 16 17 18 19 19 20 21 22 23 24 25 16 17 18 19 20 21 22 20 21 22 23 24 25 26 26 27 28 29 30 31 23 24 25 26 27 28 29 27 28 29 30 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 2 3 1 2 3 4 5 6 7 1 2 3 4 5 4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12 11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19 18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26 25 26 27 28 29 30 31 29 30 27 28 29 30 31还可以检查指定年 月的日历
ubuntu@VM-0-4-ubuntu:~$ cal 2024 #查看指定年的全部月 2024 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 2 3 4 5 6 1 2 3 1 2 7 8 9 10 11 12 13 4 5 6 7 8 9 10 3 4 5 6 7 8 9 14 15 16 17 18 19 20 11 12 13 14 15 16 17 10 11 12 13 14 15 16 21 22 23 24 25 26 27 18 19 20 21 22 23 24 17 18 19 20 21 22 23 28 29 30 31 25 26 27 28 29 24 25 26 27 28 29 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 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 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 3 4 5 6 1 2 3 1 2 3 4 5 6 7 7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14 14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21 21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28 28 29 30 31 25 26 27 28 29 30 31 29 30 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 2 3 4 5 1 2 1 2 3 4 5 6 7 6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14 13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21 20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28 27 28 29 30 31 24 25 26 27 28 29 30 29 30 31 #查看指定月份,注意月份在前 ubuntu@VM-0-4-ubuntu:~$ cal 3 2026 March 2026 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三、搜索工具:
3.1 find :在目录树中遍历搜索文件
语法:find pathname -options
find的扩展非常多,用到再查,需要记住的就是find name就可以
ubuntu@VM-0-4-ubuntu:~$ find temp.txt temp.txt总结:
- Linux下find命令在目录结构中搜索文件,并执行指定的操作。
- Linux下find命令提供了相当多的查找条件,功能很强大。由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解⼀下。
- 即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只你具有相应的权限。
- 在运行一个非常消耗资源的find命令时,很多⼈都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这⾥是指30G字节以上的文件系统)。
3.2 whereis :查找文件,找到文件的源
ubuntu@VM-0-4-ubuntu:~$ whereis ls ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz ubuntu@VM-0-4-ubuntu:~$ whereis temp.txt temp.txt: ubuntu@VM-0-4-ubuntu:~$ tree . ├── 1.txt ├── data.txt ├── snap │ └── tree │ ├── 54 │ ├── common │ │ └── marker_publisher_changed_announcement_shown │ └── current -> 54 ├── temp │ └── myfile.txt ├── temp1 ├── tempp.txt └── temp.txt 8 directories, 6 files ubuntu@VM-0-4-ubuntu:~$ whereis tree tree: /snap/bin/tree ubuntu@VM-0-4-ubuntu:~$通过上述的树可见,如果当前目录下即可找到whereis的文件,那么不会输出地址了,如果在其他分支则输出地址
3.3 grep:在文件中搜索字符串
示例:
ubuntu@VM-0-4-ubuntu:~$ grep "99" temp.txt #双引号中是要查找的东西 hello 99 hello 199 hello 299 hello 399 hello 499 hello 599 hello 699 hello 799 hello 899 hello 990 hello 991 hello 992 hello 993 hello 994 hello 995 hello 996 hello 997 hello 998 hello 999 hello 1099 hello 1199 hello 1299 hello 1399 hello 1499 hello 1599 hello 1699 hello 1799 hello 1899 hello 1990 hello 1991 hello 1992 hello 1993 hello 1994 hello 1995 hello 1996 hello 1997 hello 1998 hello 1999常用扩展:
-i:忽略大小写的不同,所有大小写视为相同-n:顺便输出其中其中文件中对应位置的行号-v:显示出没有 ‘搜寻字符串’ 内容的行
ubuntu@VM-0-4-ubuntu:~$ grep -i "abcd" data.txt #忽略大小写 ABCD abcd ubuntu@VM-0-4-ubuntu:~$ grep -n "abcd" data.txt #顺便输出行号 4:abcd ubuntu@VM-0-4-ubuntu:~$ grep -in "abcd" data.txt 3:ABCD 4:abcd ubuntu@VM-0-4-ubuntu:~$ grep -v "hello" data.txt #排除式的输出 ABCD abcd 1234 1111四、系统监控:top 查看进程动态
功能:用于实时监控进程和系统资源的核心命令,能动态显示CPU,内存,,进程等信息
常用扩展:
-d: 刷新的时间间隔-n: 刷新的次数q:退出
五、文件压缩与解压:zip 和 unzip
功能:将文件压缩为zip格式,把zip解压
常用扩展:
- -r :递归处理,将指定目录下的所有文件和子目录一并处理
- -d :解压到指定路径下
示例:
ubuntu@VM-0-4-ubuntu:~$ zip temp.zip temp.txt #中间为压缩包名 adding: temp.txt (deflated 78%) ubuntu@VM-0-4-ubuntu:~$ ubuntu@VM-0-4-ubuntu:~$ unzip temp.zip -d ./ # -d 加载到指定路径下,不加就是默认当前路径 Archive: temp.zip replace ./temp.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y inflating: ./temp.txt ubuntu@VM-0-4-ubuntu:~$ ls 1.txt data.txt snap temp temp1 tempp.txt temp.txt temp.zip ubuntu@VM-0-4-ubuntu:~$打包和压缩的区分:打包和压缩是一套动作
打包:多变一,为了防止丢失文件,以及提升效率
压缩:大变小,是算法
为什么打包压缩:用算法换时间
常见的压缩方式:
核心区分两类:
zip:压缩 + 打包一体,跨 Windows/Linuxtar:先打包,再配合 gzip /bzip2 /xz 做压缩(Linux 最主流)
ubuntu@VM-0-4-ubuntu:~$ zip test.zip test adding: test/ (stored 0%) #不加-r,而目录中有两个文件,就失败了 ubuntu@VM-0-4-ubuntu:~$ unzip -l test.zip #unzip -l是只查看,不解压 Archive: test.zip Length Date Time Name --------- ---------- ----- ---- 0 2026-09-28 20:02 test/ --------- ------- 0 1 file #加了 -r 后成功压缩了,并且目录也不为空 ubuntu@VM-0-4-ubuntu:~$ zip -r test1.zip test adding: test/ (stored 0%) adding: test/b.txt (stored 0%) adding: test/a.txt (stored 0%) ubuntu@VM-0-4-ubuntu:~$ unzip -l test1.zip Archive: test1.zip Length Date Time Name --------- ---------- ----- ---- 0 2026-09-28 20:02 test/ 4 2026-09-28 20:02 test/b.txt 4 2026-09-28 20:02 test/a.txt --------- ------- 8 3 filesrz与sz:压缩包怎么跨主机传输
rz= receive 服务器接收文件 →本地上传文件到服务器sz= send 服务器发送文件 →服务器下载文件到本地电脑
输入后可以选择路径
可看到
那么windows则可以用 rz 也可以拖拽
以上就是本期全部的内容。
不知道你会不会看到这里,不过我今天还是有点小焦虑的,因为我已经大三开学了现在CPP还没学,啊啊啊啊,今天吃了肉夹馍,番茄牛肉拌饭