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.
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 => stdinThus 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 mergedIf 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 < file1This 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>&1Deeper 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
