Input Output Redirection in Linux

Last Updated : 3 Nov, 2025

Redirection in Linux is a method of controlling where the input and output of commands go, allowing users to send command outputs to files or take inputs from files instead of the terminal.

Common redirection operators include:

  • >: Redirects standard output (overwrites file).
  • >>: Redirects and appends output to a file.
  • <: Takes input from a file instead of the keyboard.
  • 2>: Redirects error messages to a file.
  • &>: Redirects both standard output and error to a file.

Types of Redirection

Redirection in Linux is mainly divided based on how data is transferred between input, output, and error streams.

1. Overwrite Redirection: 

Overwrite redirection is useful when you want to store/save the output of a command to a file and replace all the existing content of that file. for example, if you run a command that gives a report, and you want to save the report to the existing file of the previous report you can use overwrite redirection to do this. 

  • ">" standard output
  • "<" standard input

Implementation:

So whatever you will write after running this command, will be redirected and copied to the "file.txt". This is standard output redirection.  

cat > file.txt

Input-and-Output-Redirection-in-Linux


Now, this is standard input redirection, cat command will take the input from "file.txt" and print it to the terminal screen. This line of code also shows the real working and meaning of the cat command that is copy and paste. Many people have a misconception that the cat is used to create a file, but it is not true, the main work of the cat is to copy the input and give the output to the screen.  

cat < file.txt
cat command input redirection example


Let's see an example to understand the real work of cat command  

cat


Just type cat on the terminal and hit enter. It will ask for the input lines, you could write your name and hit enter. You will see your input will be reprinted. 

(base) [root@localhost ~]# cat
Hello this is GeeksForGeeks
Hello this is GeeksForGeeks

Cat-command-in-Linux-IO-Redirection-Example


This is used when we want to append some lines to the existing content of the file. If you use only a single angular bracket all the content of the file will be lost. 

cat >> file.txt


2. Append Redirection: 

With the help of this Redirection, you can append the output to the file without compromising the existing data of the file.

  • ">>" standard output
  • "<<" standard input

Implementation:

A here-document is used to redirect input into an interactive shell script or program. You can run any program within a shell script without user action by supplying the required input for the interactive program, or interactive shell script.

The general form for a here document is −

Syntax:
command << delimiter
document
delimiter

(base) [root@localhost ~]# cat << helo.txt
> Hello This is
> GeeksForGeeks
helo.txt
Hello This is
GeeksForGeeks
(base) [root@localhost ~]#

Note: Here, helo.txt is a delimiter.

The delimiter marks the ending point of the document. Without it, the shell continues to read the input forever. The delimiter must be a single word that does not contain spaces or tabs.

To-see-the-working-of-append-standard-input

3. Merge Redirection:

This allows you to redirect the output of a command or a program to a specific file descriptor instead of standard output. the syntax for using this is ">&" operator followed by the file descriptor number.

  • "p >& q" Merges output from stream p with stream q
  • "p <& q" Merges input from stream p with stream q

Implementation:

Error Redirection: Error redirection is transferring the errors generated by some false commands to a file rather than STDOUT.

Whenever a program is executed at the terminal, 3 files are generated: standard input(0), standard output(1), standard error(2). These files are always created whenever a program is run. By default, an error stream is displayed on the screen. 

Examples:

1. In the below-mentioned example, the file descriptor used above is 2(STDERR). Using "2>" re-directs the error output to a file named "error.txt" and nothing is displayed on STDOUT.

$ somerandomcommand 2>error.txt

error Redirection in linux

2.  Here, 2>&1 means that STDERR redirects to the target of STDOUT. More formally, the error message generated by "2" gets merged with the current output "1". 

$ ls GEEK GFG > error.txt 2>&1

error Redirection in linux

 In the above example, the directory GEEK is not present. The error output is merged with the standard output which in turn is being re-directed to "error.txt". 

Suggested Quiz

0 Questions

Quiz Completed Successfully

Your Score : 0/0

Accuracy : 0%

Comment
Article Tags:

Explore