Cygwin bash sed locks my files

corresponding to Cygwin Mailinglist, use noacl for mounting

$ mount -o binary,posix=0,user,noacl C:/tmp /mnt/tmp

or directly in Windows ../cygwin/fstab before starting Cygwin bash

C:/tmp /mnt/tmp ntfs binary,posix=0,user,noacl 0 0

I had exactly the same trouble after using find in combination with grep and sed under cygwin to process a folder full of Java source code files automatically. Sed messed up the NTFS file permissions of my files, which was still persistent after a reboot, so I searched for an easy way to fix this.

I actually found an easy way to get the file permissions back to the old values. It can be done for a whole folder recursively, which is important to me, since I have modified so many files at once (several hundred). Sadly, I only know the german names for the menu entries, so I'm not sure how the menu entries (in the Windows Explorer) are called in correct english.

In german, you do: Rightclick on the parent folder of the folder where I used sed, "Eigenschaften" (context menu), "Sicherheit" (tab in the dialog), "Erweitert" (button), "Berechtigungen ändern..." (button with UAC prompt), now I just check both checkboxes (the upper one is set already, but the lower is not, but has to be). Now I click on "OK" for three times.

The actual change that is done by this is that the lower checkbox forces Windows to replace the current permissions for the folder contents recursively with the permissions of the folder you rightclicked, so all broken permissions are fixed. Worked like a charm for me, and is also really quick (only a few seconds for a lot of files).

Possible translations (guessed by me):

  • "Eigenschaften" -> "Properties"
  • "Sicherheit" -> "Security"
  • "Erweitert" -> "Advanced"
  • "Berechtigungen ändern..." -> "Change permissions..."

I ran my own set of tests, and, indeed, the permissions of the file do get set to r-xr-x--- after the sed command and subsequent cygwin commands will perceive this as a read only file.

C:\Temp>echo aaa > test.txt
C:\Temp>ls -l
total 1
----------+ 1 bikeguy ???????? 6 Feb 21 08:51 test.txt
C:\temp>sed -i 's/aaa/bbb/' test.txt
C:\temp>cat test.txt
bbb
C:\temp>ls -l test.txt
-r-xr-x---+ 1 bikeguy mkgroup-l-d 5 Feb 21 08:52 test.txt
C:\temp>

These -r-xr-x--- permissions do prevent subsequent writes by some software:

C:\temp>echo xxx > test.txt
Access is denied.
C:\temp>

So, the workaround would be to add an additional chmod on the file

C:\temp>chmod 777 test.txt
C:\temp>echo xxx > test.txt
C:\temp>