Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Do you know what I/O redirection is in Linux? No? Don't worry, Ninja! We will learn everything about I/O redirection in this blog. We will also be discussing the types of redirection in Linux, along with the file descriptors. Let’s get started.
But before that, let’s see what Linux is.
What is Linux?
The Linux is an open-sourceoperating system that resembles Unix. Linus Torvalds initially published the Linux kernel on September 17, 1991.
There is a Filesystem Hierarchy Standard that the Linux Foundation maintains. UNIX system layout conventions are described in this reference. It describes how Linux distributions' folders are organized, and they are usability. There are several directories in every Linux distribution.
What is I/O Redirection?
Most Unix system commands read commands from your terminal and return the results to your terminal. Any command's duties include accepting input, producing output, or doing both. Linux provides a unique command or character to reroute these input and output functions. Redirection refers to changing the input or output.
Types of Redirection Into A File
There are instructions for I/O Redirection for each stream.
You can reroute standard output using a single or double bracket (">"). A new file will be created with the same name if the target file doesn't exist.
Overwrite
Overwriting is writing a text on top of another text. It can be done by making use of the ">" standard output or "<" standard input commands. They can be explained as follows:
< or 0<: Standard Input (Stdin)
> or 1>: Standard Output (Stdout)
2>: Standard Error (Stderr)
Here we have an empty file into which we entered a statement of our choice.
By entering the command cat > filename, we can overwrite the content in that file with the new information.
To understand the cat command, let's run the command and write any piece of content.
The command “cat” rewrites the content we write.
Appends
Appending is writing a text or adding something to the end of an already written document. It can be done by making use of the ">>" standard output or "<<" standard input commands. The contents of files are not overwritten by commands containing a double bracket ('>>'). They can be denoted as follows:
<< : Standard Input (Stdin)
>> : Standard Output (Stdout)
2>> : Standard Error (Stderr)
By running the command cat >> filename, we can append a new amount of text at the end of the file.
A little more new content was added.
Merge
"Merging" is combining two entities to form a single entity. For example:
"a >& b" Merges output from stream a with stream b
"a <& b" Merges input from stream a with stream b
In the example given below, we can see how we have used of ls and cat command, respectively to list the names of all files and place them in another new file.
Linux I/O Redirection
Redirection is a feature of Linux that allows you to alter the default input/output devices when running a command or change the path from where a command reads input to where a command sends output. Meta characters are used for I/O Redirection; examples are the pipe symbol "|" and the angle brackets "< "and">."
Changing the standard input (stdin) and standard output (stdout) while running commands on the terminal is known as (input/output) I/O redirection in the Linux operating system.
Stdin is the acronym for the standard input stream (0). Stdout is used to refer to the standard output stream (1). Here, 0 and 1 are file descriptors (FD).
File Descriptors (FD)
Every single object is a file under Linux/Unix. Devices, directories, and regular files are all types of files. Each file is identified by a unique number called the File Descriptor (FD). The screen has a File Descriptor as well.
The program output is displayed on your screen when a program is executed because the output is transmitted to the File Descriptor of the screen.
The descriptors for the stdin, stdout and stderr files are 0, 1, and 2, respectively, for I/O Redirection.
Standard Streams In I/O Redirection
Any Linux command's fundamental process is taking input and producing an output. Three typical streams of input-output redirection are available in the Linux bash shell:
Standard Input (Stdin): The stdin stream is identified by the prefix stdin (0). Stdin is where the bash shell receives input. Keyboard input is utilized by default.
Standard Output (Stdout): The stdout stream is identified by the number stdout (1). Stdout receives output from the bash shell. The output is shown.
Standard Error (Stderr): The stream containing errors is designated as stderr (2). Error messages are sent to stderr by the bash shell. The error message is shown.
Input Redirection
The input or stdin redirection symbol is the "<" sign. A command's input from a file can be redirected like a command's output can be sent to a file. The less-than character is used to redirect a command's input, similar to how the greater-than character > is used for output redirection in I/O Redirection.
Example 1:
Lets open a random file
The wc -l command gives the output of number of rows in a file followed by the file name.
The stdin redirection symbol ‘<’ will help us redirect input to the file.
In this scenario, the file name is not printed. This occurs because the command expects to receive input from “stdin” than a file.
Output Redirection
You may redirect Stdout by using the greater-than symbol. The shell will clear the file when it encounters the > symbol while reading the line.
The abbreviation “1>” is the > notation.
Given below are examples of output redirection with some Linux commands:
Example 1:
Here we send the data we need inside the file using the echo command.
Example 2:
The content in the file is displayed here using the cat command.
The noclobber option can prevent a file from being erased while being used with >.
The noclobber can be overruled with >|.
After setting noclobber, we can overwrite the file with the text of our choice by using the echo command.
By setting the -o noclobber, we can't overwrite an existing file.
The noclobber command can be overruled with the >| command and overwrite the existing file.
append (>>)
ls -al > listings
In the above example, the output command ls-al will be redirected to the file "listings" and won't go to your screen.
Error Redirection
Redirecting errors to a file instead of the screen is known as "error redirection." In Linux, error redirection is a widely used function. You receive a ton of errors when running your code. Error redirection refers to delivering errors to a specific file rather than the screen. An error stream is automatically shown on the screen.
You frequently encounter permission-denied problems when searching files. The usual output should be clear with error messages. Redirecting these errors serves as the answer.
$ file 2>errorsfile
The program named "file" will be executed in the above example. "2>" redirects the error output to a particular file named "errorsfile." Thus, the program output is clear of errors. You can also perform error redirection using File Descriptor 2.
Here we have used the 2> command to represent the standard error.
We can also use the 2> command to list out all the commands.
Instead of sending them to Stdout, some false commands' errors are sent to files using error redirection.
Three files are created each time a program is run at the terminal: standard input(0), standard output(1), and standard error (2). Each time software is launched, these files are automatically produced. An error stream is displayed on the screen by default. An error stream is always displayed by default on the screen.
Frequently Asked Questions
In Linux, how can I utilize output redirection?
Using the ">" (greater-than symbol) or the "|" (pipe) operator, redirection transfers the std output of one command to the std input of another command.
What kinds of I/O devices are examples?
Hard disk, keyboard, mouse, and printer are standard I/O devices.
How can I redirect the output of bash?
The append >> operator adds the output. This enables you to combine the results from many commands into a single file.
What does the I/O command do?
The I/O commands allow you to open, close, own, utilize, read from, and write to devices.
In Linux, how do I move the very following directory?
cd .. command allows the user to go to the directory one level above the current one (parent directory).
Conclusion
In this blog, we have discussed I/O Redirection with a brief description of the standard streams and types of redirection in Linux I/O redirection.
If you think this blog has helped you enhance your knowledge of the above question, and if you want to learn more, check out our articles. Visit our website to read more such blogs.
But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundles for placement preparations. Enroll in our courses and refer to the mock test and problems available; look at the Problem Sheets, interview experiences, and interview bundle for placement preparations. You can also book an interview session with us.