ITNuvoAkademiya
Academy homePackagesPlaylistsToolsSubnet calculatorSearchGlossary
CiscoCCNA · CCNA: networking fundamentals1. Introduction2. Network Fundamentals3. Network Access4. IP Connectivity5. IP Services6. IPv67. Security Fundamentals8. AutomationCCNP: Enterprise networks · coming soon
LinuxLinux: system administrator üçün · Linux for system administrators1. Getting to know Linux2. Working with text3. Users and permissions4. Processes and packages5. Inside the system6. Monitoring and logs7. NetworkingRed Hat Enterprise Linux · Red Hat Enterprise Linux: from beginner to advanced1. Getting started2. Packages3. Users and permissions4. SELinux5. Services6. Storage7. Networking8. Building services9. Boot and recovery10. DevOps11. Security and monitoring
FortinetFortiGate Administrator · FortiGate Administrator: from first login to ZTNA1. Getting to know FortiGate2. Routing, policy and NAT3. Security profiles4. Users and logs5. Resilience, SD-WAN and automation6. VDOM7. VPN and ZTNA8. Proxy9. DMZ
MonitoringZabbix · Zabbix: from installation to alerts1. Introduction2. Installation and the database3. Basics: groups, templates, users4. Connecting devices5. Alerts, Grafana and the API
VirtualizationProxmox VE · coming soonVMware ESXi · coming soon
My account / Sign in
AZENRU
← itnuvo.com

stdin, stdout, stderr: redirection and pipes

Lesson 2.17 minBeginnerDecember 1, 2026
Open

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/null throws 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.
  • tee writes output both to the screen and to a file; sudo tee is the right way to write to a file owned by root.

Three streams

When a program starts, three "pipes" are connected to it:

StreamNumberWhat it isBy default
stdin0InputKeyboard
stdout1Normal resultScreen
stderr2Error messagesScreen

Because results and errors come through different streams, you can handle them separately.

Output to a file: > and >>

srv-web01
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>

srv-web01
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
FormWhat it does
2> fileWrites only the errors to a file
2>/dev/nullThrows errors away (/dev/null is a "black hole" that swallows everything)
> file 2>&1Writes errors to the same file as the output
&> fileThe 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: |.

srv-web01
ali@srv-web01:~$ sudo cat /var/log/auth.log | grep "Failed password" | wc -l
4
  1. cat sends the file to its output.
  2. grep lets through only the lines with "Failed password".
  3. wc -l counts 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

srv-web01
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:

srv-web01
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?

Errors go to stderr (2), which is why they are redirected separately with 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?

The redirection is done by your shell; sudo tee -a writes to the file as root.
Was this lesson useful?

Comments

Sign in to comment.