You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用sedfile递归修改目录文件时如何忽略文件名中的空格?

正确执行命令

你出现的问题来自两个语法错误,修正后即可同时兼容带空格的文件名、批量加载sedfile中的400条替换规则:

1. Linux 环境(GNU Sed)

find . -type f -name '*.md' -print0 | xargs -0 sed -i -f sedfile

2. macOS 环境(BSD Sed)

macOS 自带的 sed 要求-i参数必须指定备份文件后缀,不需要备份的话填写空字符串即可:

find . -type f -name '*.md' -print0 | xargs -0 sed -i '' -f sedfile

之前的报错原因

  • find命令的print0参数前缺少短横线,正确写法是-print0,你之前的写法会让find把print0识别为路径参数,直接触发路径错误。
  • 你在sed命令末尾额外加了*.md参数,xargs已经会自动把find查到的所有文件路径传给sed,额外加的通配符不仅冗余,还可能导致重复处理、或者当前目录无md文件时触发额外报错。

替代写法(无需xargs)

如果不想使用xargs,也可以直接用find的-exec参数实现相同效果,同样兼容带空格的文件名:

Linux 环境

find . -type f -name '*.md' -exec sed -i -f sedfile {} +

macOS 环境

find . -type f -name '*.md' -exec sed -i '' -f sedfile {} +

内容的提问来源于stack exchange,提问作者jwl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 22:57:04