492

What is the equivalent of /dev/null on Windows?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ove S
  • 5,205
  • 2
  • 18
  • 7

10 Answers10

571

I think you want NUL, at least within a command prompt or batch files.

For example:

type c:\autoexec.bat > NUL

doesn't create a file.

(I believe the same is true if you try to create a file programmatically, but I haven't tried it.)

In PowerShell, you want $null:

echo 1 > $null
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    @Jim: Interesting - I didn't know you could write to paths off /dev/null as if it were a directory. Hmm. – Jon Skeet Mar 19 '10 at 18:33
  • On Ubuntu: echo blah > /dev/null.txt bash: /dev/null.txt: Permission denied Maybe this is something specific to your system... – catphive Apr 26 '10 at 23:56
  • 21
    @capthive: There's a difference between /dev/null.txt and /dev/null/foo.txt. – Jon Skeet Apr 27 '10 at 05:26
  • 2
    I just looked at this again, and I retract my original statement. I was doing the write in code, and the error was getting swallowed. I'm deleting it so no one accidentally takes it as the truth. – Jim Hunziker Jul 22 '10 at 19:30
  • [Wikipedia's /dev/null article](http://en.wikipedia.org/wiki//dev/null#Usage) also states that PowerShell denotes $null as being acceptable... Trying it just now with wget (part of GnuWin32), it accepted `$null` as a destination when specified with `--output-document=$null`. NUL is shorter though so I'll be using that! – Chris Woods Nov 28 '12 at 12:05
  • @Marwelln: It's definitely not on Windows 8.0. `echo foo > NULL` then `echo foo > NUL`, then `dir n*` will show `NULL` but not `NUL`. – Jon Skeet Oct 07 '13 at 21:46
  • 4
    For people looking for Unix "special" files under Windows: [here](http://www.ltr-data.se/opencode.html/#ZeroDrv) are `/dev/random` and `/dev/zero` device drivers for Win32. – ulidtko Dec 19 '14 at 11:06
  • Note that a program, that heavily prints to stdout, will take the same amount of time to finish if redirected to NUL - although there's nothing to print. – CodeManX Aug 23 '15 at 23:52
  • 11
    @CoDEmanX: That's not my experience. Writing to the console - or even a file - can take a significant chunk of time. I've just tested it with a program writing "Hello there" to stdout 10000000 times. Redirecting to a file (on an SSD) took 18 seconds. Redirecting to NUL took 4 seconds. Not redirecting at all made me give up through a lack of patience after a while... – Jon Skeet Aug 24 '15 at 05:49
  • In my test on Windows 7 with regular HDD, the example program took 0.67s for writing to file, 1.4s for NUL and multiple minutes for printing to console. I guess it varies and there are a couple influencing conditions. – CodeManX Aug 24 '15 at 07:34
  • You can also use `C:\Windows\System32\drivers\null.sys`. – Krii Mar 18 '16 at 00:09
  • 6
    I've learned back in Win95 times that you should write a colon after special device names, so `NUL:` (and `CON:`, `PRN:`, ...), It is mostly a matter of style and shows clearly that `NUL:` is a special object and not a file called `NUL`. Also,it is possible with a special API to make a file called `NUL` (see one of the answers). I dream that maybe one day, if everybody uses the colon by convention, we will be able to deprecate the dreaded special device names :-) – jdm Dec 06 '16 at 13:47
  • I did some quick tests with LinqPad and it looks like NUL is not supported by Windows either as a filename or as a path element. My guess is that NUL is a cmd.exe thing only (hence PowerShell needing $null instead). – David Anderson Jan 17 '17 at 19:11
  • `cat foobar 2> $null` redirects stderr to null. – ggorlen Nov 19 '20 at 16:26
65

NUL in Windows seems to be actually a virtual path in any folder. Just like .., . in any filesystem.

Use any folder followed with NUL will work.

Example,

echo 1 > nul
echo 1 > c:\nul
echo 1 > c:\users\nul
echo 1 > c:\windows\nul

have the same effect as /dev/null on Linux.

This was tested on Windows 7, 64 bit.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jerry
  • 938
  • 7
  • 13
  • 7
    Nice, it's not possible to create a file named "nul" on Windows 7 64bit :) – David Ferenczy Rogožan Sep 01 '15 at 14:22
  • 9
    @DawidFerenczy it's possible, although not in the classic way. E.g. `md \\.\c:\nul` https://www.quora.com/Why-is-it-impossible-to-name-a-folder-in-Windows-with-the-word-con, http://www.gohacking.com/how-to-create-con-folder-in-windows/, http://superuser.com/questions/86999/unable-to-rename-a-folder-or-a-file-as-con – phuclv Feb 10 '16 at 09:42
  • 3
    On Windows 10 in bash, the first one creates a file `nul` – jaques-sam Aug 28 '19 at 10:43
  • throws IOException using java in win10. Just need to wrap it on try catch. Was hoping to be no exception/errors just like /dev/null – MDuh Mar 17 '20 at 20:12
64

According to this message on the GCC mailing list, you can use the file "nul" instead of /dev/null:

#include <stdio.h>

int main ()
{
    FILE* outfile = fopen ("/dev/null", "w");
    if (outfile == NULL)
    {
        fputs ("could not open '/dev/null'", stderr);
    }
    outfile = fopen ("nul", "w");
    if (outfile == NULL)
    {
        fputs ("could not open 'nul'", stderr);
    }

    return 0;
}

(Credits to Danny for this code; copy-pasted from his message.)

You can also use this special "nul" file through redirection.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
strager
  • 88,763
  • 26
  • 134
  • 176
  • 9
    Ah, so this explains why, if you create a `C:\dev` directory in Windows, and you use a lot of GNU utilities, you'll eventually acquire a mysterious file called null in that directory. – Klitos Kyriacou Dec 29 '18 at 09:39
37

Jon Skeet is correct. Here is the Nul Device Driver page in the Windows Embedded documentation (I have no idea why it's not somewhere else...).

Here is another:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Foredecker
  • 7,395
  • 4
  • 29
  • 30
22

NUL works programmatically as well. E.g. the following:

freopen("NUL", "w", stderr);

works as expected without creating a file. (MSVC++ 12.0)

Pang
  • 9,564
  • 146
  • 81
  • 122
avadin
  • 351
  • 3
  • 7
19

If you need to perform in Microsoft Windows the equivalent of a symlink to /dev/null in Linux you would open and administrator's cmd and type:

For files:

mklink c:\path\to\file.ext NUL:

Or, for directories:

mklink /D c:\path\to\dir NUL:

This will keep the file/direcotry always at 0 byte, and still return success to every write attempt.

Marco
  • 582
  • 7
  • 17
2

In Windows10, if you want to use NUL like a file e.g.

robocopy .\test NUL /move /minage:30 
# delete all files older than 30 days using robocopy

These answers all don't work.

You get the error:

ERROR 123 (0x0000007B) Accessing Destination Directory \\.\NUL\
The filename, directory name, or volume label syntax is incorrect.

However, it works if you do in cmd.exe:

echo 1 > NUL

So NUL doesn't behave exactly like a /dev/null file.

However, for the robocopy command, you can do something like:

robocopy .\test NUL\null /move /minage:30 

Then it works!

In Powershell, the $null works only as stdout redirection

echo 1 > $null

But you can't use $null in a command like for robocopy instead of a file. Neither does $null\null work.

So all I could find to have the same effect like cmd.exe in PowerShell, is to call cmd.exe from within PowerShell like this:

mkdir test1
cd test1
echo "" > test1.txt
echo "" > test2.txt
echo "" > test3.txt

$path = '.\test1'
cmd.exe /c "robocopy $path NUL\null /move"

# also this works:
cmd.exe /c "robocopy $path .\NUL\null /move"

So NUL doesn't behave exactly like /dev/null folder but like a folder which can have phantom files inside it when used as a target file except you use it with > redirection, then it behaves as it is like a null device/file.

In addition it is to be mentioned that cmd.exe creates a NUL when first used. But one cannot look into it.

Gwang-Jin Kim
  • 9,303
  • 17
  • 30
  • 2
    Doing the above with `robocopy` will actually create a folder called `NUL`, which isn't really what is desired here, especially since the folder then can't be deleted using normal means (e.g. Windows Explorer). – tomasz86 Sep 05 '22 at 10:59
  • @tomasz86 Thank you for the remark! Perhaps there is no real equivalent to Linux's /dev/null, unfortunately. – Gwang-Jin Kim Sep 05 '22 at 12:36
  • If only I read the comments before trying the Robocopy part of this answer... now to figure out how to delete this folder and its contents! – Edward Millen Nov 27 '22 at 22:27
0

You have to use start and $NUL for this in Windows PowerShell:

Type in this command assuming mySum is the name of your application and 5 10 are command line arguments you are sending.

start .\mySum  5 10 > $NUL 2>&1

The start command will start a detached process, a similar effect to &. The /B option prevents start from opening a new terminal window if the program you are running is a console application. and NUL is Windows' equivalent of /dev/null. The 2>&1 at the end will redirect stderr to stdout, which will all go to NUL.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Haseeb Mir
  • 928
  • 1
  • 13
  • 22
0

The only built-in tool, which can deal with NUL is the good old copy. But make sure, you use the switch /b (binary), otherwise the content won't be cached by OS (that was my goal).

Put a directory (recursive) to OS cache:

for /f "delims=" %f in ('dir /s /b /a-d D:\Solr\data') do @copy /b "%f" nul > nul

Use the RamMap (from Sysinternals) to verify.

jetnet
  • 571
  • 5
  • 8
  • The question: "What is the equivalent of /dev/null on Windows?" This is not an answer to Q. Also: "The only built-in tool"? The `NUL` "file device" works on much more than just the MS `COPY` command. Maybe I misunderstood you? If I did, so did others.. – B. Shea Apr 09 '23 at 13:14
0

The device you are looking for is NUL.

HOWEVER, if you don't want to see any output and send 'everything', i.e. stdout and stderr, both to NUL (to nowhere) you need to redirect stderr to NUL too. This is done appending >NUL 2>&1 at the end of the line whose output you want to hide.

For example, if you want to kill a process called EvilApp anyway and are not interested in seen the output of kill opperation, you may write something like this in your batch file for Windows:

@taskkill /F /IM EvilApp.exe >NUL 2>&1

After that, EvelApp is killed if running. You do not see any output for this, either on console (cmd) nor as file somewhere. You will also not see an error message in the case that EvilApp was not alive at all.

I hope this help.

primehunter
  • 280
  • 4
  • 10