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 gives us output on the screen. However, if we want to save the result in a file instead, we can use output redirection. This way, we can store the output of the date command in a file and refer to it later. These redirections can come in handy when we work with multiple and large outputs or inputs since we can use file data directly as input and store results in files.
All this can be done easily on the terminal using some simple commands. In this article, we will talk about different types of Redirections in linux and how to use Redirections in linux.
Types of Redirection
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
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
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
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
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 −
command << delimiter
(base) [root@localhost ~]# cat << helo.txt
> Hello This is
Hello This is
(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.
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
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“.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!