Operations 6 min read

Master Linux I/O Redirection: From Basics to Advanced Tricks

This guide explains Linux shell I/O redirection, covering output and input redirection, file descriptor semantics, the importance of order, using /dev/null to discard output, and common patterns for merging and appending streams, all with clear examples.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux I/O Redirection: From Basics to Advanced Tricks

Output Redirection

In Linux shells, the > operator redirects standard output (fd 1) to a file, while 2>&1 redirects standard error (fd 2) to the same destination as stdout. The ampersand indicates that the target is a file descriptor, not a regular file.

1 => stdout
2 => stderr
0 => stdin

Thus 2>1 creates a regular file named “1” containing stderr, whereas 2>&1 sends stderr to the file descriptor 1 (typically /dev/stdout), effectively merging the two streams.

The combined form &>file or >&file is equivalent to >file 2>&1.

Order of Redirection

Redirections are processed left‑to‑right. For example:

find /etc -name .bashrc > list 2>&1   # stdout goes to list, then stderr is merged

If the order is reversed ( find /etc -name .bashrc 2>&1 > list), stderr is first merged into the original stdout stream, and the subsequent redirection of stdout to list does not affect the already‑merged stderr.

Input Redirection

command1 < file1

This feeds the contents of file1 to the command’s standard input.

Redirecting to /dev/null

Sending output to /dev/null discards it. To silence both stdout and stderr:

command > /dev/null 2>&1

Deeper Redirection

Every Unix process opens three standard file descriptors:

stdin (0) : default input source.

stdout (1) : default output destination.

stderr (2) : default error output.

Common redirection patterns: command > file – redirect stdout to file. command < file – redirect stdin from file. command >> file – append stdout to file. command 2> file – redirect stderr to file. command 2>> file – append stderr to file. command > file 2>&1 – merge stderr into stdout and write both to file. command < file1 > file2 – redirect stdin from file1 and stdout to file2.

Redirecting stderr to stdout is useful because stdout is usually buffered while stderr is not, allowing consistent ordering of output.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxShellcommand-lineUnixI/O Redirection
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.