我想递归地添加以.ts以下文本结尾的所有文件:export {};并且我需要至少排除文件夹node_modules.
我试过:
find . f ! -path '*/node_modules/*' -iname "*.ts" -exec sed -e '1 i\export {}' \;
Run Code Online (Sandbox Code Playgroud)
结果:对于每个文件我得到:sed: 1: "1 i\export ./src/query/ ...": extra characters after \ at the end of i command
更新:我gnu-sed在 macOS 上安装并尝试使用gsed。但它似乎不起作用——它似乎无限运行。
由于:
该字符串
'{}'将被替换为当前文件名, 该文件名出现在命令的参数中,而不仅仅是出现在单独的参数中,如某些版本的 find 中。这两种结构可能都需要转义(用\)或引用,以保护它们免受 shell 的扩展。
使用以下方法:
find . -type f ! -path '*/node_modules/*' -iname "*.ts" -exec sed -i '1 i\export\{\}' {} \;
Run Code Online (Sandbox Code Playgroud)
-i, --in-place-sed就地编辑文件的选项