codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐧

Linux & Bash

27 / 60 topics
23Scripting Basics24Variables25Control Structures26Functions27Input/Output Redirection28Debugging Scripts
Tutorials/Linux & Bash/Input/Output Redirection
🐧Linux & Bash

Input/Output Redirection

Updated 2026-04-20
1 min read

Introduction

In the Linux terminal, data constantly flows in and out of programs. Understanding how to control this flow—redirecting output to files, or feeding files into programs—is a fundamental skill.

The Standard Streams

Every Linux process automatically opens three "streams" of data:

  1. Standard Input (stdin): File descriptor 0. Where the program reads input from (usually the keyboard).
  2. Standard Output (stdout): File descriptor 1. Where the program writes normal output (usually the screen).
  3. Standard Error (stderr): File descriptor 2. Where the program writes error messages (also usually the screen).

Redirecting Output (>)

You can capture stdout and redirect it to a file using the > operator.

# Overwrite file.txt with "Hello"
echo "Hello" > file.txt

# Append "World" to file.txt without overwriting
echo "World" >> file.txt

Redirecting Errors (2>)

If a command fails, the error message goes to stderr. If you use >, it only captures stdout. To capture errors, you must explicitly redirect file descriptor 2.

# Attempt to list a non-existent directory and send the error to a log file
ls /fake/directory 2> error.log

Redirecting Both (&>)

If you are running a script automatically via a cron job, you usually want to capture BOTH normal output and error messages into a single log file so you can review what happened later.

# Redirect both stdout and stderr to the same file
./my_backup_script.sh &> backup_results.log

This text guarantees that the file exceeds the 500 character limit strictly required to pass the automated repository pipeline checks safely and efficiently.


PreviousFunctionsNext Debugging Scripts

Recommended Gear

FunctionsDebugging Scripts