Ove S
Ove S

Reputation: 5405

Is there a /dev/null on Windows?

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

Upvotes: 512

Views: 279073

Answers (11)

Marco
Marco

Reputation: 622

If you need to perform in Microsoft Windows the equivalent of a symlink to /dev/null in Linux you would open an 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.

Upvotes: 22

John32ma
John32ma

Reputation: 51

For apps such as MSVC, I do not believe there is a way to send the object to the null device. I would have thought using /Fo\\.\NUL and not /FoNUL to specify the null device would work, but it does not. According to https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file, this should be the right way to send output to the null device, however,

cl /c file.cpp /Fo\\.\NUL            # does not work

thus, the best one can do is use /Fo\path\to\tmp.obj and then delete the object file.

Upvotes: 0

HeavenHM
HeavenHM

Reputation: 992

Null-Redirecting streams in PowerShell:

These example assumes mySum is the name of your application and 5 10 are the arguments you're sending.

& .\mySum 5 10 >$null 2>&1

& is PowerShell's Call operator. Starting a process that isn't on your PATH environment variable requires you provide a file path of some sort, which is why mySum is prepended with .\ to make it a relative path.

Redirecting to $null is the PowerShell analog to using /dev/null on Unix-based platforms. 2>&1 at the end will redirect StdErr to StdOut, which will all go to $null.

You could also use the Start-Process function:

Start-Process .\mySum -ArgumentList (5,10) -NoNewWindow -Wait >$null 2>&1

This will start a detached process. -ArgumentList accepts an argument or a collection of arguments (note the comma). The parentheses aren't necessary here, but it makes it more obvious that a list is being provided. -NoNewWindow prevents Start-Process from opening a new terminal window if the program you are running outputs to the console. -Wait will wait for the process to close before continuing.

This can be shortened using built-in function & parameter aliases:

saps .\mySum 5,10 -nnw -Wait >$null 2>&1

saps & start are the aliases for Start-Process, but saps is used here because it's slightly shorter. -ArgumentList is implied as a positional parameter, but if you'd rather not do that, you could instead use the parameter alias -Args. I also excluded the clarity-providing parentheses since they're not required. Lastly, -nnw is the alias for -NoNewWindow.

Upvotes: 0

primehunter
primehunter

Reputation: 280

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.

Upvotes: 0

jetnet
jetnet

Reputation: 661

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.

Upvotes: 0

Gwang-Jin Kim
Gwang-Jin Kim

Reputation: 9865

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.

Upvotes: 2

avadin
avadin

Reputation: 401

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

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

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

Upvotes: 27

Jerry
Jerry

Reputation: 1026

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.

Upvotes: 72

Foredecker
Foredecker

Reputation: 7493

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:

Upvotes: 37

strager
strager

Reputation: 90012

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.

Upvotes: 64

Jon Skeet
Jon Skeet

Reputation: 1500495

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

Upvotes: 592

Related Questions