ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

从0到1:Linux基础指令最终章

从0到1:Linux基础指令最终章 今天记住了一个热键CTRLR搜索历史指令分享给大家目录一、文件内容查看1.1 cat 与 tac :正序 / 倒序查看文件tac反过来就是cat1.3 more与less 分页查看大文件1.4 head与tail1.5 如果只显示中间某部分的内容数据流转管道|二、时间与日历date 与 cal2.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 不输出多行空行示例ubuntuVM-0-4-ubuntu:~$ echo hello world data.txt ubuntuVM-0-4-ubuntu:~$ cat data.txt hello world ubuntuVM-0-4-ubuntu:~$ echo hello world data.txt ubuntuVM-0-4-ubuntu:~$ echo hello data.txt ubuntuVM-0-4-ubuntu:~$ cat -n data.txt 1 hello world 2 hello world 3 hello ubuntuVM-0-4-ubuntu:~$ cat data.txt hello world hello world hello world hello ubuntuVM-0-4-ubuntu:~$ cat -n data.txt #-n 对所以行编号 1 hello world 2 hello world 3 4 5 6 7 8 hello world 9 hello ubuntuVM-0-4-ubuntu:~$ cat -s data.txt #-s 将多行空行压缩为一行 hello world hello world hello world hello ubuntuVM-0-4-ubuntu:~$ cat -n data.txt #-s不会对文本造成影响 1 hello world 2 hello world 3 4 5 6 7 8 hello world 9 hello ubuntuVM-0-4-ubuntu:~$ cat -b data.txt #-b 则对非空行进行编号 1 hello world 2 hello world 3 hello world 4 hello #验证tac指令的功能 ubuntuVM-0-4-ubuntu:~$ echo 1111 data.txt ubuntuVM-0-4-ubuntu:~$ cat data.txt hello world hello world hello world hello 1111 ubuntuVM-0-4-ubuntu:~$ tac data.txt 1111 #先打的最后一个倒序输出的 hello hello world hello world hello world1.2 nano linux下的记事本功能可以打开文件相当于记事本的功能语法nano 文件名示例输入如下指令后可以打开data.txt并且对该文本进行编辑ubuntuVM-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/内容 搜索比如/helloEnter 向下搜示例ubuntuVM-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退出ubuntuVM-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与tailhead和tail的功能都非常简单默认分别用于显示文件的前 10 行和后 10 行示例如下ubuntuVM-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 ubuntuVM-0-4-ubuntu:~$ head -n5 temp.txt #head -n5与head -5效果一样 hello 0 hello 1 hello 2 hello 3 hello 4 ubuntuVM-0-4-ubuntu:~$ head -5 temp.txt hello 0 hello 1 hello 2 hello 3 hello 4 ubuntuVM-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提取新文件就可以了ubuntuVM-0-4-ubuntu:~$ head -210 temp.txt tempp.txt ubuntuVM-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 ubuntuVM-0-4-ubuntu:~$这样的思想被总结后产生了管道的概念管道符号|一句话核心把左边命令的输出直接当作右边命令的输入语法命令1 | 命令2命令 1 正常会打印内容到屏幕标准输出 stdout 加管道|之后不输出到屏幕直接传给右边命令作为右边的标准输入。那么上面的示例也可以概况为一句命令ubuntuVM-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望文生义计算机中的管道只能从左向右同时管道还可以连接可以有多个管道称为管道的级联ubuntuVM-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 ubuntuVM-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 与 cal2.1 date:这里直接给出示例ubuntuVM-0-4-ubuntu:~$ date Mon Sep 28 04:50:25 PM CST 2026 #这种是外国的输出方式 ubuntuVM-0-4-ubuntu:~$ date %Y:%m:%d #进行了格式化这里Y要大写代表年m和d代表月和日 2026:09:28 ubuntuVM-0-4-ubuntu:~$ date %Y/%m/%d #与上一个的区别只有划分的符号不同这个就比较简单了 2026/09/28 ubuntuVM-0-4-ubuntu:~$ date %Y 2026 #也可以只显示年或月或日同时还可以显示时分秒扩展如下%Y年份%m月份%d日期%H小时%M分钟%S秒%s时间戳%F 相当于%Y-%m-%d%T 相当于%H:%M:%SubuntuVM-0-4-ubuntu:~$ date %F-%T # %F和%T间的-可以用其他符号代替但是不能为空 2026-09-28-16:58:09 ubuntuVM-0-4-ubuntu:~$2.2 时间戳概念从 1970-01-01 00:00:00 UTC格林威治时间到当前时刻的总秒数也叫 Epoch 时间作用是生成日志日志可以用来排除错误 。获取时间戳用 %subuntuVM-0-4-ubuntu:~$ date %s 1790586197甚至可以指明时间戳的真实时间date %F %T -d 你获取的数字ubuntuVM-0-4-ubuntu:~$ date %F-%T -d 1790586197 # 把刚刚拿到的时间戳转回可读时间 2026-09-28-17:03:17 ubuntuVM-0-4-ubuntu:~$ date %F-%T -d 0 1970-01-01-08:00:00 #可以看到时间是八点因为使用了北京的东八区时间加上了8小时2.3 cal :查看公历非常简单只需要输入cal即可ubuntuVM-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显示当前年份的日历ubuntuVM-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 #显示本月份每天为本年的第几天 ubuntuVM-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 #显示全年日历 ubuntuVM-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还可以检查指定年 月的日历ubuntuVM-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 #查看指定月份注意月份在前 ubuntuVM-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 -optionsfind的扩展非常多用到再查需要记住的就是find name就可以ubuntuVM-0-4-ubuntu:~$ find temp.txt temp.txt总结Linux下find命令在目录结构中搜索文件并执行指定的操作。Linux下find命令提供了相当多的查找条件功能很强大。由于find具有强大的功能所以它的选项也很多其中大部分选项都值得我们花时间来了解⼀下。即使系统中含有网络文件系统( NFS)find命令在该文件系统中同样有效只你具有相应的权限。在运行一个非常消耗资源的find命令时很多⼈都倾向于把它放在后台执行因为遍历一个大的文件系统可能会花费很长的时间(这⾥是指30G字节以上的文件系统)。3.2 whereis 查找文件找到文件的源ubuntuVM-0-4-ubuntu:~$ whereis ls ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz ubuntuVM-0-4-ubuntu:~$ whereis temp.txt temp.txt: ubuntuVM-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 ubuntuVM-0-4-ubuntu:~$ whereis tree tree: /snap/bin/tree ubuntuVM-0-4-ubuntu:~$通过上述的树可见如果当前目录下即可找到whereis的文件那么不会输出地址了如果在其他分支则输出地址3.3 grep在文件中搜索字符串示例ubuntuVM-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显示出没有 ‘搜寻字符串’ 内容的行ubuntuVM-0-4-ubuntu:~$ grep -i abcd data.txt #忽略大小写 ABCD abcd ubuntuVM-0-4-ubuntu:~$ grep -n abcd data.txt #顺便输出行号 4:abcd ubuntuVM-0-4-ubuntu:~$ grep -in abcd data.txt 3:ABCD 4:abcd ubuntuVM-0-4-ubuntu:~$ grep -v hello data.txt #排除式的输出 ABCD abcd 1234 1111四、系统监控top 查看进程动态功能用于实时监控进程和系统资源的核心命令能动态显示CPU内存进程等信息常用扩展-d: 刷新的时间间隔-n: 刷新的次数q退出五、文件压缩与解压zip 和 unzip功能将文件压缩为zip格式把zip解压常用扩展-r 递归处理将指定目录下的所有文件和子目录一并处理-d :解压到指定路径下示例ubuntuVM-0-4-ubuntu:~$ zip temp.zip temp.txt #中间为压缩包名 adding: temp.txt (deflated 78%) ubuntuVM-0-4-ubuntu:~$ ubuntuVM-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 ubuntuVM-0-4-ubuntu:~$ ls 1.txt data.txt snap temp temp1 tempp.txt temp.txt temp.zip ubuntuVM-0-4-ubuntu:~$打包和压缩的区分打包和压缩是一套动作打包多变一为了防止丢失文件以及提升效率压缩大变小是算法为什么打包压缩用算法换时间常见的压缩方式核心区分两类zip压缩 打包一体跨 Windows/Linuxtar先打包再配合 gzip /bzip2 /xz 做压缩Linux 最主流ubuntuVM-0-4-ubuntu:~$ zip test.zip test adding: test/ (stored 0%) #不加-r而目录中有两个文件就失败了 ubuntuVM-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 后成功压缩了并且目录也不为空 ubuntuVM-0-4-ubuntu:~$ zip -r test1.zip test adding: test/ (stored 0%) adding: test/b.txt (stored 0%) adding: test/a.txt (stored 0%) ubuntuVM-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还没学啊啊啊啊今天吃了肉夹馍番茄牛肉拌饭
返回列表