53

I have a command I am running produces a ton of output, I want to silence the output without writing to a file. I have used the following to send all output to a file:

command > out.txt 2>&1

... but again I don't want any file output:

command > /dev/null 2>&1

I have used command > /dev/null on my CentOS box before, but I can't find a similar thing for windows.

  • I hope you used command > /dev/null on your CentOS. – Kurt Pfeifle Aug 02 '10 at 20:43
  • @pipitas whatever i did it worked :D – Alec Gorge Aug 11 '10 at 18:19
  • If you indeed used command > /bin/null on CentOS, you have created a common file file named /bin/null on your system. You may say 'It worked!', if you want. This file now contains the stdout and stderr output of your command. Usually, in /bin/ there are only executable files. And usually, only the root user is allowed to create files there. So if that file is there, you did run your command as root user... /bin/null usually doesn't exist -- and /dev/null (which I mentioned) usually is used as the 'black whole' where unwanted output should disappear in... – Kurt Pfeifle Aug 11 '10 at 23:13
  • Good point, I hadn't thought of that. However, I am pretty sure it is just my faulty memory because I was on Windows at the time. – Alec Gorge Aug 18 '10 at 03:15

2 Answers2

77

You want command > nul 2>&1.

Jed Daniels
  • 7,382
  • 2
  • 36
  • 42
  • thanks! that works. I need to wait 12 minutes to accept the answer though (serverfault requirement)! – Alec Gorge Apr 16 '10 at 03:22
  • 8
    This only redirects stdout. You also need > nul 2>&1 or 2> nul to kill stderr. – u1686_grawity Apr 16 '10 at 14:02
  • Yes I know that, but thanks for mentioning it for others that visit this question! – Alec Gorge Apr 19 '10 at 20:52
  • just to add, I was getting permission denied error while using : sudo hostname -f 2> nul whereas following command worked well : sudo hostname -f 2> /dev/null For those who run out into permission issue while using nul – Ranvir Feb 10 '15 at 12:34
  • @JedDaniels what is meaning of '> nul' ?? – AminM Feb 01 '16 at 13:46
  • Doesn't seem to work with Visual Studio's cl.exe currently on Windows 10. – ZeroPhase Apr 28 '17 at 12:43
  • @Ranvir because this question is specific to Windows. You're obviously using a *nix OS, which is drastically different. – Doktor J May 31 '19 at 17:09
  • 2
    @AminM nul is Windows' special keyword for a null output (i.e. discard output). On *nix-based systems (like Ranvir is referring to) they instead use the special file /dev/null. The > means "redirect" (and 2> means redirect buffer 2 -- which is the error output) – Doktor J May 31 '19 at 17:11
  • @DoktorJ tnx for answer – AminM Jun 01 '19 at 07:43
4

You want command > $null 2>&1

nul only works in command prompt whereas $null works in powershell.

ross
  • 3
ZZ9
  • 918
  • 4
    Command Prompt is cmd.exe. '> null' creates a file called 'null' with whatever you're directing into it from cmd.exe and from PowerShell. – Mr. Smythe Jul 29 '15 at 16:43