stdin, stdout, stderr: redirection and pipes
This lesson is open to everyone. The other lessons open with a package: sign in or sign up, then choose a package.
Key points
- Every command has three streams: stdin (input, 0), stdout (normal output, 1), stderr (error output, 2).
>writes output to a file and erases the old content;>>appends to the end of the file.2>writes errors to a separate file;2>/dev/nullthrows errors away;&>writes both to one file.- A pipe
|feeds one command's output into the next command's input:cat log | grep Failed | wc -l. teewrites output both to the screen and to a file;sudo teeis the right way to write to a file owned by root.
Three streams
When a program starts, three "pipes" are connected to it:
| Stream | Number | What it is | By default |
|---|---|---|---|
| stdin | 0 | Input | Keyboard |
| stdout | 1 | Normal result | Screen |
| stderr | 2 | Error messages | Screen |
Because results and errors come through different streams, you can handle them separately.
Output to a file: > and >>
ali@srv-web01:~$ date > report.txt ali@srv-web01:~$ uptime >> report.txt ali@srv-web01:~$ cat report.txt Mon Dec 1 11:05:12 +04 2026 11:05:12 up 12 days, 3:41, 1 user, load average: 0.08, 0.12, 0.10
>creates the file and, if it exists, erases it and writes again.>>appends to the end of the file; the old data stays.
The most common mistake: writing > instead of >> to a log file and losing the old records. When in doubt, use >>.
Separating errors: 2>
ali@srv-web01:~$ ls /etc/hostname /missing ls: cannot access '/missing': No such file or directory /etc/hostname ali@srv-web01:~$ ls /etc/hostname /missing 2> errors.txt /etc/hostname ali@srv-web01:~$ cat errors.txt ls: cannot access '/missing': No such file or directory
| Form | What it does |
|---|---|
2> file | Writes only the errors to a file |
2>/dev/null | Throws errors away (/dev/null is a "black hole" that swallows everything) |
> file 2>&1 | Writes errors to the same file as the output |
&> file | The short bash form of the same |
For example, to hide "Permission denied" errors while searching the whole disk: find / -name "*.conf" 2>/dev/null.
Pipes: joining commands
The Linux philosophy: each command does one job well, and big jobs are done by joining commands. The joint is the pipe: |.
ali@srv-web01:~$ sudo cat /var/log/auth.log | grep "Failed password" | wc -l 4
catsends the file to its output.greplets through only the lines with "Failed password".wc -lcounts the lines.
Each command takes the previous one's result as input. Nothing is written to a file; the result goes straight to the screen.
tee: to the screen and a file
ali@srv-web01:~$ df -h / | tee disk.txt Filesystem Size Used Avail Use% Mounted on /dev/sda2 40G 12G 26G 32% /
tee shows what it receives on the screen and also writes it to a file. It is useful when you need to write to a file owned by root:
ali@srv-web01:~$ sudo echo "10.20.0.20 srv-db01" >> /etc/hosts -bash: /etc/hosts: Permission denied ali@srv-web01:~$ echo "10.20.0.20 srv-db01" | sudo tee -a /etc/hosts 10.20.0.20 srv-db01
Why did the first command fail? The >> redirection is done by your own shell, not by sudo, and you have no right to write to /etc/hosts. sudo tee -a writes to the file as root (-a — append).
Try sudo tee -a in the "Names: /etc/hosts" task of the Linux simulator.
Check yourself
5 questions · answers are shown instantly · 3 correct answers complete the lesson
1Which stream do error messages come through?
2>.2What does echo test > file.txt do if the file already exists?
> overwrites; you need >> to append.3What does find / -name x 2>/dev/null do?
/dev/null swallows what is written to it; here the errors are thrown away.4What does a pipe | do?
cat log | grep Failed: cat's output becomes grep's input.5What is the right way to append a line to a file owned by root?
sudo tee -a writes to the file as root.Comments
Sign in to comment.