Input Output Redirection in Linux
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
- “>” standard output
- “<” standard input
- “>>” standard output
- “<<” standard input
- “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
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 delimeter.
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.
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“.