What Is Input Output Redirection in Linux?

Input and output (I/O) redirection in the Linux command line is a mechanism that allows users to change the default sources of input and destinations for output when running commands. By default, Linux commands receive input from your keyboard and display output or errors directly on your terminal screen. By using redirection operators, you can instruct the shell to read input from a file instead of the keyboard, save command output to a file, or separate error messages from standard output.

The Three Standard Streams

Every command executed in the Linux shell automatically opens three data streams, each identified by a numeric file descriptor (FD):

  1. Standard Input (stdin, FD 0): The default stream that provides data to a command, usually originating from the keyboard.
  2. Standard Output (stdout, FD 1): The default stream that carries the normal output of a command, typically displayed in the terminal window.
  3. Standard Error (stderr, FD 2): The default stream dedicated to error messages, separated from regular output so diagnostics are not mixed with expected data.

Output Redirection (stdout)

Output redirection sends the standard output of a command to a file instead of the screen.

Error Redirection (stderr)

Because stderr uses file descriptor 2, you must explicitly reference the descriptor number when redirecting errors.

Input Redirection (stdin)

Input redirection allows a command to read data from a file rather than waiting for keyboard input.

Redirection vs. Pipelines

While redirection operators (<, >, >>) route streams between commands and files or devices, the pipe operator (|) connects the standard output of one command directly into the standard input of another command. Understanding and combining both techniques allows for powerful and flexible automation scripts in the Linux shell.