Delete a file named "NUL" on Windows
Asked Answered
S

9

90

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:

  • Windows Explorer - error: "Invalid MS-DOS function" (yes, that is seriously what it says!)
  • Command prompt using "del NUL" - error: "The filename, directory name, or volume label syntax is incorrect."
  • Deleting the entire directory - same deal as just deleting the file
  • remove() in a C program - also fails

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?

Sturmabteilung answered 26/7, 2013 at 14:5 Comment(3)
Have you tried del *.*?Gaby
Better yet, how to prevent its creation?Fungiform
AFIK, NUL has a special meaning in Windows. If it was created in a Cygwin environment, it should be removeable there as well, i.e. by doing a rm NUL. BTW: In a Windows shell (Powershell, or in a Batchfile), /dev/null can not be used to denote the bitbucket in such cases. Cygwin programs of course do understand /dev/null.Matrimony
A
200

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.

Argon answered 26/7, 2013 at 14:19 Comment(14)
Interestingly, Far Manager can delete that file without an issue. Perhaps it uses that trick internally unconditionally.Olva
Great, thanks! Even deleting it directly seems to work if I use the \\.\ prefix before the full path, eg. del \\.\C:\Temp\NULSturmabteilung
@Јοеу On my Windows 8 Far had issues with deleting / creating a file named `nul' but I'm using v1.75 - v3.0 may have that feature added.Argon
Creating and deleting the file definitely works in my Far 3.0.2950. Being someone who likes and uses Unicode a lot 1.75 isn't for me anymore ;-)Olva
@Јοеу Yeah, I know. I have some plugins that I like a lot which don't exists for 3.0. I, too, have 3.0 on my machine but my shortcuts are for 1.75. One of these days I'll go through the trouble of switching over.Argon
It worked for me. Important thing to remember is to start the filepath like: \\.\C:\Principalities
My file was in D: directory so i started my path to the Nul file using \\.\D:\...\NUL. (also with a '.' at the end, otherwsie it wont work)Principalities
It's better in general to use "\\?\" instead of "\\.\" because the question-mark prefix bypasses all normalization, including stripping trailing spaces and dots. It will pass exactly whatever name you pass in quotes, such as "\\?\C:\nul. ", whereas "\\.\C:\nul. " gets normalized as "\\.\C:\nul".Garaway
Would be great if there was a recursive version where a folder and all its subfolders would be purged of these pesky NULsManeuver
Just a comment: If the path to the NUL file contains a space, then it is necessary to put it in quotes INCLUDING the prefix, i.e.: del "\\.\C:\path\that\contains\a space character\NUL"Kieserite
I wonder what would be the reason for this file to appear in the first place. I do not use cygwin or git desktop. However, it pops up any time I render an Rmd document with R Studio. I am looking for a preventive solution.Fungiform
@DragosBandur: it's gotta be a problem in R Studio - they're probably running some script behind the scenes that wants to hide output (by sending to nul), but if the script is actually running on something like bash for Windows, then nul is interpreted as a file name (they should be using /dev/null).Mccandless
@Mccandless In my case one culprit seems to have been the tinytex R package. The issue was solved per this post. Still not sure about R StudioFungiform
There's a comment to prefer \\?\ over \\.\ . When I was trying to rename a folder named NUL, I found that the '?' version returned an error (The syntax of the command is incorrect.), whereas the '.' version worked correctly.Repartition
L
39

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

Lynea answered 15/8, 2018 at 15:29 Comment(4)
This solves a different problem. OP is trying to delete a file called "NUL" which is a keyword in Windows.Mosher
My example also applies to files named NUL (I confirmed)Lynea
Thanks this exactly solved my problem brilliantly!! As Zack says this is solves a different problem, but it seems google directly me here.Anaesthesiology
This solution worked for me, it seems that the windows CMD can not delete the file from one explanation i found win32 tools can not access this file - so the git bash and using rm is a good solution.Firewarden
B
8

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.

Breechloader answered 10/7, 2021 at 18:22 Comment(0)
C
4

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.

Carniola answered 16/5, 2014 at 23:27 Comment(3)
If you've got Cygwin, just try 'rm NUL'. It worked for me.Lanell
I had a similar problem as noted by Jan E., except I'm using Windows Subsystem on Linux (WSL). WSL bash's rm deleted the file.Pericynthion
I use MobaXterm to run bash commands on Windows 10 and just doing rm nul worked like a charm. I think this would also work with other SSH/FTP clients such as PuTTY and WinSCP.Maurizia
T
3

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.

Trishatriskelion answered 24/10, 2022 at 13:50 Comment(0)
B
2

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

Bergson answered 2/4, 2018 at 11:15 Comment(0)
K
1

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.

Koweit answered 24/1, 2017 at 5:4 Comment(1)
I think yours is a slightly different case where the long name of the directory is "NUL", but the MSDOS 8.3 name is not. So this is useful to know, but not the same as my original problem, which I just tested again using Cygwin. After running mkdir nul in cygwin dir /x/a shows no short name, so the folder is really named "nul". rd \\?\C:\Path\NUL and rd \\.\C:\Path\NUL both work in this case.Sturmabteilung
F
0

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>"
Felicio answered 28/11, 2023 at 12:46 Comment(0)
P
-2

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.

Prokofiev answered 26/7, 2013 at 14:14 Comment(1)
Pretty sure this would be interpreted by the Win32 layer as referring to the so-called "DOS" device, not the file. (Presumably, the "DOS" device wouldn't actually end up deleted, because if that was a thing that could be done by accident than I'm pretty sure it would have happened to someone participating here or at superuser.com/questions/282194/… ...)Keri

© 2022 - 2024 — McMap. All rights reserved.