在不指定备份文件的情况下进行就地编辑会覆盖只读权限
sed -i -e cmd file 将修改 file,即使其权限设置为只读。
此命令的行为类似于
sed -e cmd file > tmp; mv -f tmp file
而不是
sed -e cmd file > tmp; cat tmp > file; rm tmp
以下示例使用 gnu sed:
$ echo 'Extremely important data' > input
$ chmod 400 input # Protect that data by removing write access
$ echo 'data destroyed' > input
-bash: input: Permission denied
$ cat input
Extremely important data (#phew! Data is intact)
$ sed -i s/important/destroyed/ input
$ cat input
Extremely destroyed data (#see, data changed)
通过使用 i 选项指定 SUFFIX 来创建备份可以减轻这种情况:
$ sed -i.bak s/important/destroyed/ input
$ cat input
Extremely destroyed data
$ cat input.bak
Extremely important data