EM0
EM0

Reputation: 6327

Delete a file named "NUL" on Windows

I ran a program on Windows 7 that was compiled under Cygwin and passed "NUL" as an output file name. Instead of suppressing output it actually created a file named "NUL" in the current directory. (Apparently it expects "/dev/null", even on Windows.) Now I'm stuck with this "NUL" file that I cannot delete!

I've already tried:

How can I get rid of these NUL files (I have several by now), short of installing the full Cygwin environment and compiling a C program under Cygwin to do it?

Upvotes: 101

Views: 33699

Answers (9)

Xeem_Pad
Xeem_Pad

Reputation: 1

C:> rename \.\C:..\NUL. deletefile.txt
C:> del deletefile.txt

This command from user xxbbcc was kinda good, but the comments were even more useful. Just wanna point out, that this command didn't help me, but tip from the guy named Eryk Sun did:

It's better in general to use "\?" instead of "\." because the question-mark prefix bypasses all normalization, including stripping trailing spaces and dots.

So the best command would be as follows:

rename "\\?\C:\..\<broken filename>" deletefile.txt
del deletefile.txt

Or it could be even shorter:

del "\\?\C:\..\<broken filename>"

Upvotes: -1

Jo&#227;o Frigo
Jo&#227;o Frigo

Reputation: 41

I was having this same issue.
If you have WSL2 installed just go to that directory and run:

rm -f nul

In my case the file was lowercase. You should be good.

Upvotes: 4

hitesh_fulwani
hitesh_fulwani

Reputation: 81

If you have git on windows, just right click on the folder containing the nul file -> go to gitbash here -> and type "rm nul" or "rm nul.json" depending upon the file type.

Upvotes: 8

Geovani Martinez
Geovani Martinez

Reputation: 2143

If you have Git for Windows Installed (v2.18) do the following

  1. Open the directory containing the files you want to remove
  2. Left Click and select Git Bash Here
  3. Type rm nul.json at the command prompt and hit ENTER, the file now should be removed.

run Git Bash Here

NOTE: These screenshots show the removal of file nul.topo.json which is another file that I could not removed with a simple delete.

after command execution

Upvotes: 47

Olivier
Olivier

Reputation: 41

To remove a nul file situated here: C:\unix\cygwin\dev\nul

I simply use (tested only on Windows 10) : Del \?\C:\unix\cygwin\dev\NUL

Upvotes: 2

spryce
spryce

Reputation: 607

I solved this in a slightly different way.

I thought I would add this here because it is high in the google results and I had a similar issue for a folder named NUL.

I tried rmdir\\?\C:\My\Path\NUL and rmdir\\.\C:\My\Path\NUL without any success and also tried several commands using bash from my SourceTree installation. No joy.

In the end I used DIR /X /A from cmd to list the short names in the parent directory. This showed me NUL~1 for my NUL folder and identified the crux of the problem.

This was then used in the standard command rmdir /s NUL~1 and finally resolved the issue.

Upvotes: 1

Ben Jacobs
Ben Jacobs

Reputation: 2526

I had a similar issue. It was probably caused by Cygwin as well (since I use this regularly), but I've since forgotten exactly how the file was created.

I also had trouble deleting it. I followed the advice of some other posts and tried booting into safe mode to delete the file, though this did nothing. The tip from +xxbbcc didn't work for me either.

However, I was able to delete the file using the Cygwin terminal! Cygwin createth and Cygwin destroyeth.

Upvotes: 4

xxbbcc
xxbbcc

Reputation: 17327

Open a command prompt and use these commands to first rename and then delete the NUL file:

C:\> rename \\.\C:\..\NUL. deletefile.txt
C:\> del deletefile.txt

Using the \\.\ prefix tells the high-level file I/O functions to pass the filename unparsed to the device driver - this way you can access otherwise invalid names.

Read this article about valid file / path names in Windows and the various reserved names.

Upvotes: 208

asdf
asdf

Reputation: 49

Try writing a short C program that calls the Windows API to delete that file.

http://msdn.microsoft.com/en-us/library/aa363915%28v=vs.85%29.aspx

If that doesn't work, try opening a handle to the file with CreateFile() with FILE_FLAG_DELETE_ON_CLOSE, and then close the handle.

Upvotes: -2

Related Questions