Input Output Redirection in Linux

06 Jan, 2022
The work of any command is either taking input or gives an output or both. So, Linux has some command or special character to redirect these input and output functionalities. For example: suppose we want to run a command called “date” if we run it will print the output to the current terminal screen. But our requirement is different, we don’t want the output to be displayed on the terminal. We want the output to be saved in a file. This could be done very easily with output redirection. Redirection here simply means diverting the output or input. 
Similarly, if we have a command that needs input to be performed. Let take a command “head” this needs input to give output. So either we write input in the form of command directly or redirect input from any other place or file. Suppose we have a file called “file.txt” to print the starting some lines of the file we could use the “head”. So let’s see how this all is done on the terminal. 
Types of Redirection 
1. Overwrite  
“>” standard output
“<” standard input
2. Appends  
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
“>>” standard output
“<<” standard input
3. Merge
“p >& q” Merges output from stream p with stream q
“p <& q” Merges input from stream p with stream q
Implementation: So whatever you will write after running this command, all will be redirected and copied to the “file.txt”. This is standard output redirection.  
cat > file.txt
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
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
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
To see the working of append standard input: 
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.
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
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
  
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“. 
Linux Tutorials | Redirection - The three standard files in Linux | GeeksforGeeks - YouTubeGeeksforGeeks529K subscribersLinux Tutorials | Redirection - The three standard files in Linux | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 9:28•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=SP0gDpamofY" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
yashbeersingh42
akshaysingh98088
adnanirshad158
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Docker - COPY Instruction
scp command in Linux with Examples
chown command in Linux with Examples
SED command in Linux | Set 2
nohup Command in Linux with Examples
Introduction to Linux Operating System
Array Basics in Shell Scripting | Set 1
chmod command in Linux with examples
mv command in Linux with examples
Basic Operators in Shell Scripting