Faculty of Engineering and Mathematical Sciences 
Not logged in (login)

help4407


This forum is provided to promote discussion amongst students enrolled in Open Source Tools and Scripting (CITS4407).

Assignment 2 and Clarifications.
 
Options:
RSS cloud
Jump to:

sed changing rw to r permissions on files in WSL

7 of 390 articles shown, currently no other people reading this forum.
photo
From: Alastair H.
Date: Thu 21st May, 9:55pm
Actions: 
        Login-to-reply
I'm in WSL and experiencing an issue with sed - with -i to edit inline and overwrite 
the file ( I believe it does this by creating a temp file initially...) the 
permissions of the file in question change from rw to read only. Is this normal 
behaviour? 

example for "testfile" (rw permissions): sed -i "s/cat/dog/" testfile
returns 
"preserving permissions for 'tempfilename......"
now, "testfile" has read only permissions. 

sed changing rw to r permissions on files in WSL

photo
From: Maktoom A.
Date: Thu 21st May, 11:38pm
Actions: 
        Login-to-reply
"Alastair Haldane"                               wrote:

> I'm in WSL and experiencing an issue with sed - with -i to edit inline and overwrite 
> the file ( I believe it does this by creating a temp file initially...) the 
> permissions of the file in question change from rw to read only. Is this normal 
> behaviour? 
> 
> example for "testfile" (rw permissions): sed -i "s/cat/dog/" testfile
> returns 
> "preserving permissions for 'tempfilename......"
> now, "testfile" has read only permissions. 

There's a few things. One, make sure you don't already have the file open i.e. some other program is using the file. 

Otherwise, its likely caused by the Windows Security Settings. Make sure your user has Full Control on the folder, this can be done in the Properties > Security tab. 

sed changing rw to r permissions on files in WSL

photo
From: Alastair H.  O.P.
Date: Fri 22nd May, 5:12am
Actions: 
        Login-to-reply
"Maktoom Ahmed"                               wrote:

> "Alastair Haldane"                               wrote:
> 
> > I'm in WSL and experiencing an issue with sed - with -i to edit inline and overwrite 
> > the file ( I believe it does this by creating a temp file initially...) the 
> > permissions of the file in question change from rw to read only. Is this normal 
> > behaviour? 
> > 
> > example for "testfile" (rw permissions): sed -i "s/cat/dog/" testfile
> > returns 
> > "preserving permissions for 'tempfilename......"
> > now, "testfile" has read only permissions. 
> 
> There's a few things. One, make sure you don't already have the file open i.e. some other program is using the file. 
> 
> Otherwise, its likely caused by the Windows Security Settings. Make sure your user has Full Control on the folder, this can be done in the Properties > Security tab. 

Thanks Maktoom, if I run the command with sudo - it works fine, and doesn't change the file permissions. So yes it would seem that the temp file sed creates is tripping me 
up somewhere in windows file permissions.

sed changing rw to r permissions on files in WSL

photo
From: Christopher M.
Date: Fri 22nd May, 8:11am
Actions: 
        Login-to-reply
"Alastair Haldane"                               wrote:

> Thanks Maktoom, if I run the command with sudo - it works fine, and doesn't change the file permissions. So yes it would seem that the temp file sed creates is tripping me 
> up somewhere in windows file permissions.


I can't replicate the problem on macOS or Linux  (even while simultaneously editing the file),
so it does sound like Windows doing strange things.

It's a 'common/boring' kind of operation, but I can't find related discussion about it online.

sed changing rw to r permissions on files in WSL

photo
From: Lee dB.
Date: Fri 22nd May, 8:29am
Actions: 
        Login-to-reply
As far as I know, the -i flag in sed works by writing the modified stream to a temporary file, then replacing the original file with the temporary file. I don’t know if this is done by 
overwriting the contents of the existing file, or replacing the file entirely. Either way, I wonder if there’s some strange interaction going on between the Windows filesystem Access 
Control List and the Linux Subsystem? 

Or maybe it’s an issue with your umask environment variable causing the new (replacement) file to have the wrong permissions, though I would have thought sed would be designed to preserve 
the mode of the file regardless of how it operates internally. 

sed changing rw to r permissions on files in WSL

photo
From: Alastair H.  O.P.
Date: Fri 22nd May, 9:53am
Actions: 
        Login-to-reply
"Lee de Byl"                               wrote:

> As far as I know, the -i flag in sed works by writing the modified stream to a temporary file, then replacing the original file with the temporary file. I don’t know if this is done by 
> overwriting the contents of the existing file, or replacing the file entirely. Either way, I wonder if there’s some strange interaction going on between the Windows filesystem Access 
> Control List and the Linux Subsystem? 
> 
> Or maybe it’s an issue with your umask environment variable causing the new (replacement) file to have the wrong permissions, though I would have thought sed would be designed to preserve 
> the mode of the file regardless of how it operates internally. 

Thanks all for your input, it does seem to be an issue with file permissions in windows, independent of Bash. Folders marked read only, revert back to read only immediately after being unchecked, 
so it could have something to do with a recent update. I can work around it for now. 

sed changing rw to r permissions on files in WSL

photo
From: Christopher M.
Date: Fri 22nd May, 9:55am
Actions: 
        Login-to-reply
"Alastair Haldane"                               wrote:

> Thanks all for your input, it does seem to be an issue with file permissions in windows, independent of Bash. Folders marked read only, revert back to read only immediately after being unchecked, 
> so it could have something to do with a recent update. I can work around it for now. 

For the relatively small files we're working with, you can just use:

  sed .... < infield > outfile && mv outfile infile
This Page


Program written by: [email protected]
Feedback welcome
Last modified:  8:27am May 24 2020