Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sed creates un-deleteable files in Windows

I'm trying to run the following command in Windows Server 2003 but sed creates a pile of files that I can't delete from the command line inside the current directory.

for /R %f in (*.*) do "C:\Program Files\gnuwin32\bin\sed.exe" -i "s/bad/good/g" "%f"

Does anyone have any suggestions? Mysteriously enough, I'm able to delete the files using Windows Explorer.

As requested, here are some example filenames:

  • sed0E3WZJ
  • sed5miXwt
  • sed6fzFKh

And, more troubleshooting info...

  • It occurs from both the command prompt & batch files
  • If I just need to run sed on a single directory, then I use sed "s/bad/good/g" *.* and everything is OK. Alas, I also need it to tackle all the subdirectories.
  • I only have Sed installed.
  • Sed is creating the files
like image 700
Zian Choy Avatar asked Dec 01 '09 02:12

Zian Choy


3 Answers

I have replicated your setup and I have the following observations.

  1. I dont think there is a problem in the loop. The simple command "C:\Program Files\gnuwin32\bin\sed.exe" -i "s/bad/good/g" . - creates the same set of temporary files.
  2. The files are indeed created by sed. sed creates these temporary files when the "in place" (-i) option is turned on. In the normal course, sed actually deletes the files (that is what happens in cygwin) using a call to the 'unlink' library. In case of gnuwin32, it looks like the 'unlink' fails. I have not been able to figure out why. I took a guess that maybe the unlink call is dependent on the gnuwin32 'coreutils' library and tried to download and install the coreutils library - no dice.
  3. If you remove the 'read-only' restriction in the parent folder before executing the sed command, you can delete the temporary files from windows command prompt. So that should give you some temporary respite.

I think we now have enough information to raise a bug report. If you agree, I think it may be a good idea to bring it to the notice of the good folks responsible for gnuwin32 and ask them for help.

Meanwhile, the following version cleans up its temporary file:

  • https://github.com/mbuilov/sed-windows
like image 132
NP Compete Avatar answered Oct 24 '22 08:10

NP Compete


As this is a known bug in sed with the -i option you can run attrib -R <filename> to remove the read only attribute from file after sed completes.

Alternatively do not use the -i option and redirect the output to a new file and then delete and rename the input and output.

like image 3
darrenmc Avatar answered Oct 24 '22 09:10

darrenmc


Cygwin hoses the ACLs on files sometimes, you'll probably have to use cacls or chmod to fix it up before you can delete the file.

like image 1
Ana Betts Avatar answered Oct 24 '22 08:10

Ana Betts