What does “> /dev/null 2>&1″ mean?

I remember being confused for a very long time about the trailing garbage in commands I saw in Unix systems, especially while watching compilers do their work. Nobody I asked could tell me what the funny greater-thans, ampersands and numbers after the commands meant, and search engines never turned up anything but examples of it being used without explanation. In this article I’ll explain those weird commands.

Here’s an example command:

wibble > /dev/null 2>&1

Output redirection

The greater-thans (>) in commands like these redirect the program’s output somewhere. In this case, something is being redirected into /dev/null, and something is being redirected into &1.

Standard in, out, and error

There are three standard sources of input and output for a program. Standard input usually comes from the keyboard if it’s an interactive program, or from another program if it’s processing the other program’s output. The program usually prints to standard output, and sometimes prints to standard error. These three file descriptors (you can think of them as “data pipes”) are often called STDIN, STDOUT, and STDERR.

Sometimes they’re not named, they’re numbered! The built-in numberings for them are 0, 1, and 2, in that order. By default, if you don’t name or number one explicitly, you’re talking about STDOUT.

Given that context, you can see the command above is redirecting standard output into /dev/null, which is a place you can dump anything you don’t want (often called the bit-bucket), then redirecting standard error into standard output (you have to put an & in front of the destination when you do this).

The short explanation, therefore, is “all output from this command should be shoved into a black hole.” That’s one good way to make a program be really quiet!Technorati Tags:No Tags

You might also like:

  1. How to convert MySQL output to HTML tables
  2. How to create a VB6 console program
  3. Three updated tools in MySQL Toolkit
  4. Introducing MySQL Table Checksum

57 Responses to “What does “> /dev/null 2>&1″ mean?”


  1. 1 Tim McCormack

    The short explanation, therefore, is “all output from this command should be shoved into a black hole.”

    All the output, that is, except for error messages, right? So, one would use this structure to only listen to standard error?

  2. 2 Xaprb

    Error messages go to STDERR, which I’ve redirected to STDOUT, which is being shoved into a black hole. The program should not make a peep.

    To listen only to STDERR, I can just redirect STDOUT into /dev/null, like so: wibble > /dev/null

  3. 3 Paul

    My source for finding this information, since I usually forget how it works, is man bash and search for the REDIRECTION section.

    Some of the man page is bash specific, but most is pretty generic.

  4. 4 chrisortiz

    I’ve seen “2>&1 &” at the end of many tutorials, but I never knew what they were for. Thanks for the info guys.

  5. 5 Tim McCormack

    Ah, I understand now — I misunderstood the nature of redirection, treating it as commands instead of pipes. That clears up a lot.

  6. 6 David

    So logically that means you could catch STDERR from the command line by appending “2>>/var/log/logfile” to a shell command.

    So, where “2” is STDERR, “>>” is to append and “/var/log/logfile” is the where errors will be logged.

  7. 7 Tim

    Nice, Short, Sweet. So in short:

    “> /dev/null” will redirect STDOUT to a BLACK HOLE

    “2 > &1” will redirect STDERR to STDOUT which in turn is redirected to a BLACK HOLE

    Right? Thank you for the explanation!

  8. 8 Xaprb

    Yep, that’s right!

  9. 9 Eric Dodemont

    Does it mean that

    >/dev/null 2>&1

    is equivalent to

    >/dev/null 2>/dev/null?

  10. 10 Xaprb

    Yes, that’s true.

  11. 11 Bobby Howie

    Excellent! Well explained.

  12. 12 Hari Ganesh

    Excellent Tutorial. Hats off Mr.Xaprb

  13. 13 Raghu Balasubramanian

    Hi,

    This explanation helps clear a lot. Thanks and keep up the good work.

  14. 14 Nadeem

    Good work friend :)

  15. 15 Alex

    Really excellent explanation, thanks!

  16. 16 Hari Ganesh

    So >/dev/null 2 > /var/log/logfile will redirect STDOUT to bit-bucket and STDERR to the log file. Am i right?

    And if >/dev/null is given,will both STDOUT & STDERR be redirected to bit-bucket?

  17. 17 Xaprb

    Hari, your first sentence is right, but in the second, only STDOUT goes to the bit-bucket.

  18. 18 Aman Jain

    If &1,&2,&3 are used to refer to STDIN,STDOUT and STDERR respectively
    Then why not “wibble > /dev/null &2>&1 “
    instead of “wibble > /dev/null 2>&1″

  19. 19 pulica

    just try: ” ;tryME >> /dev/null 2>&1 ” it outputs bash: syntax error near unexpected token `;’ :))

  20. 20 pulica

    to answer you Aman Jain I think that:
    1) “wibble > /dev/null 2>1″ stdout is redirected to /dev/null and stderr is redirected to a file named “1″ (in the current directory)
    2) “wibble > /dev/null 2>&1″ stdout is redirected to /dev/null and stderr is redirected to where stdout is redirected (bit bucket)
    3) “wibble > /dev/null &2>1″ stdout is redirected to /dev/null , then character “&” comes, which means that this process will run in background(parallel with the shell u are using). Then comes 2>1 which will create an empt file with the name 1.
    And FINALLY!!!
    4) “wibble > /dev/null &2>&1 ” stdout is redirected to /dev/null. “&” means that, that process(wibble) will run in background(with stdout redirected only).Then another process is started with 2>&1 which creates no file.
    U can try it. Maybe im wrong somewhere, Hey im not Steve Bourne(author of sh) ;)

  21. 21 tnt

    a good job

  22. 22 Arun

    Thanks a lot….Your information really helped me a lot.

  23. 23 ajay

    Thanks to xaprb for the nice explanation..

  24. 24 Raj

    Hi,

    This is an awsome sweet tutorial. I have the following:
    find . -mtime 3 -regex ‘.*\.md5$’ | xargs sudo rm -f {} \; > /dev/null 2>&1

    I am trying to find files that are modified past 3 days or files more than 3 days old, remove them and redirect them to >dev/null…

    When I redirect > TO /dev/null 2>&1 the entire command didn’t work, and when I removed things started working.

    My question is

    1. Do we need to specify the /dev/null 2>&1?
    2. Is this for what went wrong - post analysis kind of thing?

    Thank you

    Raj

  25. 25 Cameron

    Great little article - thankyou.

  26. 26 iris

    good explanation..was wonderin all these days as to y we ve de > /dev/null 2>&1 in all our cronjobs…… thx… :)

  27. 27 bilel

    echo “good work man, thx a lot” >/dev/null 2>&1;
    lol

  28. 28 Mel Briggs

    In the following statement there is a “”. Does that combination signify someting unique or is it just a input and output redirection symbol sitting side by side?

    smf: :sysinit:/lib/svc/bin/svc.startd >/dev/msglog 2/dev/msglog “), then exactly what is this statement saying?

  29. 29 Mel Briggs

    I just submitted a question and noticed that the main item I needed clarification on was stripped out. I wanted to know how the information flows within a signle command when there are multiple redirection symbols, and in this case, a input redirection symbol placed right next to an output redirection symbol. It looks like: “smf: :sysinit:/lib/svc/bin/svc.startd >/dev/msglog 2/dev/msglog /dev”. That puts the less-than and greater-than signs adjacent. (From Solaris 10 etc/initab.)

  30. 30 Oscar Rombo

    Av been using >/dev/null 2>&1 for as long as i can remmeber with no clue but now it’s clear.Thanks for the tutorial

  31. 31 Ram

    Its a very useful info, if somebody doubted so, no easy to understand even if it looks simple, thanks god, now it apprears first in the googe search result.\

    Thank you once again,.

  32. 32 jt

    If anyone is wondering how to refer to the greater-than sign (or angle bracket) when used in this context - some people call it a “hoinky” Probably my all-time favorite tech jargon term

  33. 33 remo

    grt job

  34. 34 Luke

    Would anyone be able to explain why it wouldn’t work if you wrote it like this?

    command 2>&1 >> file

  35. 35 Mike

    This was a very helpful tutorial. Thanks again!

  36. 36 Mark

    Why do you need the & symbol in from of 1?

  37. 37 jt

    If I recall correctly, “>&” redirects handles vs. “>” redirecting output to a file. If you leave out the & it would create a file named “1″

  38. 38 MattS

    Really well done. Thanks for being so clear and concise!

  39. 39 Janbur

    Great job, thanks a lot!

  40. 40 Neeraj

    Please explain difference between 2>&1 and 2&>1

  41. 41 VJ

    2>&1: it means 2(stderr) is redirected to &1. & tells the address (fd) of 1(stdout). so the stderr is redirected to stdout.
    2&>1: it means 2 that is nothing will get executed in bg because of &. And the output will be redirected to a file named 1 not the stdout.

  42. 42 Steven

    Excellent tutorial, I’m glad people like you exist :)

  43. 43 Martin

    Good job! I never understood this!

  44. 44 sas

    i feel agitated when i have to use something without knowing what its meant for. your info was a quick insight. thanks a million

  45. 45 Xrules

    Great stuff,
    I have a question. Specailly on bash, if I connect to a computer with ssh and run a process in background with ./job & and try to quit the shell hangs or actually waits for the process to finish, which is not what I want. I want to quit and let the process run.

    Now I have a solution to this problem, i.e, if I put ./job 2

  46. 46 Xrules

    I was trucncated..

    Now I have a solution to this problem, i.e, if I put “./job 2

  47. 47 Xrules

    truncated again because of less than symbol
    Now I have a solution to this problem, i.e, if I put ./job 2 [lessthan symbol instead of greaterthan]/dev/null (note the direction is inward and not outward) the shell happily exits and leaves the process in background.

    Any ideas why this happens would be educating.

    Thanks in advance and the article and the discussion are helpful

  48. 48 Xaprb

    Xrules, look into the nohup command. Even better, though: use screen. It is one of the top 5 things I cannot live without.

  49. 49 Arun

    Thanks

  50. 50 adam

    I used redirectors all the time, but the 2>&1 always confused me, very well explained!

  51. 51 Ryan

    Here’s a shortcut for directing stdout and stderr to /dev/null:

    wibble >& /dev/null

    Now I have a question: What if I want to redirect stdout and stderr to a file called “2″? “wibble >& 2″ doesn’t do it, of course.

  52. 52 Dinesh

    >/dev/null 2>&1

    is equivalent to

    >/dev/null 2>/dev/null?

    Then can we say like, “&” is used to trace the direction where 1 (STDOUT) directed?

  53. 53 Dinesh

    Also, Is it possible to redirect only the STDERR error message to any log file (without redirecting program output to STDOUT)

  54. 54 jt

    Ryan:
    wibble >./2 2>&1

    Dinesh - see VJ’s comment above for your first question.
    your second question is also answered in the original post and comments:
    ls 2>stderr.log

  55. 55 Dinesh

    Thanks Jt…

  56. 56 bheemboy

    thanks it helped me a lot

  1. 1 My del.icio.us bookmarks for September 27th-- Tales of a Minnesota Geek

Leave a Reply

Please do not use this blog to get help with problems or bugs in Maatkit or innotop: use the Sourceforge forums, mailing list, or bug trackers. If you're asking for help with MySQL, please use the MySQL mailing list instead.