82

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?

EM0
  • 5,369
  • 7
  • 51
  • 85
  • Have you tried `del *.*`? – chqrlie Jul 10 '21 at 18:32
  • 1
    Better yet, how to prevent its creation? – Dragos Bandur Dec 10 '21 at 21:10
  • 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**](https://stackoverflow.com/questions/313111/is-there-a-dev-null-on-windows) be used to denote the bitbucket in such cases. Cygwin programs of course do understand /dev/null. – user1934428 Oct 25 '22 at 06:19

8 Answers8

191

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.

xxbbcc
  • 16,930
  • 5
  • 50
  • 83
  • 2
    Interestingly, Far Manager can delete that file without an issue. Perhaps it uses that trick internally unconditionally. – Joey Jul 26 '13 at 14:31
  • 20
    Great, thanks! Even deleting it directly seems to work if I use the \\.\ prefix before the full path, eg. del \\.\C:\Temp\NUL – EM0 Jul 26 '13 at 14:37
  • @Јοеу 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. – xxbbcc Jul 26 '13 at 14:37
  • 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 ;-) – Joey Jul 26 '13 at 16:03
  • @Јοеу 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. – xxbbcc Jul 26 '13 at 16:07
  • 2
    It worked for me. Important thing to remember is to start the filepath like: \\.\C:\ – revolutionary Nov 20 '13 at 19:22
  • 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) – revolutionary Nov 20 '13 at 19:23
  • Worked like charm! Thanks :) – Fawkes Aug 21 '17 at 12:31
  • 2
    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". – Eryk Sun Oct 28 '19 at 21:10
  • Would be great if there was a recursive version where a folder and all its subfolders would be purged of these pesky NULs – phaebz Mar 30 '20 at 07:57
  • 1
    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" – PGlivi Jun 05 '20 at 13:56
  • 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. – Dragos Bandur Dec 09 '21 at 20:36
  • @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`). – Paul May 15 '22 at 05:58
  • @Paul In my case one culprit seems to have been the `tinytex` R package. The issue was solved per [this](https://github.com/yihui/tinytex/issues/371) post. Still not sure about `R Studio` – Dragos Bandur May 16 '22 at 15:06
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

Geovani Martinez
  • 2,053
  • 2
  • 27
  • 32
  • 3
    This solves a different problem. OP is trying to delete a file called "NUL" which is a keyword in Windows. – Zack Oct 07 '18 at 13:11
  • 3
    My example also applies to files named NUL (I confirmed) – Geovani Martinez Oct 08 '18 at 16:58
  • Thanks this exactly solved my problem brilliantly!! As Zack says this is solves a different problem, but it seems google directly me here. – Brian Feb 23 '19 at 10:23
  • 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. – Lupin Apr 03 '19 at 11:48
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.

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.

Ben Jacobs
  • 2,526
  • 4
  • 24
  • 34
  • 9
    If you've got Cygwin, just try 'rm NUL'. It worked for me. – Jan Ehrhardt May 11 '17 at 00:31
  • 2
    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. – Bill Hoag Nov 16 '17 at 19:50
  • 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. – BGranato Apr 18 '22 at 16:09
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

Olivier
  • 41
  • 4
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.

spryce
  • 616
  • 7
  • 14
  • 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. – EM0 Jan 24 '17 at 11:25
1

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.

James Risner
  • 5,451
  • 11
  • 25
  • 47
-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.

asdf
  • 49
  • 2
  • 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 https://superuser.com/questions/282194/how-do-i-remove-a-file-named-nul-on-windows ...) – SamB Jun 18 '17 at 15:49