Xaprb

Stay curious!

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

with 119 comments

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!

Written by Xaprb

June 6th, 2006 at 9:26 pm

Posted in GNU/Linux

119 Responses to 'What does “> /dev/null 2>&1″ mean?'

Subscribe to comments with RSS or TrackBack to 'What does “> /dev/null 2>&1″ mean?'.

  1. 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?

    Tim McCormack

    6 Jun 06 at 11:29 pm

  2. 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

    Xaprb

    7 Jun 06 at 8:26 am

  3. 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.

    Paul

    10 Jul 06 at 2:44 pm

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

    chrisortiz

    20 Jul 06 at 5:03 pm

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

    Tim McCormack

    23 Jul 06 at 3:30 pm

  6. 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.

    David

    22 Nov 06 at 7:14 pm

  7. 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!

    Tim

    29 Nov 06 at 11:52 am

  8. Yep, that’s right!

    Xaprb

    29 Nov 06 at 12:11 pm

  9. Does it mean that

    >/dev/null 2>&1

    is equivalent to

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

    Eric Dodemont

    9 Jan 07 at 12:23 pm

  10. Yes, that’s true.

    Xaprb

    9 Jan 07 at 1:04 pm

  11. Excellent! Well explained.

    Bobby Howie

    12 Jan 07 at 10:58 am

  12. Excellent Tutorial. Hats off Mr.Xaprb

    Hari Ganesh

    16 Jan 07 at 5:22 am

  13. Hi,

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

    Raghu Balasubramanian

    30 Jan 07 at 1:28 am

  14. Good work friend :)

    Nadeem

    9 Feb 07 at 6:48 pm

  15. Really excellent explanation, thanks!

    Alex

    21 Feb 07 at 7:40 am

  16. 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?

    Hari Ganesh

    8 Mar 07 at 12:56 am

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

    Xaprb

    8 Mar 07 at 8:51 am

  18. 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″

    Aman Jain

    25 Mar 07 at 11:33 pm

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

    pulica

    27 Mar 07 at 3:04 pm

  20. 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) ;)

    pulica

    27 Mar 07 at 3:28 pm

  21. a good job

    tnt

    24 Apr 07 at 8:51 pm

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

    Arun

    10 May 07 at 2:10 am

  23. Thanks to xaprb for the nice explanation..

    ajay

    31 May 07 at 5:00 am

  24. 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

    Raj

    7 Jun 07 at 7:01 pm

  25. Great little article – thankyou.

    Cameron

    10 Jul 07 at 6:48 am

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

    iris

    8 Aug 07 at 7:06 am

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

    bilel

    25 Aug 07 at 4:37 am

  28. 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?

    Mel Briggs

    2 Sep 07 at 9:27 am

  29. 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.)

    Mel Briggs

    2 Sep 07 at 9:34 am

  30. 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

    Oscar Rombo

    3 Sep 07 at 2:29 am

  31. 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,.

    Ram

    18 Sep 07 at 2:51 pm

  32. 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

    jt

    19 Sep 07 at 4:04 pm

  33. [...] What does … mean? at Xaprb – [...]

  34. grt job

    remo

    8 Oct 07 at 1:41 pm

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

    command 2>&1 >> file

    Luke

    8 Nov 07 at 12:57 am

  36. This was a very helpful tutorial. Thanks again!

    Mike

    28 Nov 07 at 11:58 am

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

    Mark

    8 Dec 07 at 5:31 pm

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

    jt

    8 Dec 07 at 6:10 pm

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

    MattS

    3 Jan 08 at 6:43 pm

  40. Great job, thanks a lot!

    Janbur

    9 Jan 08 at 10:06 am

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

    Neeraj

    16 Jan 08 at 12:49 am

  42. 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.

    VJ

    15 Feb 08 at 1:56 pm

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

    Steven

    21 Feb 08 at 12:47 pm

  44. Good job! I never understood this!

    Martin

    25 Feb 08 at 2:33 pm

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

    sas

    3 Mar 08 at 5:02 pm

  46. 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

    Xrules

    24 Mar 08 at 3:58 pm

  47. I was trucncated..

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

    Xrules

    24 Mar 08 at 3:58 pm

  48. 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

    Xrules

    24 Mar 08 at 3:59 pm

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

    Xaprb

    24 Mar 08 at 6:19 pm

  50. Thanks

    Arun

    2 Apr 08 at 2:42 am

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

    adam

    5 Apr 08 at 11:54 am

  52. 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.

    Ryan

    7 Apr 08 at 1:33 am

  53. >/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?

    Dinesh

    7 Apr 08 at 4:26 am

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

    Dinesh

    7 Apr 08 at 4:36 am

  55. 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

    jt

    7 Apr 08 at 12:10 pm

  56. Thanks Jt…

    Dinesh

    8 Apr 08 at 12:27 am

  57. thanks it helped me a lot

    bheemboy

    16 Jun 08 at 12:03 pm

  58. And if you just want to hide STDERR output (for instance if you’re creating a bunch of system vars), you can say 2>/dev/null.

    export VAR=`command that causes lots of error output 2>/dev/null`

    Michael

    23 Jul 08 at 8:49 pm

  59. To Eric Dodemont and Dinesh:

    It is not exactly correct to say that the command

    Command 1: wibble >/mylogs/logfile 2>&1

    is equivalent to the command

    Command 2: wibble >/mylogs/logfile 2>/mylogs/logfile

    99.99% of the times you treat them identically, the result will be interchangeable, but if the wibble program generates both normal output on STDOUT and error messages on STDERR, you might notice a difference.

    This gets really nit-picky, but here’s the explanation. Command 1 sets up a situation in which the process running wibble has a single I/O channel to /mylogs/logfile which is shared cooperatively by the STDOUT and STDERR pipes. However, command 2 sets up a different situation in which there are TWO independent I/O channels to /mylogs/logfile, one used by the STDOUT pipe, and the other by the STDERR pipe.

    In the single channel situation, there’s a single location that keeps track of how the file is being written – specifically the position in the file for the next write. In the dual channel case, there are two separate and uncoordinated locations tracking the position of the next write.

    The end result is that in the single channel case, the logfile receives output and error messages in the sequence in which they are generated by the wibble program, but in the dual channel case, error messages tend to overwrite normal output and vice-versa.

    So, if the wibble program would normally run like this:

    wibble
    This is a line of output.
    error 1

    This command:

    wibble >/mylogs/logfile 2>&1

    will generate a logfile that looks like this:

    This is a line of output.
    error 1

    Whereas this command:

    wibble >/mylogs/logfile 2>/mylogs/logfile

    generates a logfile that looks like this:

    error 1 a line of output.

    wjs32246

    24 Jul 08 at 8:05 pm

  60. To Luke:

    the reason that this command:

    Command 1: wibble >/mylogs/logfile 2>&1

    works differently than this one:

    Command 2: wibble 2>&1 >/mylogs/logfiles

    has to do with the way in which the shell processes the command line. The shell deals with the I/O redirection tokens not as a totality, but rather in the sequence in which they appear; thus, in command 1, STDOUT is directed to logfile, then STDERR is directed to the same place as STDOUT, whereas in command 2, STDERR is directed to the terminal (because that’s where STDOUT is directed at this time), *then* STDOUT is redirected to logfile.

    wjs32246

    24 Jul 08 at 8:18 pm

  61. Thanks. I didn’t think I was going to get an answer for that :)

    Luke

    25 Jul 08 at 3:09 am

  62. “> /dev/null 2>&1″ mean?

    action 1: STDOUT is redirected to /dev/null
    action 2: STDERR is redirected to STDOUT

    question 1: is the above action 1 & action 2 is happening serial or parallel?

    question 2: if the above actions happens in serial in which order do they happen? (action 1 -> action2) /or/ (action 2 -> action 1)

    if the above questions are invalid then it means still I didn’t understand it. TIA.

    smg

    22 Aug 08 at 1:57 pm

  63. simple and explain. I cannot leave this page without typing this comment. Thankyou.

    vadivu

    7 Sep 08 at 10:42 pm

  64. I’ve been trying to keep a cron job from writing to the mail spooler for output and errors. Using the >/dev/null 2>&1 would work, but it suppresses all the file output, and these jobs write to a file for status reporting. Is there anything I can do to have the normal onscreen output written to dev/null but keep the other file outputs?

    Bill

    12 Sep 08 at 8:42 am

  65. Thanks a lot…
    its really clear my doubt…

    Jyoti Mishra

    24 Sep 08 at 7:36 am

  66. Gr888 work…

    Abdussami

    7 Oct 08 at 2:26 am

  67. Wow! simple things like this are sometimes overlooked but you’ve done well enough to explain this in detail.
    Thanks!

    Ang3L

    8 Oct 08 at 4:16 am

  68. Good and simple explanation..

    Kishore KVR

    24 Oct 08 at 4:55 am

  69. Thanks a lot!

    It really helps to be able to find such a comprehensive yet easily understandable tutorial.

    Exlellent work!

    vitaliy

    25 Oct 08 at 7:03 am

  70. 1) 1> /dev/null 2>&1
    2) 2> /dev/null 1>&2

    Why 1) is different from 2)

    Logu

    26 Oct 08 at 1:44 pm

  71. [...] http://www.xaprb.com/blog/2006/06/06/what-does-devnull-21-mean/ « Reset xorg.conf in Fedora [...]

  72. Thanks a lot! Googling “2>&1″ brought me here and definitely answered my question :)

    Daniel

    12 Nov 08 at 3:40 am

  73. Xaprb and others,

    I was also confused about this for a long time – Your explanations made it very clear to me now.

    Thanks so much!

    Also, I have a question about the less then redirect symbol as in the following example:

    “wobble /dev/null 2>/dev/null”

    I have seen this in some *.ksh scripts and it works fine – but I don’t understand what the less-then redirect is doing.

    choosakul

    13 Nov 08 at 3:41 pm

  74. some how the less-then symbol did not show up – it should look like this:

    Also, I have a question about the less then redirect symbol as in the following example:

    wobble [less-then-symbol]/dev/null 2>/dev/null

    replace [less-then-symbol] with the less then symbol.

    Thanks again! d:o)

    choosakul

    13 Nov 08 at 3:44 pm

  75. thks! i’m an idiot to unix/linux, but getting this is 1 notch up, and millions more to go.

    Ronald

    14 Nov 08 at 4:44 am

  76. /dev/null 2> error.txt &
    that is only report STDERR message to error.txt??

    if I need both STDERR and STDOUT message to error.txt
    how should I modify it?
    /dev/null 2>&1 error.txt & <<<<is correct??

    yichu

    16 Nov 08 at 11:47 pm

  77. Thanks deude for the explanation!

    Azhar

    18 Nov 08 at 7:32 pm

  78. I’m having a command to find
    /test/bdump/*.trc -mtime +7 -exec rm -f {} \;

    when I’m running this command manually without scheduling in cronjobs its working fine, but when i schedule it…it is not getting executed.
    to make it work do i require to specify
    /test/bdump/*.trc -mtime +7 -exec rm -f {} \; 2>&1
    if so how abt /dev/null should that be specified too.

    please suggest.

    Rajiv

    18 Nov 08 at 8:35 pm

  79. yichu:

    foo > error.txt 2>&1

    snoz

    18 Nov 08 at 9:27 pm

  80. Rajiv, it could be that the file glob isn’t working from within the cron job and it’s looking for a file with an actual asterisk in the name. try it with a single filename instead of the glob.
    what is your platform/shell? there might be a way to enable/disable file globbing?

    snoz

    18 Nov 08 at 9:40 pm

  81. we have IBM AIX – 5.3 ML07.

    Rajiv

    18 Nov 08 at 11:15 pm

  82. snoz
    thanks your suggest.
    I’ll try it later..

    yichu

    19 Nov 08 at 3:02 am

  83. rajiv, I’m also on AIX 5.3 and I had to double the backslash before the semicolon:
    find /test/bdump/*.trc -mtime +7 -exec rm -f {} \\;

    I think that will work
    as for the output, I think it all goes to the cron log file unless it was started with -Q. So you don’t have to mess with /dev/null
    if you want stdin/out to append to a logfile, just put the redirect in your crontab file after the command
    …. \\; >> output.log 2>&1

    snoz

    19 Nov 08 at 6:27 pm

  84. as i understand .. a program has 2 outputs, both redirected to the console by default:

    stdout = 1 (default)
    stderr = 2

    you can specify other redirection with symbols:

    > = redirect to a file
    >& = redirect to a pipe (is right?)
    & = run the preceded command in background?

    | = redirect to another program
    >> = append to a file
    file = redirects stdout to the file “file”
    command >file = same
    command >2 = redirects stdout to the file “2″
    command >&2 = redirects stdout to pipe 2 (stderr)

    command >file 2>&1 = redirects stdout to a file, and stderr to stdout ? (but stdout previously redirected to a file?, then both outputs redirected to the file?)
    ———

    ANOTHER QUESTION:

    what if i want to redirect stdout to a file and stderr to the console ?

    ———

    (tips:
    empty a file # echo “” > file
    filter file lines with “word” # cat file|grep word

    process file1 with command1; pass to command2, command3, and append in file2
    # command1 > file2
    )

    AT-HE

    23 Nov 08 at 1:57 pm

  85. AT-HE
    -what if i want to redirect stdout to a file and stderr to the console ?

    stderr goes to the console by default, so just don’t do anything with it

    Not sure if this is covered here, but &>file will send stdout and stderr to a file. (bash)

    man bash for more, or whatever your shell is.

    snoz

    23 Nov 08 at 5:27 pm

  86. Hi snoz,

    Thanks it is working for me now. not sure why IBM is asking for one more “\” at the end.
    anyways thanks a lot for your support.

    Rajiv

    24 Nov 08 at 12:33 am

  87. great thread…

    Santosh

    2 Dec 08 at 9:07 pm

  88. Thanks for the info.

    In Linux

    # command > test.log 2> test.log

    is the same as

    # command &> test.log

    tacallah

    5 Dec 08 at 5:18 pm

  89. discard all output messages?

    kevin

    11 Dec 08 at 12:23 am

  90. [...] Now if you use your php script in a cron task, don’t include the –verbose and make sure you check the $Verbose flag before printing any results. Don’t forget to add to the end of the command line ” > /dev/null 2>&1″ minus the quotes, this sends any std out and std error messages to a black hole. [...]

  91. Thanks for this!! I think the answer is buried somewhere in the shell’s manpage, but that’s such a big file I never figured it out.

    I wanted to redirect the output of a command to /dev/null. I put “2>1″ in one of my makefiles and I ended up with small “1″ files all over the place. Turns out I wanted “2>/dev/null”.

    Sympleko

    14 Jan 09 at 5:27 pm

  92. Really appreciate, too many unix books ignore the explanation of this common usage.

    jim

    14 Jan 09 at 7:44 pm

  93. i have a problem on AIX SO.

    I want to execute a daemon with NOHUP:

    nohup daemon > /dev/null 2>&1

    But after this, my hard disk is full with the output file!!

    Why? How?

    PS: nohup command generates a file called nohup.out, and i don’t need it.

    SaTanI

    10 Feb 09 at 5:00 pm

  94. This is repeating AT-HE’s question from above, and expanding it:

    what if i want to redirect stdout to a file and stderr to the console?

    AND

    I am running a system command from a cgi script. By default the stderr output of cgi scripts are not directed to the screen. You get nothing in the way of errors when running system commands from cgi scripts. If you want to display errors, you have to add “2>&1″ to any commands you execute within a cgi-script being run from a browser. But what I want to do is surpress the normal stdout stuff but direct stderr to the screen. My command might look something like this:
    rm /tmp/*.* > /dev/null
    except I want any errors to come to the screen. What do I add to command to direct stderr to the screen?

    lswote

    10 Mar 09 at 10:22 am

  95. Sounds good, but how about this

    nohup myprogram > foo.out 2> foo.err < /dev/null &

    what is this do? foo.err < /dev/null

    Alex

    17 Mar 09 at 1:15 pm

  96. [...] And finally all the url with wget and feeding any response to the black hole that is /dev/null [...]

  97. Thank you very much for the explanation.. It was really helpfull.

    Pradeep

    25 Mar 09 at 4:20 am

  98. complaints > /dev/null

    Saeros

    7 Apr 09 at 12:13 am

  99. Thank you so much. That 2>&1 thing has been eating at me for a while.

    darthmalis

    1 May 09 at 10:41 am

  100. Good explanation :)
    cheers!

    Anand

    23 Jun 09 at 4:01 am

  101. There now, That wasn’t so hard, was it? How come no-one else on the internet took the time to explain this? :-)

    Many thanks mate!

    Doubi

    25 Jun 09 at 6:08 am

  102. Thanks a lot , excellent explanation

    srini

    9 Jul 09 at 11:17 am

  103. excellent

    srini

    9 Jul 09 at 11:18 am

  104. excellent

    Nageswara J

    9 Jul 09 at 11:19 am

  105. nice explanation!!

    nayan

    13 Jul 09 at 10:42 am

  106. you could use &> /dev/null to achieve the same thing.

    absolado

    23 Jul 09 at 2:29 am

  107. Hi all. I’ve been looking for that days now. But mine is a bit different. Any help appreciated and plz have in mind that I’m no linux geek.
    1st of all I use the latest ubuntu 9.04 and I have this file LoginServer_loop.sh which i run with a bash script (think it is bash not sure) and looks like this.

    #!/bin/bash

    err=1
    until [ $err == 0 ];
    do
    . ./setenv.sh
    [ -f log/java0.log.0 ] && mv log/java0.log.0 “log/`date +%Y-%m-%d_%H-%M-%S`_java.log”
    [ -f log/stdout.log ] && mv log/stdout.log “log/`date +%Y-%m-%d_%H-%M-%S`_stdout.log”
    java -Dfile.encoding=UTF-8 -Xmx128m com.equal.loginserver.L2LoginServer > log/stdout.log 2>&1
    err=$?
    # /etc/init.d/mysql restart
    sleep 10;
    done

    Well what i’m looking for is to display the process like in windows the .bat file starts it and has “echo” to display all in a java console window.
    Hope i’ve explained as better as I could.
    Thanks for any help

    manos

    9 Aug 09 at 4:50 pm

  108. so shouldn’t this redirect 1 into 2 and
    then 2 into /dev/null in which case you’d
    think you’d see nothing, but you don’t

    echo foo >&2 2> /dev/null

    i must be missing something

    polypus

    15 Aug 09 at 8:03 pm

  109. @polypus
    I could be wrong, but I think what’s happening is you’re directing the output to wherever 2 is going (the console) Then you’re directing STDERR to dev/null after the fact. When it gets to 2>/dev/null, the output has already been sent to the console. Just a guess really.

    nulldev

    15 Aug 09 at 8:26 pm

  110. Thank you for the information :-)

    Priyanka

    18 Aug 09 at 12:41 pm

  111. I have trying to figure this out.. here is small example..
    ls -d /etc /eta
    ls: 0653-341 The file /eta does not exist.
    /etc

    if I give
    ls -d /etc /eta >/dev/null
    out put is
    “ls: 0653-341 The file /eta does not exist.”

    but if I give
    ls -d /etc /eta >/dev/null 2>&1

    no out put. where did the sterr go?? shoulnt it be same as above command?

    Soham

    1 Sep 09 at 5:08 am

  112. That’s very well done. AWSOME

    Mohamed

    8 Sep 09 at 5:49 pm

  113. Here’s a twist… Given the following line from a CRON file (that I did not write):
    11 11 * * * ($HOME/bin/wibble 2>&1) >> $HOME/logs/wibble.log

    (I understand the scheduling part)

    Does this mean that:
    1) Because nothing was specified, ‘normal’ output will still go to STDOUT
    2) 2>&1 means that STDERR will go to STDOUT too
    3) >> $HOME/logs/wibble.log means that STDOUT (which now includes STDERR) will now be redirected (and appended) to $HOME/logs/wibble.log

    Would another way to do this be (in CRON format):
    11 11 * * * ($HOME/bin/wibble >>$HOME/logs/wibble.log 2>&1)

    Or is there something about CRON that would make this not work? (perhaps the >> append)

    BTW, this is running on Fedora.

    Thanks!

    MontrealPaul

    29 Sep 09 at 7:13 pm

  114. Great explaination, i see it is explained in 2006, but still by googling we are directed to this site ….

    Really good explanation abt the file descriptors !!!

    Thanks !

    Abdul Raheem

    12 Oct 09 at 8:22 am

  115. The mnemonic that I use when trying to remember the syntax “2>&1″ is “two is greater than one”, where “than” and “and/&” rhyme.

    Bubba

    9 Dec 09 at 6:36 pm

  116. Bubba, what a great mnemonic! I am constantly doubting whether I’ve got it right when I type it. I think this’ll help me a lot.

    Xaprb

    9 Dec 09 at 6:52 pm

  117. Be careful when using the “tee” command with 2>&1.

    In the following examples, ONLY stderr output is triggered by calling “grep” for
    pattern “hello” on a non-existent file “NO_FILE.txt” during an attempt to redirect
    both stdout and stderr to file “./stdout_and_stderr”. Finally, I echo out the output
    file name “./stdout_and_stderr” and dump the file contents to stdout using “cat”.

    ########################################
    ## WORKED: stderr was redirected to file
    > grep hello NO_FILE.txt > ./stdout_and_stderr 2>&1 ; echo “cat ./stdout_and_stderr:” ; cat ./stdout_and_stderr
    cat ./stdout_and_stderr:
    grep: 0652-033 Cannot open NO_FILE.txt.
    ########################################

    ########################################
    ## FAILED: stderr was NOT redirected to file
    > grep hello NO_FILE.txt 2>&1 > ./stdout_and_stderr ; echo “cat ./stdout_and_stderr:” ; cat ./stdout_and_stderr
    grep: 0652-033 Cannot open NO_FILE.txt.
    cat ./stdout_and_stderr:
    ########################################

    ########################################
    ## WORKED: stderr was “tee”-ed BOTH to stdout AND file
    > grep hello NO_FILE.txt 2>&1 | tee ./stdout_and_stderr ; echo “cat ./stdout_and_stderr:” ; cat ./stdout_and_stderr
    grep: 0652-033 Cannot open NO_FILE.txt.
    cat ./stdout_and_stderr:
    grep: 0652-033 Cannot open NO_FILE.txt.
    ########################################

    ########################################
    ## FAILED: stderr was “tee”-ed to stdout but NOT to the file.
    ## In this case, 2>&1 was applied to the “tee”, but NOT to the “grep”
    ## before the pipe. Therefore, when piping commands, make sure that the 2>&1
    ## appears immediately BEFORE the pipe processing the command whose
    ## stderr stream you want redirected to stdout.
    > grep hello NO_FILE.txt | tee ./stdout_and_stderr 2>&1 ; echo “cat ./stdout_and_stderr:” ; cat ./stdout_and_stderr
    grep: 0652-033 Cannot open NO_FILE.txt.
    cat ./stdout_and_stderr:
    ########################################

    D

    23 Dec 09 at 3:44 pm

  118. Goodone bro! thanks.

    sojin

    6 Jan 10 at 10:55 pm

  119. Do you ever use 2>&- at the end of a command to temporarily turn off the stderr file descriptor?

    Josh

    12 Jan 10 at 3:14 pm

Leave a Reply