Posted at 2008/04/10 14:12 // in Tips // by Daniel

  • The following command saves stdout and stderr to the files "out.txt" and "err.txt", respectively.
    [root@server /root]# ./cmd 1>out.txt 2>err.txt
  • The following command appends stdout and stderr to the files "out.txt" and "err.txt", respectively.
    [root@server /root]# ./cmd 1>>out.txt 2>>err.txt
  • The following command functions similar to the above two commands, but also copies stdout and stderr to the files "stdout.txt" and "stderr.txt", respectively.
    [root@server /root]# (((./cmd | tee stdout.txt) 3>&1 1>&2 2>&3\
    |tee stderr.txt) 3>&1 1>&2 2>&3) 1>out.txt 2>err.txt

    Note: Lines that end in a backslash are continued on the next line. Any such lines should be keyed in as one complete line. The lines are too long to fit on the web page without formatting them this way.

  • Capturing stdout
    The following will capture a copy of stdout and save it to a file called "stdout.txt"
    [root@server /root]# ./cmd | tee stdout.txt

    stdout goes through the pipe and tee is able to save a copy of it to the file "stdout.txt"; however, we just lost control of stderr. stderr will not go through the pipe, instead it goes directly to our display.

  • Gaining control of stderr and stdout.
    Lets gain control again of stderr and stdout. We do this by surrounding our command with a set of parenthesis.
    [root@server /root]# (./cmd | tee stdout.txt)

    Capturing stderr
    Now that we have swapped our stdout and stderr, lets hook up tee once again. tee will now capture stderr (tee believes that it is really stdout because stdout is the only thing that can come through the pipe).

    [root@server /root]# (./cmd | tee stdout.txt) 3>&1 1>&2 2>&3 \
    | tee stderr.txt


  • 크리에이티브 커먼즈 라이센스
    Creative Commons License