Linux系统笔记

1. 文件和目录操作

1.1 基本文件操作

查看文件和目录

1
2
3
4
5
6
7
8
9
10
# 查看当前目录内容
ls                          # 基本列表
ls -l                      # 详细信息
ls -la                     # 包含隐藏文件
ls -lh                     # 人性化大小显示

# 查看目录结构
tree                       # 树状显示
tree -d                    # 只显示目录
tree -L 2                  # 限制显示层级

文件内容查看

1
2
3
4
5
6
# 查看文件内容
cat filename               # 显示全部内容
head filename              # 显示前10行
head -n 20 filename        # 显示前20行
tail filename              # 显示后10行
tail -f filename           # 实时监控文件变化

目录操作

1
2
3
4
5
6
7
8
9
# 创建目录
mkdir dirname              # 创建单个目录
mkdir -p path/to/dir       # 创建多层目录

# 切换目录
cd /path/to/directory      # 绝对路径
cd ../                     # 上级目录
cd ~                       # 家目录
cd -                       # 上次访问的目录

1.2 文件删除操作

rm命令详解

1
2
3
4
5
6
7
8
9
10
11
12
# 删除文件
rm filename                # 基本删除
rm -i filename             # 删除前确认(安全)
rm -f filename             # 强制删除,不询问

# 删除目录
rm -r dirname              # 递归删除目录
rm -rf dirname             # 强制递归删除(常用)

# 批量删除
rm *.txt                   # 删除所有txt文件
rm -rf dir1 dir2 dir3      # 删除多个目录

参数说明:

  • -r: 递归删除,用于删除目录

  • -f: 强制删除,不询问确认

  • -i: 交互模式,删除前询问

  • -v: 详细模式,显示删除过程

安全删除实践

1
2
3
4
# 安全删除(推荐)
rm -i important_file       # 删除前确认
ls -la before_delete/      # 删除前查看内容
rm -rf test_directory      # 确认无误后删除

2. 打包与压缩

2.1 tar 打包和解包

基本打包操作

1
2
3
4
5
6
7
8
# 打包文件
tar -cvf archive.tar file1 file2 directory/

# 解包文件
tar -xvf archive.tar

# 查看包内容
tar -tvf archive.tar

参数说明:

  • c: 创建打包文件

  • x: 解开档案文件

  • v: 显示详细过程

  • f: 指定文档文件名称

  • t: 查看包内容

2.2 压缩和解压

gzip 压缩

1
2
3
4
5
6
7
8
# 压缩文件(tar + gzip)
tar -zcvf archive.tar.gz files_or_directories/

# 解压文件
tar -zxvf archive.tar.gz

# 解压到指定目录
tar -zxvf archive.tar.gz -C /target/directory

其他压缩格式

1
2
3
4
5
6
7
8
9
10
11
# bzip2 压缩(更高压缩比)
tar -jcvf archive.tar.bz2 files/
tar -jxvf archive.tar.bz2

# xz 压缩(最高压缩比)
tar -Jcvf archive.tar.xz files/
tar -Jxvf archive.tar.xz

# zip 压缩
zip -r archive.zip directory/
unzip archive.zip

2.3 实用压缩示例

日常备份操作

1
2
3
4
5
6
7
8
# 备份网站文件
tar -zcvf website_backup_$(date +%Y%m%d).tar.gz /var/www/html/

# 备份配置文件
tar -zcvf config_backup.tar.gz /etc/nginx/ /etc/mysql/

# 排除特定文件
tar -zcvf backup.tar.gz --exclude='*.log' --exclude='node_modules' /project/

3. 后台进程管理

3.1 Screen 会话管理(推荐)

Screen 是一个全屏窗口管理器,相当于在一个物理终端上虚拟出多个进程。

基本 Screen 命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 创建新会话
screen -S session_name

# 列出所有会话
screen -ls

# 恢复会话
screen -r session_name

# 分离会话(在会话内执行)
Ctrl+A+D

# 删除会话
screen -S session_name -X quit

Screen 常用操作

1
2
3
4
5
6
7
8
9
10
11
12
13
# 创建一个名为 "webserver" 的会话
screen -S webserver

# 在会话中启动服务
python app.py

# 分离会话(Ctrl+A+D),服务继续在后台运行

# 重新连接会话
screen -r webserver

# 强制连接会话(如果已连接)
screen -d -r webserver

Screen 参数详解

  • -A: 调整所有窗口大小为当前终端大小

  • -d <会话名>: 分离指定会话

  • -h <行数>: 指定缓冲区行数

  • -m: 强制创建新会话

  • -r <会话名>: 恢复指定会话

  • -R: 尝试恢复会话,找不到则创建新会话

  • -S <会话名>: 指定会话名称

  • -ls: 显示所有会话

  • -wipe: 清理无效会话

3.2 nohup 后台运行

基本 nohup 使用

1
2
3
4
5
6
7
8
9
10
11
# 后台运行程序
nohup python script.py &

# 指定输出文件
nohup python script.py > output.log 2>&1 &

# 查看后台任务
jobs

# 将后台任务调到前台
fg %1

3.3 systemctl 服务管理

服务控制命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 启动服务
systemctl start service_name

# 停止服务
systemctl stop service_name

# 重启服务
systemctl restart service_name

# 重新加载配置
systemctl reload service_name

# 查看服务状态
systemctl status service_name

# 设置开机自启
systemctl enable service_name

# 禁用开机自启
systemctl disable service_name

4. 文件搜索

4.1 find 命令详解

按文件名搜索

1
2
3
4
5
6
7
8
# 基本语法
find [搜索路径] -name "文件名"

# 搜索示例
find / -name "nginx.conf" 2>/dev/null # 全局搜索配置文件
find . -name "*.py" # 当前目录搜索Python文件
find /home -name "*config*" 2>/dev/null # 搜索包含config的文件
find /usr -iname "NGINX*" 2>/dev/null # 忽略大小写搜索

按文件类型搜索

1
2
3
4
5
6
7
8
# 搜索目录
find /usr -type d -name "*mingw*" 2>/dev/null

# 搜索普通文件
find /var -type f -name "*.log" 2>/dev/null

# 搜索符号链接
find /usr -type l -name "*lib*" 2>/dev/null

按时间搜索

1
2
3
4
5
6
7
8
# 7天内修改的文件
find /home -type f -mtime -7

# 30天前的日志文件
find /var/log -name "*.log" -mtime +30

# 1小时内访问的文件
find /tmp -type f -amin -60

按大小搜索

1
2
3
4
5
6
7
8
# 大于100MB的文件
find /home -type f -size +100M

# 小于1KB的文件
find /tmp -type f -size -1k

# 查找空文件
find /var -type f -empty

组合搜索和操作

1
2
3
4
5
6
7
8
# 搜索并删除
find /tmp -name "*.tmp" -delete

# 搜索并执行命令
find /home -name "*.log" -exec gzip {} \;

# 搜索并显示详细信息
find /etc -name "*ssh*" -exec ls -la {} \;

4.2 其他搜索命令

locate 快速搜索

1
2
3
4
5
6
7
8
9
# 更新文件数据库
updatedb

# 快速搜索
locate filename
locate "*.conf"

# 限制搜索结果数量
locate -l 10 "*.log"

which 和 whereis

1
2
3
4
5
6
7
# 搜索可执行文件位置
which python
which gcc

# 搜索程序及相关文件
whereis nginx
whereis python

4.3 grep 文本搜索

基本文本搜索

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 在文件中搜索文本
grep "pattern" filename

# 递归搜索目录
grep -r "pattern" /path/to/directory

# 忽略大小写
grep -i "error" /var/log/*.log

# 显示行号
grep -n "function" script.py

# 显示匹配行的上下文
grep -C 3 "error" logfile.log

5. 系统监控

5.1 进程监控

查看进程

1
2
3
4
5
6
7
8
9
10
11
12
13
# 查看所有进程
ps aux

# 查看进程树
pstree

# 实时监控进程
top
htop # 增强版top

# 查看特定进程
ps aux | grep nginx
pgrep -f nginx

进程管理

1
2
3
4
5
6
7
# 终止进程
kill PID
kill -9 PID # 强制终止

# 通过名称终止进程
killall process_name
pkill -f process_name

5.2 系统资源监控

内存使用

1
2
3
# 查看内存使用情况
free -h # 人性化显示
cat /proc/meminfo # 详细内存信息

磁盘使用

1
2
3
4
5
6
7
# 查看磁盘使用情况
df -h # 磁盘空间使用
du -h /path/to/directory # 目录大小
du -sh * # 当前目录下各文件大小

# 查看最大的文件/目录
du -h /home | sort -hr | head -10

系统负载

1
2
3
4
5
6
7
# 查看系统负载
uptime
w # 查看登录用户和负载

# 查看CPU信息
lscpu
cat /proc/cpuinfo

6. 网络管理

6.1 网络连接查看

查看网络连接

1
2
3
4
5
6
7
8
9
# 查看所有网络连接
netstat -tulpn

# 查看特定端口
netstat -tlnp | grep :80
lsof -i :80

# 查看网络统计
ss -tulpn # 现代版netstat

网络测试

1
2
3
4
5
6
7
8
9
10
# 测试网络连通性
ping google.com
ping -c 4 192.168.1.1 # 发送4个包

# 测试端口连通性
telnet host port
nc -zv host port # netcat测试端口

# 路由追踪
traceroute google.com

6.2 防火墙管理

iptables 防火墙

1
2
3
4
5
6
7
8
# 查看防火墙规则
iptables -L -n

# 开放端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# 保存规则
service iptables save

firewalld 防火墙

1
2
3
4
5
6
7
8
9
# 查看防火墙状态
firewall-cmd --state

# 开放端口
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload

# 查看开放端口
firewall-cmd --list-ports

7. 软件包管理

7.1 yum/dnf 包管理(CentOS/RHEL/Fedora)

基本包操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 更新包列表
yum update
dnf update

# 安装软件包
yum install package_name
yum install -y package_name # 自动确认

# 搜索软件包
yum search keyword
yum list available | grep keyword

# 删除软件包
yum remove package_name

实用 yum 操作

1
2
3
4
5
6
7
8
9
# 安装开发工具
yum groupinstall "Development Tools"

# 安装常用工具
yum install -y wget curl vim git htop tree

# 查看已安装包
yum list installed
yum history # 查看安装历史

7.2 apt 包管理(Ubuntu/Debian)

基本包操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 更新包列表
apt update

# 升级系统
apt upgrade

# 安装软件包
apt install package_name
apt install -y package_name # 自动确认

# 搜索软件包
apt search keyword
apt list --installed | grep keyword

# 删除软件包
apt remove package_name
apt purge package_name # 删除包和配置文件

7.3 编译安装

从源码编译

1
2
3
4
5
6
7
8
9
10
11
12
13
# 下载源码
wget http://example.com/software.tar.gz
tar -zxvf software.tar.gz
cd software

# 编译三部曲
./configure --prefix=/usr/local/software
make
make install

# 设置环境变量
echo 'export PATH=/usr/local/software/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

8. 系统维护和故障排查

8.1 日志查看

系统日志

1
2
3
4
5
6
7
8
9
# 查看系统日志
journalctl # systemd日志
journalctl -f # 实时查看
journalctl -u service_name # 查看特定服务日志

# 传统日志位置
tail -f /var/log/messages # 系统消息
tail -f /var/log/syslog # 系统日志
tail -f /var/log/auth.log # 认证日志

8.2 性能调优

系统调优

1
2
3
4
5
6
7
8
9
# 查看系统信息
uname -a # 系统信息
cat /etc/os-release # 发行版信息
lsb_release -a # 发行版详细信息

# 查看硬件信息
lshw # 硬件信息
lspci # PCI设备
lsusb # USB设备

8.3 重定向和管道详解

文件描述符基础

1
2
3
4
# Linux 中的标准文件描述符
0 - stdin (标准输入) # 通常是键盘输入
1 - stdout (标准输出) # 通常是屏幕显示
2 - stderr (标准错误) # 通常是屏幕显示(错误信息)

输出重定向详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 标准输出重定向
command > output.txt # 覆盖写入到文件(等同于 1>)
command >> output.txt # 追加写入到文件(等同于 1>>)

# 错误输出重定向
command 2> error.log # 重定向错误输出到文件
command 2>> error.log # 追加错误输出到文件
command 2>/dev/null # 丢弃错误输出

# 同时重定向标准输出和错误输出
command > output.log 2>&1 # 先重定向 stdout,再将 stderr 重定向到 stdout
command 2>&1 > output.log # 错误的顺序!stderr 仍然输出到屏幕
command &> output.log # 简化写法(bash 4.0+)
command >& output.log # 另一种简化写法

& 符号的作用详解

1
2
3
4
5
6
7
8
9
10
# & 表示"文件描述符"
2>&1 # 将文件描述符2(stderr)重定向到文件描述符1(stdout)
1>&2 # 将文件描述符1(stdout)重定向到文件描述符2(stderr)

# & 在命令末尾表示后台运行
command & # 在后台运行命令
nohup command > log.txt 2>&1 & # 后台运行并重定向输出

# 实际示例
make 2>&1 | grep error # 将 stderr 合并到 stdout,然后通过管道传递给 grep

输入重定向

1
2
3
4
5
6
7
8
9
# 标准输入重定向
command < input.txt # 从文件读取输入
command << EOF # Here Document(多行输入)
line 1
line 2
EOF

# Here String
command <<< "single line input"

管道操作详解

1
2
3
4
5
6
7
8
9
# 基本管道(将前一个命令的 stdout 作为后一个命令的 stdin)
command1 | command2 # command1 的输出传递给 command2

# 多级管道
ps aux | grep nginx | awk '{print $2}' # 查找进程ID

# 管道与重定向结合
command1 | command2 > output.txt # 管道结果重定向到文件
command1 2>&1 | command2 # 包含错误输出的管道

实用的重定向和管道组合

1
2
3
4
5
6
7
8
9
10
11
# 编译项目并过滤错误
make 2>&1 | grep -i error # 查看编译错误
make 2>&1 | tee build.log | grep -i error # 保存日志同时显示错误

# 查看日志并过滤
tail -f /var/log/nginx/access.log | grep "404" # 实时查看404错误
journalctl -f | grep -i error # 实时查看系统错误

# 统计和分析
ps aux | wc -l # 统计进程数量
cat access.log | cut -d' ' -f1 | sort | uniq -c | sort -nr # 分析访问IP

8.4 grep 命令详解

grep 基本语法

1
grep [选项] 模式 [文件...]

常用选项说明

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
# 大小写控制
-i, --ignore-case # 忽略大小写
-v, --invert-match # 反向匹配(显示不匹配的行)

# 输出控制
-n, --line-number # 显示行号
-c, --count # 只显示匹配行的数量
-l, --files-with-matches # 只显示包含匹配的文件名
-L, --files-without-match # 只显示不包含匹配的文件名

# 上下文显示
-A NUM, --after-context=NUM # 显示匹配行后 NUM 行
-B NUM, --before-context=NUM # 显示匹配行前 NUM 行
-C NUM, --context=NUM # 显示匹配行前后各 NUM 行

# 递归搜索
-r, --recursive # 递归搜索目录
-R, --dereference-recursive # 递归搜索,跟随符号链接

# 正则表达式
-E, --extended-regexp # 使用扩展正则表达式
-F, --fixed-strings # 将模式视为固定字符串
-P, --perl-regexp # 使用 Perl 兼容正则表达式

# 颜色和格式
--color=always # 总是使用颜色
--color=auto # 自动使用颜色(默认)
--color=never # 不使用颜色

grep 实用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 基本文本搜索
grep "error" /var/log/messages # 搜索错误信息
grep -i "warning" *.log # 忽略大小写搜索警告
grep -n "function" script.py # 显示行号

# 上下文搜索
grep -A3 -B3 "error" build.log # 显示错误行前后3行
grep -C5 "exception" app.log # 显示异常行前后5行

# 多模式搜索
grep -E "(error|warning|fatal)" build.log # 搜索多个关键词
grep -E "^(ERROR|WARN)" application.log # 搜索行首的错误和警告

# 递归搜索
grep -r "TODO" /project/src/ # 递归搜索代码中的 TODO
grep -r -l "main" /usr/include/ # 查找包含 main 的头文件

# 反向搜索
grep -v "debug" app.log # 显示不包含 debug 的行
grep -v -E "^#|^$" config.file # 去除注释和空行

# 统计和计数
grep -c "error" build.log # 统计错误数量
grep -r -c "TODO" /project/src/ # 统计每个文件的 TODO 数量

正则表达式在 grep 中的应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 基本正则表达式
grep "^error" file.log # 匹配行首的 error
grep "error$" file.log # 匹配行尾的 error
grep "erro." file.log # . 匹配任意单个字符
grep "erro*" file.log # * 匹配前面字符0次或多次

# 扩展正则表达式(-E 选项)
grep -E "error|warning" file.log # 或操作
grep -E "error[0-9]+" file.log # 匹配 error 后跟数字
grep -E "(fatal|critical) error" file.log # 分组匹配

# 字符类
grep "[Ee]rror" file.log # 匹配 Error 或 error
grep "[0-9]+" file.log # 匹配数字
grep "[a-zA-Z]+" file.log # 匹配字母

编译项目中的 grep 应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 查找编译错误
make 2>&1 | grep -i error # 基本错误过滤
make 2>&1 | grep -i -A3 -B3 error # 显示错误上下文
make VERBOSE=1 2>&1 | grep -i error # 详细模式下查找错误

# 查找特定类型的错误
make 2>&1 | grep -i -E "(error|fatal|undefined)" # 多种错误类型
make 2>&1 | grep -i "undefined reference" # 链接错误
make 2>&1 | grep -i "file not found" # 文件未找到错误
make 2>&1 | grep -i -E "(clang|gcc).*error" # 编译器错误

# 过滤和统计
make 2>&1 | grep -i error | wc -l # 统计错误数量
make 2>&1 | grep -i warning | head -5 # 只看前5个警告
make 2>&1 | grep -i error | grep -v "error_code.h" # 排除误报

# 保存错误信息
make 2>&1 | tee build.log | grep -i error # 保存完整日志并显示错误
make 2>&1 | grep -i error > errors.txt # 只保存错误信息

高级 grep 技巧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 使用颜色高亮
grep -i --color=always error build.log | less -R # 在 less 中保持颜色

# 二进制文件搜索
grep -a "string" binary_file # 在二进制文件中搜索文本
grep -I "pattern" * # 跳过二进制文件

# 性能优化
grep -F "fixed_string" large_file # 固定字符串搜索(更快)
grep --mmap "pattern" large_file # 使用内存映射(大文件)

# 多文件搜索
grep -r --include="*.cpp" "pattern" /project/src/ # 只搜索 cpp 文件
grep -r --exclude="*.log" "pattern" /project/ # 排除 log 文件
grep -r --exclude-dir="build" "pattern" /project/ # 排除 build 目录

实际项目中的应用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
# CMake 项目调试
cmake .. 2>&1 | grep -i -E "(error|warning|not found)" # 配置阶段错误
make VERBOSE=1 2>&1 | grep -i -A2 -B2 error # 编译错误详情
make 2>&1 | grep -i "undefined reference" # 链接错误

# 代码审查
grep -r -n "TODO" /project/src/ # 查找待办事项
grep -r -n "FIXME" /project/src/ # 查找需要修复的代码
grep -r -E "(printf|cout)" /project/src/ --include="*.cpp" # 查找调试输出

# 日志分析
tail -f app.log | grep -i --color=always -E "(error|exception|fail)" # 实时错误监控
grep -E "^[0-9]{4}-[0-9]{2}-[0-9]{2}" app.log | grep -i error # 按日期过滤错误

管道和重定向的组合应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 复杂的日志分析管道
cat access.log | \
grep "POST" | \ # 过滤 POST 请求
awk '{print $1}' | \ # 提取 IP 地址
sort | \ # 排序
uniq -c | \ # 统计重复次数
sort -nr | \ # 按数量倒序
head -10 # 显示前10个

# 编译错误分析管道
make 2>&1 | \
grep -i error | \ # 过滤错误
sed 's/.*error: //' | \ # 提取错误信息
sort | \ # 排序
uniq -c | \ # 统计相同错误
sort -nr # 按频率排序

# 系统监控管道
ps aux | \
grep -v grep | \ # 排除 grep 进程本身
awk '{print $3, $11}' | \ # 提取 CPU 使用率和命令
sort -nr | \ # 按 CPU 使用率排序
head -5 # 显示前5个进程

重定向的高级用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 分别处理输出和错误
command > success.log 2> error.log # 分别保存到不同文件

# 同时显示和保存
command 2>&1 | tee output.log # 显示在屏幕上同时保存到文件

# 条件重定向
if make 2>&1 | grep -q error; then # -q 静默模式,只返回状态码
echo "Build failed"
make 2>&1 | grep -i error > errors.txt
else
echo "Build successful"
fi

# 多命令重定向
{
echo "Build started at $(date)"
make 2>&1
echo "Build finished at $(date)"
} | tee build.log # 整个代码块的输出都保存到日志

实际编译项目中的应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 编译并过滤错误(推荐用法)
make 2>&1 | grep -i -A3 -B3 error # 显示错误及上下文

# 编译并保存完整日志
make VERBOSE=1 2>&1 | tee build.log | grep -i -E "(error|warning)"

# 只查看错误,忽略警告
make 2>&1 | grep -i error | grep -v warning

# 统计编译结果
echo "Errors: $(make 2>&1 | grep -i -c error)"
echo "Warnings: $(make 2>&1 | grep -i -c warning)"

# 清理重建并监控错误
rm -rf build/ && mkdir build && cd build && \
cmake .. 2>&1 | grep -i error && \
make 2>&1 | grep -i -A2 error

# 交叉编译特定的错误过滤
make 2>&1 | grep -i -E "(error|fatal|undefined|cannot find)" | \
grep -v "error_code" | \ # 排除误报
head -10 # 只看前10个错误

grep 在代码开发中的应用

1
2
3
4
5
6
7
8
9
10
11
12
13
# 代码搜索和分析
grep -r -n "std::min" /project/src/ # 查找 std::min 的使用
grep -r -E "(TODO|FIXME|HACK)" /project/src/ # 查找代码注释标记
grep -r -l "#include.*string" /project/src/ # 查找包含 string 头文件的文件

# 查找函数定义和调用
grep -r -n "^.*function_name.*{" /project/src/ # 查找函数定义
grep -r -n "function_name(" /project/src/ # 查找函数调用

# 查找编译器和链接器问题
make VERBOSE=1 2>&1 | grep -E "(clang|gcc|ld).*error" # 编译器错误
make 2>&1 | grep -i "undefined reference" | head -5 # 链接错误
make 2>&1 | grep -i "file not found" | sort | uniq # 文件未找到错误

调试技巧组合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 实时监控编译过程
make 2>&1 | while read line; do
echo "$line"
if echo "$line" | grep -q -i error; then
echo "*** ERROR DETECTED ***" >&2
fi
done

# 编译错误快速定位
make 2>&1 | grep -i -n -A5 -B5 error | less # 在 less 中查看错误详情

# 批量文件错误检查
for file in *.cpp; do
echo "Checking $file:"
grep -n -i -E "(error|warning|todo)" "$file"
done

这些重定向、管道和 grep 的技巧在日常开发和系统维护中非常实用,特别是在调试编译问题和分析日志时。


9. 常用命令速查表

文件操作

1
2
3
4
5
6
7
ls -la                        # 详细列出文件
mkdir -p path/to/dir # 创建多层目录
rm -rf directory # 强制删除目录
cp -r source dest # 递归复制
mv source dest # 移动/重命名
chmod 755 file # 修改权限
chown user:group file # 修改所有者

文本处理

1
2
3
4
5
6
cat file                     # 显示文件内容
head -n 10 file # 显示前10行
tail -f file # 实时监控文件
grep pattern file # 搜索文本
sed 's/old/new/g' file # 替换文本
awk '{print $1}' file # 提取列

系统信息

1
2
3
4
5
6
7
ps aux                       # 查看进程
top # 实时进程监控
df -h # 磁盘使用情况
free -h # 内存使用情况
uptime # 系统运行时间
whoami # 当前用户
id # 用户ID信息

网络相关

1
2
3
4
5
6
ping host                    # 测试网络连通性
netstat -tulpn # 查看网络连接
wget url # 下载文件
curl url # 发送HTTP请求
ssh user@host # SSH连接
scp file user@host:/path # 安全复制文件

10. 实际应用场景

10.1 服务器维护脚本

系统监控脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
# 系统监控脚本

echo "=== 系统信息 ==="
uptime
echo ""

echo "=== 磁盘使用 ==="
df -h
echo ""

echo "=== 内存使用 ==="
free -h
echo ""

echo "=== 进程TOP 10 ==="
ps aux --sort=-%cpu | head -11

日志清理脚本

1
2
3
4
5
6
7
8
#!/bin/bash
# 清理7天前的日志文件

LOG_DIR="/var/log"
DAYS=7

find $LOG_DIR -name "*.log" -mtime +$DAYS -delete
echo "清理完成:删除了 $DAYS 天前的日志文件"

10.2 常见问题解决

磁盘空间不足

1
2
3
4
5
6
7
8
9
# 查找大文件
find / -type f -size +100M 2>/dev/null | head -20

# 清理临时文件
rm -rf /tmp/*
rm -rf /var/tmp/*

# 清理日志文件
journalctl --vacuum-time=7d # 保留7天的日志

进程占用过高

1
2
3
4
5
6
7
8
# 找出CPU占用高的进程
ps aux --sort=-%cpu | head -10

# 找出内存占用高的进程
ps aux --sort=-%mem | head -10

# 结束异常进程
kill -9 PID