问题
I'm using mingw.
$ sed -i "s/a/b/" test.txt
sed: preserving permissions for `./sed003480': Permission denied
I can touch/rm files in current dir.
回答1:
It's caused by Windows security settings.
Open the folder's Properties settings from the context menu. In the Security tab, click Edit, press Add... in the pop-up window and add your user to the list, check Full Control in the Allow column. Press OK twice to apply the changes.
回答2:
For me, folder was read only. unchecking readonly option fix my problem. Thanks to Zenadix commment
回答3:
On Windows 10 WSL i got similar issue. The issue was caused by VS code being open and apparently using the file. Closing VS code solved my issue.
回答4:
TL;DR: Used sudo
In my case, I was running a bash script running on Windows Subsystem for Linux (WSL) as follows:
./generate.sh
The script created a bunch of files however, for reasons unknown, sed was denied permission to modify the permissions on these files for some reason:
The actual sed command was used to perform a search-replace that looked a bit like this:
grep -rl $PATTERN $PUBLIC_API_FOLDER/ \
| xargs sed -i 's/$PATTERN/$REPLACE/g'
As you can see, despite not having elevated priviledges at any point, I nevertheless was unable to modify the files the script created:
sed: preserving permissions for ‘public_api/models/sedt0qk4D’: Operation not permitted
sed: preserving permissions for ‘public_api/models/sedOxoc1O’: Operation not permitted
sed: cannot rename public_api/models/sedOxoc1O: Permission denied
sed: preserving permissions for ‘public_api/controllers/sedx5BafW’: Operation not permitted
sed: preserving permissions for ‘public_api/models/sedVkdqzc’: Operation not permitted
sed: preserving permissions for ‘public_api/models/sedLvyS3s’: Operation not permitted
sed: cannot rename public_api/models/sedLvyS3s: Permission denied
sed: preserving permissions for ‘public_api/controllers/sedE7GSe8’: Operation not permitted
sed: cannot rename public_api/controllers/sedE7GSe8: Permission denied
sed: preserving permissions for ‘public_api/controllers/sednHZBQf’: Operation not permitted
sed: cannot rename public_api/controllers/sednHZBQf: Permission denied
The solution in my case was to simply run the script with sudo.
sudo ./generate.sh
来源:https://stackoverflow.com/questions/14313318/permission-denied-when-sed-in-place-edit-in-mingw