【Linux学习】Linux学习第四弹——Linux基本指令3

🎬 个人主页MSTcheng · CSDN
🌱 代码仓库MSTcheng · Gitee
🔥 精选专栏 : 《C语言
数据结构
《算法学习》
C++由浅入深
《Linux操作系统》

💬座右铭: 路虽远行则将至,事虽难做则必成!


上一篇文章中【Linux】Linux学习第三弹------Linux基本指令2我们给大家介绍了像tree,rm,cp以及输入/输出相关的重定向等指令和概念,本篇文章我们就来介绍设计到文件操作的相关指令以及相关概念。

Linux指令相关联的文章:

【Linux学习】Linux学习第二弹------Linux基本指令1

【Linux学习】Linux学习第三弹------Linux基本指令2

文章目录

  • [一、`file` 指令:识别文件真实类型](#一、file 指令:识别文件真实类型)
    • [1. 演示](#1. 演示)
    • 2.解释:
    • [3.也可使用 `cat` 查看文件内容](#3.也可使用 cat 查看文件内容)
  • [二、查看大文件:`head` / `tail` / `more` / `less`](#二、查看大文件:head / tail / more / less)
    • [1. 为什么需要它们](#1. 为什么需要它们)
    • [2. `head`:看文件开头](#2. head:看文件开头)
    • [3. `tail`:看文件末尾](#3. tail:看文件末尾)
    • [4. `more` / `less`:分页查看](#4. more / less:分页查看)
    • [1. `which`:查找命令在哪](#1. which:查找命令在哪)
    • [2. `whereis`:查找命令及相关文件](#2. whereis:查找命令及相关文件)
    • [3. `find`:按条件查找文件](#3. find:按条件查找文件)
    • [4. `grep`:按内容查找](#4. grep:按内容查找)
    • [5. 管道 `|`:让命令串联起来](#5. 管道 |:让命令串联起来)
  • [五、管道 `|`:一个命令的输出,喂给下一个命令 本节重点⭐⭐⭐](#五、管道 |:一个命令的输出,喂给下一个命令 本节重点⭐⭐⭐)
    • [1. 概念](#1. 概念)
    • [2. 在上面一篇文章中我们介绍过`Linux一切皆文件`,那管道是文件吗?](#2. 在上面一篇文章中我们介绍过Linux一切皆文件,那管道是文件吗?)
    • [3. 重定向 vs 管道(高频辨析)](#3. 重定向 vs 管道(高频辨析))
  • [六、压缩与解压指令:`zip` / `unzip` / `tar`](#六、压缩与解压指令:zip / unzip / tar)
    • [1. 为什么需要压缩](#1. 为什么需要压缩)
    • [2. `zip`:压缩成 .zip 包](#2. zip:压缩成 .zip 包)
    • [3. `unzip`:解压 .zip 包](#3. unzip:解压 .zip 包)
    • [4. `tar`:打包 + 压缩(Linux 最常用)](#4. tar:打包 + 压缩(Linux 最常用))
  • [七、上传 / 下载文件:`rz` / `sz`(重点)](#七、上传 / 下载文件:rz / sz(重点))
    • [1. 为什么需要它们](#1. 为什么需要它们)
    • [2. `rz`:把文件从 Windows 传到 Linux(上传)](#2. rz:把文件从 Windows 传到 Linux(上传))
    • [3. `sz`:把文件从 Linux 传到 Windows(下载)](#3. sz:把文件从 Linux 传到 Windows(下载))
    • [4. 重点总结:`rz` / `sz` 使用要点](#4. 重点总结:rz / sz 使用要点)
  • [八、系统信息查看:`top` / `htop` / `uname` / `date`](#八、系统信息查看:top / htop / uname / date)
    • [1. `top`:实时查看系统状态](#1. top:实时查看系统状态)
    • [2. `htop`:`top` 的升级版(更友好)](#2. htoptop 的升级版(更友好))
    • [3. `uname -r`:查看内核版本](#3. uname -r:查看内核版本)
    • [4. `date`:查看 / 设置系统时间](#4. date:查看 / 设置系统时间)
  • 九、总结

一、file 指令:识别文件真实类型

1. 演示

bash 复制代码
mstcheng@ubuntu-dev:~/117/code$ ll
total 44
drwxrwxr-x  8 mstcheng mstcheng 4096 Sep  6 22:50 ./
drwxrwxr-x  5 mstcheng mstcheng 4096 Aug 28 15:02 ../
drwxrwxr-x  2 mstcheng mstcheng 4096 Oct 28  2025 cgdbtest/
-rw-rw-r--  1 mstcheng mstcheng   75 Sep  6 22:50 code.c
drwxrwxr-x 18 mstcheng mstcheng 4096 Jul 18 15:22 linux-code/
drwxrwxr-x  2 mstcheng mstcheng 4096 Oct 28  2025 list/
-rw-rw-r--  1 mstcheng mstcheng  533 Oct 17  2025 makefile
drwxrwxr-x  2 mstcheng mstcheng 4096 Oct 21  2025 procbar/
drwxrwxr-x  2 mstcheng mstcheng 4096 Aug 29 18:06 test1/
drwxrwxr-x  2 mstcheng mstcheng 4096 Nov 23  2025 testCode/
-rw-rw-r--  1 mstcheng mstcheng   30 Sep  1 18:25 tmp.txt

mstcheng@ubuntu-dev:~/117/code$ file code.c 
code.c: C source, ASCII text

//将.c文件编译成可执行文件 注意Linux系统下可执行文件不是.exe后缀 
//Linux系统下可以任意后缀!!后面会介绍!
mstcheng@ubuntu-dev:~/117/code$ gcc code.c -o code.exe

mstcheng@ubuntu-dev:~/117/code$ file code.exe 
code.exe: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=a649fb372ce907f0e30dba78e795f88f3afada95, for GNU/Linux 3.2.0, not stripped

mstcheng@ubuntu-dev:~/117/code$ file tmp.txt 
tmp.txt: ASCII text

2.解释:

  • file 文件名:不看后缀,通过读取文件内容的特征,告诉你这个文件真正是什么
  • code.cC source, ASCII text:是一个C 语言源码文件(ASCII 文本)
  • my.exeELF 64-bit LSB executable, x86-64 ...:是一个 64 位 x86-64 架构的 ELF 可执行程序ELF 就是 Linux 下可执行文件的格式,等价于 Windows 的 PE/.exe)。
  • tmp.txtASCII text:是一个 纯文本文件 (虽然后缀是 .txt,但 file 指令不看后缀,直接根据内容特征识别出它是 ASCII 文本)。

3.也可使用 cat 查看文件内容

bash 复制代码
mstcheng@ubuntu-dev:~/117/code$ cat code.c
#include <stdio.h>
int main()
{
    printf("hello Linux\n");
     return 0;
}
  • cat 文件名:把文件内容原样打印到屏幕上(查看文件内容的命令)。
  • 这里 code.c 里实际装的是一段 C 语言代码

二、查看大文件:head / tail / more / less

1. 为什么需要它们

  • cat 会把整个文件刷屏,大文件(如日志)必须"按需查看"
  • 这一组指令归类为:"查看大文件 / 日志文件!"类指令

例如:

bash 复制代码
//使用脚本往tmp.txt里面写了1000个hello Linux
mstcheng@ubuntu-dev:~/117/code$ ./write_hello.sh 
write 1000 row done! you can search!
1000 tmp.txt

//如果使用cat命令 1000行hello Linux瞬间刷屏非常不方便看
mstcheng@ubuntu-dev:~/117/code$ cat tmp.txt 
hello Linux
hello Linux
hello Linux
hello Linux
......

因此下面的head / tail / more / less 这些命令才是我们查看大文件的正确姿势!

2. head:看文件开头

  • 作用:作用:查看文件**前面(开头)**部分。
  • 演示上面那个tmp.txt文件:
bash 复制代码
mstcheng@ubuntu-dev:~/117/code$ head tmp.txt 
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
mstcheng@ubuntu-dev:~/117/code$ 

//上面默认是显示前10行 你也可以指定看前n行!
mstcheng@ubuntu-dev:~/117/code$ head -20 tmp.txt 
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
mstcheng@ubuntu-dev:~/117/code$ 
  • 常用写法head -n 文件(看前 n 行);默认看前 10 行。

3. tail:看文件末尾

  • 作用 :查看文件末尾部分。
  • 演示:
bash 复制代码
//默认查的是倒数后10行
mstcheng@ubuntu-dev:~/117/code$ tail tmp.txt 
990 hello Linux
991 hello Linux
992 hello Linux
993 hello Linux
994 hello Linux
995 hello Linux
996 hello Linux
997 hello Linux
998 hello Linux
999 hello Linux
mstcheng@ubuntu-dev:~/117/code$ 

mstcheng@ubuntu-dev:~/117/code$ tail -20 tmp.txt 
980 hello Linux
981 hello Linux
982 hello Linux
983 hello Linux
984 hello Linux
985 hello Linux
986 hello Linux
987 hello Linux
988 hello Linux
989 hello Linux
990 hello Linux
991 hello Linux
992 hello Linux
993 hello Linux
994 hello Linux
995 hello Linux
996 hello Linux
997 hello Linux
998 hello Linux
999 hello Linux
mstcheng@ubuntu-dev:~/117/code$ 
  • 常用写法:tail -n 11 文件(看最后 11 行);默认看最后 10 行。
  • 重点应用tail -f 文件名 可以实时跟踪文件末尾新增内容(查看正在不断增长的日志)------生产环境看日志神器。

4. more / less:分页查看

  • "more or less。"

  • less 文件名:分页显示,支持:

    • 上下方向键 翻页/滚动
    • /搜索关键字 搜索并高亮
    • 回车,n :回车看下一行;按 n 跳到下一个匹配
    • q 退出

    演示:

  • more 是 less 的老大哥,功能更少(只能往下翻);more VS less less更强大,支持回翻和搜索

如果忘记了相关参数,可以直接使用man来查看对应的手册! # 三、查找文件 / 内容:find / whereis / which / grep 与管道ch`

1. which:查找命令在哪

  • 作用:查看某个 命令(可执行程序) 所在的路径。
  • 演示:
bash 复制代码
mstcheng@ubuntu-dev:~/117/code$ which gcc
/usr/bin/gcc

mstcheng@ubuntu-dev:~/117/code$ which ls
/usr/bin/ls
  • 解释:which 命令名 会去环境变量 PATH (后面会介绍到)对应的目录里,找到第一个匹配的可执行文件并返回路径。目前就记住这个命令能够查文件路径!
  • 常用写法:which 命令名

2. whereis:查找命令及相关文件

  • 作用:查找命令的二进制文件、帮助手册等关联文件。
  • 演示:
bash 复制代码
mstcheng@ubuntu-dev:~/117/code$ whereis gcc
gcc: /usr/bin/gcc /usr/lib/gcc /usr/share/man/man1/gcc.1.gz

mstcheng@ubuntu-dev:~/117/code$ whereis find
find: /usr/bin/find /usr/share/man/man1/find.1.gz
  • 解释:whereis 除了找可执行文件,还会顺带找出手册页 等资料,适合快速定位一个命令的安装位置。
  • 常用写法:whereis 命令名

3. find:按条件查找文件

  • 作用:强大的文件查找命令,可以按文件名、类型、时间、大小等条件递归查找。
  • 演示:
bash 复制代码
//在当前目录下查找名为 code.c 的文件
mstcheng@ubuntu-dev:~/117/code$ find . -name "code.c"
./code.c

//查找所有 .txt 文件
mstcheng@ubuntu-dev:~/117/code$ find . -name "*.txt"
./tmp.txt
  • 常用写法:
    • find 路径 -name "文件名":按名字查找,支持通配符 *
    • -type f:只查找普通文件。
    • -type d:只查找目录。
    • 示例:find . -type f -name "*.c" 表示查找当前目录下所有 .c 文件。
  • 区别记忆:which / whereis 主要用来找命令 ,而 find 用来找任意文件

4. grep:按内容查找

  • 作用 :前面 find 主要按文件名、类型、时间、大小 等条件找文件;grep 则是在文件内容 里查找包含某个关键字的行,属于"按内容查找"。
  • 演示:继续拿刚才的 tmp.txt 举例,查找包含 hello 的行:
bash 复制代码
//在 tmp.txt 中查找包含 hello 的行
mstcheng@ubuntu-dev:~/117/code$ grep "hello" tmp.txt
hello Linux
hello Linux
hello Linux
hello Linux
......


- 常用写法:`grep "关键字" 文件名`。
- 解释:`grep` 会把文件中**包含关键字的每一行**打印出来;如果文件很大,匹配行很多,直接输出仍然会刷屏。
- 补充几个常用选项:

```bash

//先准备一个包含大小写、不同内容的测试文件
mstcheng@ubuntu-dev:~/117/code$ cat > hello.txt << EOF
Hello Linux
hello world
HELLO linux
hello Linux
goodbye Linux
EOF

//-i:忽略大小写,Hello / HELLO / hello 都算匹配
mstcheng@ubuntu-dev:~/117/code$ grep -i "hello" hello.txt
Hello Linux
hello world
HELLO linux
hello Linux

//-n:顺便输出行号
mstcheng@ubuntu-dev:~/117/code$ grep -n "hello" hello.txt
2:hello world
4:hello Linux

//-v:反向选择,只显示没有 "hello" 内容的那一行
mstcheng@ubuntu-dev:~/117/code$ grep -v "hello" hello.txt
Hello Linux
HELLO linux
goodbye Linux
  • -i:忽略大小写,所以大小写视为相同。
  • -n:顺便输出行号。
  • -v:反向选择,显示出没有搜索字符串内容的那一行。
    屏。

5. 管道 |:让命令串联起来

  • 问题 :像上面的 grep "hello" tmp.txt,如果匹配了 1000 行,命令行还是会刷屏,怎么办?
  • 管道符 |左边命令的输出** ,作为**右边命令的输入****,这样就可以把 grep 和其他命令配合使用。
bash 复制代码
//grep 查找后,只显示前 10 行
mstcheng@ubuntu-dev:~/117/code$ grep "hello" tmp.txt | head
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux
hello Linux

//数一数 tmp.txt 里有多少行包含 hello
mstcheng@ubuntu-dev:~/117/code$ grep "hello" tmp.txt | wc -l
1000
  • 解释:
    • grep "hello" tmp.txt | headgrep 输出的结果通过 | 交给 head,所以只显示前 10 行,不再刷屏。
    • grep "hello" tmp.txt | wc -lgrep 的结果交给 wc -l,统计一共有多少行。
    • 这里 wc -l 表示"统计行数"。
  • 总结 :管道 | 把多个简单命令串联成一条"流水线":前一个命令的输出,自动成为后一个命令的输入。grep 负责过滤内容,headwc -l 等命令负责进一步处理。

那么什么是管道呢?下面就来学习一下

五、管道 |:一个命令的输出,喂给下一个命令 本节重点⭐⭐⭐

1. 概念

  • "管道:既是入口 也是出口,它是传送资源的!
  • 管道 | 的作用 :把左边命令的输出 ,当作右边命令的输入
  • 形象理解:一根管子把两个命令"接"起来,左边的结果流进右边的入口。

2. 在上面一篇文章中我们介绍过Linux一切皆文件,那管道是文件吗?

  • 答案:是,但也不是普通文件 。在 "一切皆文件" 的意义下,管道也是一种文件/文件接口;但它和普通文件有本质区别------
  • 管道的特点:"内存级" 。管道是内存中的缓冲区,数据不落盘(不会写进磁盘),命令执行完就没了。
  • 对比 :重定向 > 是"写进 磁盘 上的文件", 管道 | 是"直接传给下一个命令(内存中转)"。

3. 重定向 vs 管道(高频辨析)

| | 重定向 >/>> | 管道 | |
|--------|--------------|-------------------|
| 连接对象 | 命令 与 文件 | 命令 与 命令 |
| 数据去向 | 写入磁盘文件 | 传给下一个命令(内存缓冲) |
| 数据是否落盘 | 落盘 | 不落盘(内存级) |

六、压缩与解压指令:zip / unzip / tar

1. 为什么需要压缩

  • 前面我们学了 cp 复制、mv 移动,但 文件多了、大了,传输和备份都很麻烦
  • 压缩 :把多个文件/目录"打包"并压缩体积,方便传输、备份、归档
  • 所以这一组指令归类为:"压缩 / 解压!"类指令

2. zip:压缩成 .zip 包

  • 作用 :把文件或目录压缩 成一个 .zip 压缩包(Windows 也能直接打开)。
  • 演示:
bash 复制代码
mstcheng@ubuntu-dev:~/117/code$ ll
total 76
drwxrwxr-x  8 mstcheng mstcheng  4096 Sep  6 23:24 ./
drwxrwxr-x  5 mstcheng mstcheng  4096 Aug 28 15:02 ../
drwxrwxr-x  2 mstcheng mstcheng  4096 Oct 28  2025 cgdbtest/
-rw-rw-r--  1 mstcheng mstcheng    75 Sep  6 22:50 code.c
-rwxrwxr-x  1 mstcheng mstcheng 15952 Sep  6 22:51 code.exe*
drwxrwxr-x 18 mstcheng mstcheng  4096 Jul 18 15:22 linux-code/
drwxrwxr-x  2 mstcheng mstcheng  4096 Oct 28  2025 list/
-rw-rw-r--  1 mstcheng mstcheng   533 Oct 17  2025 makefile
drwxrwxr-x  2 mstcheng mstcheng  4096 Oct 21  2025 procbar/
drwxrwxr-x  2 mstcheng mstcheng  4096 Aug 29 18:06 test1/
drwxrwxr-x  2 mstcheng mstcheng  4096 Nov 23  2025 testCode/
-rw-rw-r--  1 mstcheng mstcheng 15890 Sep  6 23:25 tmp.txt
-rwxrwxr-x  1 mstcheng mstcheng   174 Sep  6 23:24 write_hello.sh*


//把 code.c 和 tmp.txt 压缩成一个 code.zip
mstcheng@ubuntu-dev:~/117/code$ zip code.zip code.c tmp.txt 
  adding: code.c (deflated 5%)
  adding: tmp.txt (deflated 86%)


//把整个目录递归压缩(-r 表示递归,把目录里的所有文件都打进去)
mstcheng@ubuntu-dev:~/117/code$ zip test.zip -r test1
  adding: test1/ (stored 0%)
  adding: test1/code.exe (deflated 86%)

  ......
  • 常用写法:
    • zip 压缩包名.zip 文件1 文件2:压缩多个文件
    • zip -r 压缩包名.zip 目录名递归 压缩整个目录(-r 必须加,否则目录是空的)。
  • 解释zip 会生成一个 .zip 后缀的压缩包,原文件还在,不会消失。

其实就是相当于你在windows选中几个文件然后右键点击压缩。

3. unzip:解压 .zip 包

  • 作用 :把 .zip 压缩包解压出来,还原成原来的文件/目录。
  • 演示:
bash 复制代码
//把 code.zip 解压到当前目录
mstcheng@ubuntu-dev:~/117/code$ unzip code.zip 
Archive:  code.zip
  inflating: code.c                  
  inflating: tmp.txt  

//解压到指定目录(-d 指定解压路径)
mstcheng@ubuntu-dev:~/117/code$ unzip code.zip -d mydir/
  • 常用写法:
    • unzip 压缩包名.zip:解压到 当前目录
    • unzip 压缩包名.zip -d 目录名:解压到 指定目录(目录不存在会自动创建)。
  • 解释unzip 会把压缩包里的文件还原 出来;如果解压时提示 command not found,说明还没安装,用 sudo apt install zip unzip 安装即可。

4. tar:打包 + 压缩(Linux 最常用)

  • 作用tar 是 Linux 下最常用 的打包/压缩工具,既能打包 (把多个文件合成一个),也能配合 gzip 实现压缩
  • 演示:
bash 复制代码
//打包并压缩成 .tar.gz(-c 创建,-z 用 gzip 压缩,-v 显示过程,-f 指定文件名)
mstcheng@ubuntu-dev:~/117/code$ tar -czvf code.tar.gz code.c tmp.txt
code.c
tmp.txt

//解压 .tar.gz(-x 解压,-z 解压 gzip,-v 显示过程,-f 指定文件名)
mstcheng@ubuntu-dev:~/117/code$ tar -xzvf code.tar.gz
code.c
tmp.txt

//只打包不压缩(生成 .tar,体积不减小,只是把多个文件合成一个)
mstcheng@ubuntu-dev:~/117/code$ tar -cvf code.tar code.c tmp.txt
code.c
tmp.txt
  • 常用写法(记口诀:czvf 打包,xzvf 解包 ):
    • tar -czvf 包名.tar.gz 文件/目录打包 + 压缩(最常用)。
    • tar -xzvf 包名.tar.gz解压
    • tar -cvf 包名.tar 文件/目录只打包不压缩
  • 解释tar 本身只负责"打包",-z 才调用 gzip 压缩;所以 .tar.gz 才是"打包 + 压缩"的完整形态。
  • 区别记忆:zip 是"压缩 + 打包一步到位"tar 是"先打包、再压缩"Linux 服务器上 tar.gz 更常见

七、上传 / 下载文件:rz / sz(重点)

1. 为什么需要它们

  • 前面学的都是在 Linux 内部 操作文件;但实际中,我们经常需要在 Windows 和 Linux 之间传文件
  • rz / sz 就是配合 Xshell / SecureCRT 等终端工具 ,在本地 Windows 和远程 Linux 之间互传文件的神器。
  • 这一组指令归类为:"本地 ↔ 远程 传文件!"类指令

2. rz:把文件从 Windows 传到 Linux(上传)

  • 作用rz(receive Zmodem)接收 文件,把本地 Windows 上的文件上传到远程 Linux
  • 演示:
bash 复制代码
//在 Linux 终端输入 rz,回车后会弹出 Windows 的文件选择框
mstcheng@ubuntu-dev:~/117/code$ rz

//选择 Windows 上的 test.txt,上传完成后 Linux 当前目录就多了一个 test.txt
mstcheng@ubuntu-dev:~/117/code$ ls
test.txt  code.c  tmp.txt
  • 常用写法:rz(直接回车,会弹出文件选择框,选完自动上传)。
  • 解释rz 上传的文件会 保存在当前目录;上传时 同名文件会覆盖,注意别传错。

3. sz:把文件从 Linux 传到 Windows(下载)

  • 作用sz(send Zmodem)发送 文件,把远程 Linux 上的文件下载到本地 Windows
  • 演示:
bash 复制代码
//把 Linux 上的 code.c 下载到 Windows(会弹出保存位置选择框)
mstcheng@ubuntu-dev:~/117/code$ sz code.c

//也可以一次下载多个文件
mstcheng@ubuntu-dev:~/117/code$ sz code.c tmp.txt
  • 常用写法:sz 文件名(可一次传多个文件,用空格隔开)。
  • 解释:sz 下载的文件会保存到 Windows 上你选择的位置;下载目录可以在 Xshell 的"文件传输"设置里改。

4. 重点总结:rz / sz 使用要点

  • rz = 上传 (Receive,Windows → Linux);sz = 下载 (Send,Linux → Windows)。
  • 前提:必须使用 Xshell / SecureCRT 等支持 Zmodem 协议的终端工具,纯命令行(如直接 SSH)不支持。
  • 如果提示 command not found,先安装:sudo apt install lrzsz
  • 对比记忆
    • rz / sz图形化弹窗传文件,适合小文件、临时传。
    • scp / ftp命令行传文件,适合脚本、批量、大文件(后面会介绍)。

八、系统信息查看:top / htop / uname / date

1. top:实时查看系统状态

  • 作用 :实时显示 CPU、内存、进程 等系统运行状态,相当于 Windows 的"任务管理器"。
  • 演示:
bash 复制代码
mstcheng@ubuntu-dev:~/117/code$ top

top - 23:26:01 up 1 day,  2:34,  1 user,  load average: 0.00, 0.01, 0.05
Tasks: 123 total,   1 running, 122 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.3 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   1987.3 total,    523.1 free,    812.4 used,    651.8 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used   2048.0 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
   1234 mstcheng  20   0  168432   9876   7654 S   0.3   0.5   0:01.23 bash
   5678 mstcheng  20   0  245678  12345   9876 S   0.0   0.6   0:02.45 top
   ......
  • 常用写法:top(直接回车进入实时监控界面,按 q 退出)。
  • 解释:top每隔几秒自动刷新 一次,展示当前系统最耗资源的进程排行;按 q 退出,按 1 可以查看每个 CPU 核心的使用情况。

2. htoptop 的升级版(更友好)

  • 作用htoptop升级版 ,界面更美观、支持鼠标操作彩色显示,看起来更直观。
  • 演示:
bash 复制代码
mstcheng@ubuntu-dev:~/117/code$ htop

  1  [|                                     0.3%]     Tasks: 123, 122 thr; 1 running
  2  [|                                     0.3%]     Load average: 0.00 0.01 0.05 
  Mem[|||||||||||||||||||||||||||||||||  812M/1987M]     Uptime: 1 day, 02:34:00
  Swp[                                       0K/2048M]

  PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
 1234 mstcheng   20   0 168M 9876 7654 S  0.3  0.5  0:01.23 bash
 5678 mstcheng   20   0 245M 12345 9876 S  0.0  0.6  0:02.45 htop
 ......
  • 常用写法:htop(直接回车进入,按 q 退出,按 F10 也可以退出)。
  • 解释:htop 顶部用彩色进度条 直观展示 CPU 和内存占用,下面进程列表支持上下键选择、F9 直接杀进程 ,比 top 更人性化。
  • 如果提示 command not found,先安装:sudo apt install htop

3. uname -r:查看内核版本

  • 作用uname 用来查看系统信息 ,其中 uname -r 专门查看 Linux 内核版本号
  • 演示:
bash 复制代码
mstcheng@ubuntu-dev:~/117/code$ uname -r
7.0.0-30-generic
  • 常用写法:uname -r(查看内核版本);uname -a(查看全部系统信息,包括主机名、内核、架构等)。
  • 解释:uname -r 输出的就是当前系统的内核版本 ,比如 7.0.0-30-generic 是 Ubuntu 常见的内核版本号;排查驱动、内核相关问题时经常用到。

4. date:查看 / 设置系统时间

  • 作用 :查看当前系统日期和时间,也可以按指定格式输出。
  • 演示:
bash 复制代码
mstcheng@ubuntu-dev:~/117/code$ date
2026年 09月 07日 星期一 23:26:01 CST

//按指定格式输出(+ 后面跟格式符)
mstcheng@ubuntu-dev:~/117/code$ date "+%Y-%m-%d %H:%M:%S"
2026-09-07 23:26:01
  • 常用写法:date(直接查看当前时间);date "+%Y-%m-%d %H:%M:%S"(按 年-月-日 时:分:秒 格式输出)。
  • 解释:date 默认输出当前系统时间;配合 + 和格式符可以自定义输出格式 ,比如 %Y 表示年份、%m 表示月份、%d 表示日期,在写脚本生成带时间戳的文件名时非常常用。

九、总结

本篇文章围绕 Linux 文件操作,依次介绍了:

1、用 file 识别文件真实类型、用 head / tail / more /less 查看大文件;

2、用 find / whereis / which / grep 查找文件与内容、用管道 |串联命令

3、用 zip / unzip / tar 压缩解压;

4、用 rz / sz 跨系统传文件;

5、用 top /htop / uname / date 查看系统信息。

这些指令覆盖了日常使用 Linux 最频繁的操作场景,是后续学习的基础。

相关推荐
lihaihui19911 小时前
STM32 学习
学习
是隼人2 小时前
buuctf-pwn jarvisoj_level5(64位ret2libc)题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
筝筝ba2 小时前
OpenBMC 自动化测试中的多进程陷阱:从 Execute Process 返回异常到 output.xml 损坏问题分析
xml·linux·运维·服务器·网络
饕餮争锋2 小时前
PATH路径列表介绍
java·linux·服务器
V158897262012 小时前
从滴滴模式看机器人租赁:撮合型租赁平台开发源码的调度系统设计思路
linux·前端·机器人
代码村新手2 小时前
Linux-vim的使用和配置
linux
吴声子夜歌2 小时前
编写Shell脚本——启动项目
linux·运维·shell
闲云野鹤在人间2 小时前
云计算入门|网络与存储云服务知识梳理
linux·运维·服务器·网络·centos·云计算·php
heqingchun162 小时前
Ubuntu 22.04 交叉编译 RK3568 MPP + FFmpeg 完整指南
linux·ubuntu·ffmpeg