21

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
Zian Choy
  • 2,846
  • 6
  • 33
  • 64
  • It would help if you showed an example filename. I tried your command with "c:\cygwin\bin\sed.exe" and it worked fine other than a warning about backslashes vs. slashes. – Dennis Williamson Dec 01 '09 at 07:45
  • sed -i actually works quite well for me if the file is not read-only to begin with. No undeleteable temporary files leftover and no errors. I should mention I am using it in combination with find instead of in a for loop. – mejdev Dec 05 '14 at 18:01

9 Answers9

26

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:

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
NP Compete
  • 2,369
  • 2
  • 16
  • 15
  • 4
    It's a known bug (http://sourceforge.net/projects/gnuwin32/forums/forum/74807/topic/845128). I've filed a bug report but I'm not hopeful (https://sourceforge.net/tracker/?func=detail&aid=2931036&group_id=23617&atid=379173). – Zian Choy Jan 13 '10 at 00:02
3

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.

darrenmc
  • 1,721
  • 1
  • 19
  • 29
1

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.

Ana Betts
  • 73,868
  • 16
  • 141
  • 209
1

Here is where a bit of troubleshooting comes into play. Does this happen when you run that command from the command-line and a batch file? What if you run sed on an individual file on the command line - does it create these files for every file, or just certain files/filetypes? Does it only happen for that replacement, or all replacements in general, or just always when you run sed.exe on a file? Is it only sed creating these files, or all Gnuwin32 exe's (eg. awk, cat, etc)? Does the same thing happen on a sed.exe from a new install of Gnuwin32? What error message does it give when you try to delete the files? Can you delete the files from explorer while the command prompt is still open? What if you close the command prompt and reopen it, then try to delete the files?

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
0

you can just run sed without for loop

c:\test> sed -i.bak "s/bad/good/g" file*.*
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
  • Just tried that (minus the "file") in a folder named "Test" with a subdirectory named "New Folder" and I get the following error: C:\Documents and Settings\zchoy\Desktop\Test>"C:\Program Files\gnuwin32\bin\sed.exe" -i.bak "s/bad/good/g" *.* sed.exe: can't read New Folder: Permission denied I also got a garbage file: sed3aQhvO – Zian Choy Dec 02 '09 at 01:51
0

This is a stab in the dark, but it wouldn't surprise me in the least if the gnuwin32 implementation of sed is duff (i.e., faulty in some way). Can you try to replicate the problem using the AT&T U/Win POSIX support for windows? It is easy to install and includes the Korn shell, sed, and find, so you can use find instead of the FOR /R. (I'm wondering if part of the problem is that the MS FOR and gnuwin32 sed don't play nicely together.)

Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
0

I realize this is an old thread but It's still an issue. My fix is to add "DEL sed*" to the end of a batch file after sed. Quick and dirty.

Adrian R
  • 11
  • 1
  • I thought of that, unfortunately, this band-aid won't work for me for two reasons: 1. I have files in my project that have a name starting with sed. 2. Any developer working on my project can add additional files that have a name starting with sed. – Jesus H May 11 '17 at 20:18
0

I am using this command to clean up the temporary files created by gnuwin32's sed:

FOR /f "tokens=*" %%a in ('dir /b ^| findstr /i "^sed[0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z]$"') DO del %%a
Seb T
  • 795
  • 8
  • 16
0

i know this is old. But just want to share with people what I did that cause this.

It was in fact the temp file for an already open file through gvim example .swap file that causing the sed tmp file didnt get remove completely.

So sed trying to read and append into the opened file which the user is currently viewing and having trouble doing it which causes the tmp file error.