'Is there a /dev/null on Windows?
What is the equivalent of /dev/null on Windows?
Solution 1:[1]
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.
Solution 2:[2]
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.
Solution 3:[3]
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:
Solution 4:[4]
NUL works programmatically as well. E.g. the following:
freopen("NUL", "w", stderr);
works as expected without creating a file. (MSVC++ 12.0)
Solution 5:[5]
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.
Solution 6:[6]
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.
Solution 7:[7]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Peter Mortensen |
| Solution 2 | Peter Mortensen |
| Solution 3 | Peter Mortensen |
| Solution 4 | Pang |
| Solution 5 | |
| Solution 6 | Peter Mortensen |
| Solution 7 |
