Operating Systems & Administration

[Day 3] 2. Redirection

D
DITISS Admin
••4 min read

Redirection in Linux

In Linux, the three standard streams—stdin (standard input), stdout (standard output), and stderr (standard error)—can be redirected. Redirection alters the flow of input/output for commands.

Basic Commands:

  • wc
    : Returns the number of lines, word count, and character count in a file.

    Example:

    wc < test.txt

    This command takes input from the file
    test.txt
    instead of standard input (stdin), demonstrating input redirection.

!2. Redirection.png

Input and Output Redirection:

  • wc < test.txt > output.txt

    This command demonstrates both input and output redirection. Input is taken from
    test.txt
    , and the result is redirected to the file
    output.txt
    .

Error Redirection and Merging:

  • wc -p < test.txt > output.txt 2>&1

    This merges stderr (error stream) with stdout, where
    2
    refers to
    stderr
    and
    1
    refers to
    stdout
    . The command means that both output and errors are written into
    output.txt
    .

How Redirection works?

  1. Create a FIFO file

    mkfifo fifo

    • This creates a named pipe (FIFO file) called
      fifo
      . It's a special file that acts as a temporary buffer for data between processes, ensuring that data is read in the same order it was written.
  2. Sorting numbers from

    numbers.txt
    and writing them into
    fifo
    sort numbers.txt > fifo

    • The command
      sort numbers.txt
      sorts the numbers in the file
      numbers.txt
      .
    • The
      >
      symbol redirects the sorted output to the FIFO file
      fifo
      , instead of displaying it on the terminal (which is the default behavior of
      stdout
      ).
  3. Reading from

    fifo
    using
    uniq
    and removing duplicates
    uniq < fifo > output.txt

    • Here, the input redirection (
      < fifo
      ) reads the sorted data from the FIFO file
      fifo
      and passes it to the
      uniq
      command.
    • The
      uniq
      command removes duplicate entries from the sorted list.
    • The
      >
      symbol redirects the final output (unique sorted numbers) to a file called
      output.txt
      .

!2. Output Redirection.png


How Redirection Works Internally:

  1. Input Redirection (

    <
    ):

    • For
      uniq < fifo
      , the
      uniq
      command reads its input from
      fifo
      , which contains the sorted data from the previous step. This replaces the default stdin (keyboard input) with
      fifo
      .
  2. Output Redirection (

    >
    ):

    • In both
      sort numbers.txt > fifo
      and
      uniq < fifo > output.txt
      , the output is redirected from the terminal (the default stdout) to either a FIFO file or an output file (
      output.txt
      ).
    • The default output stream (
      stdout
      , file descriptor
      1
      ) is closed, and the new file (
      fifo
      or
      output.txt
      ) is assigned a file descriptor.
  3. FIFO (First In, First Out):

    • A FIFO acts like a buffer between two processes (sort and uniq in this case). One process writes to it (
      sort
      ), while another process reads from it (
      uniq
      ). The data is processed in the same order it was written.

Summary of Redirection in the Example:

  • You sort the contents of
    numbers.txt
    and redirect the sorted output to the FIFO (
    sort numbers.txt > fifo
    ).
  • Then, you read the sorted data from
    fifo
    using input redirection (
    < fifo
    ) and pass it to
    uniq
    , which removes the duplicates and redirects the unique data into
    output.txt
    .

This is how redirection works, allowing you to process data without pipes, instead relying on explicit redirection with FIFO files.

Read Next

8/26/2026

[Day 3] 3. Pipes

### **Pipes in Linux** Pipes are used for **Inter-Process Communication (IPC)**, allowing data to flow between processes. They have two ends: one f...

8/26/2026

[Day 3] 4. Some more commands

#### **1. Head and Tail Commands:** - **`head`**: Displays the first part of files. `head -5 filename.txt` - This command pr...