Introduction to Unix

../_images/ksus5.jpg ../_images/UNIX_blocks5.jpeg

4.5. File Redirection

As was discussed in Standard Files and File Descriptors, standard input, output and error (stdin, stdout, stderr) are open file descriptors in every process.

4.5.1. stdin Redirection

Redirect standard input from a file (instead of the keyboard) using the < metacharacter.

$ command < file

4.5.2. stdout Redirection

Redirect standard output to a file (instead of the terminal) using the > metacharacter. The >> metacharacter redirects stdout appending the file.

$ command > file

$ command >> file

4.5.3. stderr Redirection

Redirect standard error (error messages) to a file (instead of the terminal) using the 2> metacharacter. The 2>&1 metacharacter redirects stderr to the same file that stdout was redirected to.

# stdout to terminal and stderr to file:
$ command 2> file

# stdout and stderr to different files:
$ command > out_file  2> error_file

# stdout and stderr to the same file:
$ command > file 2>&1