# POSIX Redirection

Originally written for and copyright by *_[Information Security
Magazine](http://www.infosecuritymag.com/)_*, February 2002 " *[Windows
Security Scripting](http://www.infosecuritymag.com/2002/feb/features_scripting.shtml)*."

Note: Under DOS & Win9x/ME some of the simple STDOUT redirection and
pipes work, but none of the advanced STDERR or multiple command methods
are supported.

-----

## IO Redirection in Windows NT, 2000, XP, UNIX (sh, bash and variants)

### File Descriptors

| FD | Description                               |
| -- | ----------------------------------------- |
| 1  | STDOUT                                    |
| 2  | STDERR                                    |
| 3+ | Additional files as opened by the process |


### Redirection

|  Command          | Description                                                             |
| ----------------- | ----------------------------------------------------------------------- |
| `cmd1 \| cmd1`    | Pipe STDOUT of cmd1 into STDIN of cmd2                                  |
| `\> file`         | Direct STDOUT to file, overwriting existing contents                    |
| `\>\> file`       | Direct STDOUT to file, appending to existing contents                   |
| `\>| file`        | Direct STDOUT to file, overwriting even if noclobber is set (UNIX only) |
| `2\> file`        | Direct STDERR to file, overwriting existing contents                    |
| `2\>\> file`      | Direct STDERR to file, appending to existing contents                   |
| `\< file`         | Get STDIN from file                                                     |
| `2\>&1`           | Direct STDERR to the same place as STDOUT                               |
| `\>& file`        | Direct both STDOUT and STDERR to file                                   |
| `2\>&`            | Duplicate STDOUT to STDERR                                              |
| `echo 'foo' \>&2` | Send output to STDERR instead of STDOUT                                 |


### Notes:

1.  Numbered file descriptions above may be used arbitrarily.
2.  noclobber is a UNIX setting that prevents overwriting (clobbering)
    existing files by redirection.
3.  UNIX `/dev/null` is equivalent to Windows `NUL`. Windows `NUL` is not case
    sensitive.
4.  `^` is the meta-character escape in DOS/Windows, so it may sometimes
    be necessary to use `^|` (e.g. when using `egrep` in a batch file). You
    may use `^^` for a literal `^`.

### Examples:


|  Command                       | Description                             |
| ------------------------------ | --------------------------------------- |
| `dir c:\*.* > myls.txt`        | Redirect output of ls into myls.txt, overwriting or creating myls.txt if necessary                                                       |
| `dir c:\winnt\*.* >> myls.txt` | Append more output of ls into myls.txt                                                                                                   |
| `noisy_cmd > NUL`              | Make STDOUT output from noisy\_cmd go away                                                                                               |
| `noisy_cmd 2> NUL`             | Make STDERR output from noisy\_cmd go away                                                                                               |
| `noisy_cmd > NUL 2>&1`         | Make ALL output from noisy\_cmd go away                                                                                                  |
| `noisy_cmd 2> NUL 1>&2`        | Make ALL output from noisy\_cmd go away                                                                                                  |
| `noisy_cmd \| more`            | Pipe noisy\_cmd STDOUT into more (or less or whatever)                                                                                   |
| `noisy_cmd 2>&1 \| more`       | Pipe noisy\_cmd STDOUT and STDERR into more (this is great for those "net" commands that scroll off the screen when you try to get help) |
| `echo some message 1>&2`       | Use the echo command to send output to STDERR (it usually goes to STDOUT).                                                               |


-----

## Running Multiple Commands in Windows NT, 2000, UNIX (sh, bash and variants)

Use parentheses to nest as needed.

|  Command         | Description                                         |
| ---------------- | --------------------------------------------------- |
| `cmd1 & cmd2`    | Run cmd1, then run cmd2                             |
| `cmd1 ; cmd2`    | Run cmd1, then run cmd2 (UNIX only)                 |
| `cmd1 && cmd2`   | Run cmd1. If it finishes successfully then run cmd2 |
| `cmd1 \|\| cmd2` | Run cmd1. If it fails then run cmd2                 |
