Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
Input Output Redirection in Linux
Next article icon

Linux Error Redirection

Last Updated : 02 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Redirection is a feature in Linux which can be used to change the standard input device (keyboard) or standard output device (screen) during the execution of a command. The basic process of any Linux command is that it takes an input and gives output but the standard/input and output can be changed using the redirection technique.

Standard Output and Standard Error

In Linux, there are two primary output streams:

  1. Standard Output (stdout): This stream is used for regular output, such as the results of a command or program execution.
  2. Standard Error (stderr): This stream is used for error messages and diagnostic information.

By default, both stdout and stderr are displayed on the terminal. However, you can redirect these streams using special file descriptors and redirection operators.

Redirecting Standard Error

The redirection operator > only redirects the standard output (stdout) of a command. To redirect the standard error (stderr), you need to use the file descriptor 2.

2> stderr

The 2> operator redirects the standard error (stderr) of a command to a file. This separates error messages from the regular output, providing a cleaner output. The syntax is as follows:

command 2> file

Consider an example:

eccho "gfg" 2>file.txt
echo "gfg" 2>file.txt

There is no command as eccho and hence error message will be displayed. But if 2>file.txt is used, then the error will be redirected to file.txt and no error is displayed on the screen. The cat command can be used to display the content of the file named file.txt which is the error message for the previous command. While executing the second command, gfg is displayed as the echo is a command in Linux to display. Hence, 2> does not redirect standard output.

2>stderr error redirection” width=”445″ height=”inherit” srcset=”https://media.geeksforgeeks.org/wp-content/uploads/20201125214826/1.png, “><figcaption> </figcaption></figure> <h3 style=2>&1

The 2>&1 operator redirects both standard error (stderr) and standard output (stdout) to the same file or stream.

Syntax:

command > file 2>&

Consider an example:

eccho "gfg" >error.txt 2>&1
echo "gfg" >error.txt 2>&1

There is no command as echo and hence the error message is redirected to the file error.txt and no error is displayed. The second command echo “gfg” is correct but still, no output is displayed, as the standard output is also redirected to the file error.txt. The content of the file can be displayed after each step using the cat command.

Note: >& can be used to redirect both standard output and standard error, but it is not supported in all shells. sh and ksh do not support >& while bash and zsh support it.

Conclusion

Redirection in Linux lets you control where command input and output goes. Using different operators like >, >>, and 2>, you can send regular output and error messages to files instead of the screen. This helps keep things neat and organized. Redirection is a handy tool that makes it easier to work with commands and manage output on the Linux command line, whether you’re running scripts, troubleshooting issues, or processing data.



Next Article
Input Output Redirection in Linux

M

mharshita31
Improve
Article Tags :
  • Linux-Unix

Similar Reads

  • Input Output Redirection in Linux
    In Linux, whenever an individual runs a command, it can take input, give output, or do both. Redirection helps us redirect these input and output functionalities to the files or folders we want, and we can use special commands or characters to do so. For example, if we run the "date" command, it giv
    4 min read
  • Oralyzer : Linux Tool To Identify Open Redirection
    Open Redirection is the security vulnerability in a web-based application that causes it to fail to properly authenticate URLs. When any web-based application receives requests for URLs, they are supposed to prove that the requested URLs are an integral part of their domain. No other URLs should be
    4 min read
  • Linux Terminal
    Linux is one of the most powerful operating systems used for development, system administration, cybersecurity, and cloud computing. However, installing Linux on a physical machine is not always convenient. This is where a Linux terminal becomes an ideal solution. What is a Linux Terminal?The Linux
    9 min read
  • How to Redirect Standard (stderr) Error in Bash
    Bash (Bourne Again SHell) is the scripting language that is generally used to automate tasks. It is a Linux shell and command language written by Brain Fox. Bash is the default shell for Linux and macOS operating systems. What is a Standard Error in Bash?When a code is run in Bash shell and if the c
    7 min read
  • Linux Commands Cheat Sheet
    Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
    13 min read
  • How to Run a File in Linux
    The command line is one of the most powerful tools in Linux. It allows you to execute commands, manage files, and automate tasks all from a single terminal window. One common task you'll often need to do is run a file, whether it’s a script, a compiled program, or even a text file. In this article,
    6 min read
  • How to Open a File in Linux​
    In Linux, a file is a fundamental unit of storage, representing everything from documents and images to system logs and program data. Unlike traditional operating systems, Linux treats almost everything—files, directories, devices, and processes—as a file. Whether you're accessing a simple text docu
    6 min read
  • Piping in Unix or Linux
    A pipe is a form of redirection (transfer of standard output to some other destination) that is used in Linux and other Unix-like operating systems to send the output of one command/program/process to another command/program/process for further processing. The Unix/Linux systems allow the stdout of
    6 min read
  • Processes in Linux/Unix
    A program/command when executed, a special instance is provided by the system to the process. This instance consists of all the services/resources that may be utilized by the process under execution. Whenever a command is issued in Unix/Linux, it creates/starts a new process. For example, pwd when i
    6 min read
  • OpenRedireX – Open Redirection Vulnerability Finder Tool in Linux
    Open redirect is a security defect in an app or a web page that causes it to fail to properly authenticate URLs. When apps and web pages have requests for URLs, they are supposed to prove that those URLs are part of the expected page's domain. To test the web-based application manually for Open Redi
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences