251 字
1 分钟
Linux find 批量替换字符脚本
批量替换脚本(Ubuntu 直接执行,在 ~/Astro 目录运行)
1. 先预览替换(不修改文件,先看结果)
cd /home/ubuntu/Astro/src/content/postsfind . -name "*.md" -exec grep 'tags: \["数据库与监控"' {} \;2. 正式批量替换
find . -type f -name "*.md" -exec sed -i 's/tags: \["数据库与监控", "运维", "数据库"\]/tags: \["zabbix监控", "运维", "数据库"\]/g' {} \;说明:
- `find . -type f -name "*.md"`:遍历所有子目录全部md文章- `sed -i`:原地修改文件- 精准整串匹配:`["数据库与监控", "运维", "数据库"]` → `["zabbix监控", "运维", "数据库"]`3. 修改完校验
# 查看修改后find . -name "*.md" -exec grep 'tags: \["zabbix监控"' {} \;# 确认旧标签不存在find . -name "*.md" -exec grep 'tags: \["数据库与监控"' {} \;4. 重新打包部署
cd /home/ubuntu/Astronpm run build拓展:如果是模糊替换(只要出现「数据库与监控」就改成zabbix监控,不管数组顺序)
find . -type f -name "*.md" -exec sed -i 's/数据库与监控/zabbix监控/g' {} \; Linux find 批量替换字符脚本
https://fuwari.vercel.app/posts/指南/linux-find-批量替换字符脚本/