侧边栏壁纸
博主头像
钟小言

致力于为您提供丰富而有趣的内容,旨在启发思考、分享知识。

  • 累计撰写 21 篇文章
  • 累计收到 4 条评论

最常用且实用性最高**的Linux Shell脚本

2025-4-17 / 0 评论 / 5579 阅读

以下是20个最常用且实用性最高的Linux Shell脚本示例,涵盖系统管理、文件操作和自动化运维场景:

1. 自动备份目录

#!/bin/bash
# 压缩备份指定目录到目标路径
backup_dir="/path/to/source"
dest_dir="/path/to/backup"
tar -czf "${dest_dir}/backup_$(date +%Y%m%d).tar.gz" "$backup_dir"
echo "备份已完成:${dest_dir}/backup_$(date +%Y%m%d).tar.gz"

2. 监控CPU和内存使用

#!/bin/bash
# 资源使用率告警(阈值可调)
cpu_threshold=80
mem_threshold=90

cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1)
mem_usage=$(free | grep Mem | awk '{print $3/$2 * 100}' | cut -d'.' -f1)

[ "$cpu_usage" -ge "$cpu_threshold" ] && echo "警告:CPU使用率 ${cpu_usage}%"
[ "$mem_usage" -ge "$mem_threshold" ] && echo "警告:内存使用率 ${mem_usage}%"

3. 清理旧日志文件

#!/bin/bash
# 删除超过30天的日志
find /var/log -name "*.log" -type f -mtime +30 -delete
echo "已清理30天前的日志文件"

4. 检查磁盘空间

#!/bin/bash
# 检测磁盘使用率超过90%的分区
df -h | awk '{if($5+0 > 90) print "磁盘告警: " $6 " 使用率 " $5}'

5. 批量重命名文件

#!/bin/bash
# 将所有.txt文件后缀改为.md
for file in *.txt; do
  mv "$file" "${file%.txt}.md"
done
echo "批量重命名完成"

6. 网络连通性测试

#!/bin/bash
# 测试是否能访问外网和DNS
ping -c 2 8.8.8.8 > /dev/null && echo "外网连通正常" || echo "外网连接失败"
ping -c 2 example.com > /dev/null && echo "DNS解析正常" || echo "DNS解析失败"

7. 自动更新系统(APT)

#!/bin/bash
# Debian/Ubuntu系统自动更新
apt update && apt upgrade -y && apt autoclean

8. 服务状态监控

#!/bin/bash
# 检查Nginx是否运行,自动重启
if ! systemctl is-active --quiet nginx; then
  systemctl start nginx
  echo "Nginx已重启"
fi

9. 生成随机密码

#!/bin/bash
# 生成12位强密码(需安装openssl)
password=$(openssl rand -base64 12 | tr -d '/+=' | cut -c1-12)
echo "随机密码: $password"

10. 查找大文件(>100MB)

#!/bin/bash
# 搜索系统中大于100MB的文件
find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null

11. 同步目录(增量备份)

#!/bin/bash
# 使用rsync同步目录到远程服务器
rsync -avz --delete /local/path/ user@remote:/remote/path/

12. 批量杀进程

#!/bin/bash
# 强制结束所有匹配进程名的进程
pkill -9 -f "process_name"

13. 统计文件行数

#!/bin/bash
# 统计当前目录所有文本文件行数
wc -l *.txt

14. 自动添加定时任务

#!/bin/bash
# 每天凌晨3点执行备份脚本
(crontab -l 2>/dev/null; echo "0 3 * * * /path/to/backup.sh") | crontab -
echo "定时任务已添加"

15. 检测HTTP服务状态

#!/bin/bash
# 检查网站是否返回200状态码
status=$(curl -s -o /dev/null -w "%{http_code}" https://example.com)
[ "$status" = "200" ] && echo "服务正常" || echo "服务异常(状态码: $status)"

16. 用户登录审计

#!/bin/bash
# 显示最近10条SSH登录记录
last -n 10 | grep -E "pts/"

17. 文件权限修复

#!/bin/bash
# 递归修复目录权限(目录755,文件644)
find /path/to/fix -type d -exec chmod 755 {} \;
find /path/to/fix -type f -exec chmod 644 {} \;
echo "权限修复完成"

18. 磁盘IO监控

#!/bin/bash
# 检测高磁盘IO进程(需安装iotop)
iotop -obn2 | head -n 6

19. 自动配置SSH密钥

#!/bin/bash
# 生成密钥并复制到远程主机(需输入密码)
ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
ssh-copy-id user@remote_host

20. 交互式菜单

#!/bin/bash
# 基础命令行菜单
echo "1. 查看系统信息"
echo "2. 显示磁盘使用"
echo "3. 退出"
read -p "输入选项: " choice
case $choice in
  1) neofetch ;;
  2) df -h ;;
  3) exit ;;
  *) echo "无效选项" ;;
esac

使用说明

  1. 将脚本保存为 .sh 文件(例如 script.sh
  2. 添加执行权限:chmod +x script.sh
  3. 运行脚本:./script.sh

适用场景

  • 日常系统维护
  • 自动化任务(备份/监控/清理)
  • 故障排查(网络/磁盘/服务)
  • 批量文件操作

可根据实际需求调整路径、阈值和命令参数。

收藏

扫描二维码,在手机上阅读

评论一下?

OωO
取消