In a linux machine, which of the following redirection operators is used to change standard input?

Redirection can be defined as changing the way from where commands read input to where commands sends output. You can redirect input and output of a command.

For redirection, meta characters are used. Redirection can be into a file (shell meta characters are angle brackets '<', '>') or a program ( shell meta characters are pipesymbol '|').


Standard Streams In I/O Redirection

The bash shell has three standard streams in I/O redirection:

  • standard input (stdin) : The stdin stream is numbered as stdin (0). The bash shell takes input from stdin. By default, keyboard is used as input.
  • standard output (stdout) : The stdout stream is numbered as stdout (1). The bash shell sends output to stdout. Output goes to display.
  • standard error (stderr) : The stderr stream is numbered as stderr (2). The bash shell sends error message to stderr. Error message goes to display.

Redirection Into A File

Each stream uses redirection commands. Single bracket '>' or double bracket '>>' can be used to redirect standard output. If the target file doesn't exist, a new file with the same name will be created.

Overwrite

Commands with a single bracket '>' overwrite existing file content.

  • > : standard output
  • < : standard input
  • 2> : standard error

Note: Writing '1>' or '>' and '0<' or '<' is same thing. But for stderr you have to write '2>'.

Syntax:

Example:

In a linux machine, which of the following redirection operators is used to change standard input?

Look at the above snapshot, command "cat > sample.txt" has created 'sample.txt' with content 'a, b, c'. Same file 'sample.txt' is created again with command "cat > sample.txt" and this time it overwrites earlier file content and only displays 'd, e, f '.


Append

Commands with a double bracket '>>' do not overwrite the existing file content.

  • >> - standard output
  • << - standard input
  • 2>> - standard error

Syntax:

Example:

In a linux machine, which of the following redirection operators is used to change standard input?

Look at the above snapshot, here again we have created two files with the same name using '>>' in command "cat >> sample.txt". But this time, content doesn't overwrite and everything is displayed.


Redirection Into A Program

Pipe redirects a stream from one program to another. When pipe is used to send standard output of one program to another program, first program's data will not be displayed on the terminal, only the second program's data will be displayed.

Although the functionality of pipe may look similar to that of '>' and '>>' but has a significance difference. Pipe redirects data from one program to another while brackets are only used in redirection of files.

Example:

In a linux machine, which of the following redirection operators is used to change standard input?

Look at the above snapshot, command "ls *.txt | cat > txtFile" has put all the '.txt' files into a newly created file 'txtFile'.


>” and “>>” both of these operators represent output redirection in Linux.

Operators are characters that offer various functionalities. And these redirection operators redirect the result or the output. 

Anyone can learn the Linux terminal, but you need to put effort to have a firm grasp on it.

So, how’s “>” different from “>>”? What’s the main difference? In this tutorial, you’ll learn redirection in a nutshell and the difference between “>” and “>>” in Linux.

What is Redirection?

Redirection is a Linux feature. Inputs and outputs of a command can be redirected to another command or a file using redirection operators.

Linux programmers use:

1) Input redirection operator to redirect the input given 

2) Output redirection operator to redirect the output.

A less-than sign (<) represents input redirection. On the other hand, a greater than sign (>) is used for the output redirection. “<” and “>” are also called angled brackets.

But what’s the need for using “>” and “>>” both to redirect the output? Let’s find the reason for using both of these as output redirects.

> and >>” both are output (STDOUT) direction operators, however, they differ in the following ways.

The “>” Operator

“>” overwrites an already existing file or a new file is created providing the mentioned file name isn’t there in the directory. This means that while making changes in a file you need to overwrite certain any existing data, use the “>” operator.

This sign is used to redirect the output to something else than stdout.

Example 1:

echo “Welcome to Linux” > my_file_1.txt

After executing the above command, you’ll find that a text file “my_file_1.txt” is created in the directory. It’ll contain the text “Welcome to Linux” in it.

To check whether the file has been created successfully or not, type:

ls

The following command helps read the file type.

 cat my_file_1.txt

Now, let’s execute the same command with a different text.

echo “Learn latest tips about Linux” > my_file_1.txt

And type the command below to read the file.

cat my_file_1.txt

You’ll see that the new text has successfully overwritten the earlier text.

Example 2:

ls > allmyfiles.txt

This is another example using the “>” operator. The above command creates the file called “allmyfiles.txt”. And fills it up with the directory listing given by the “ls” command.

Example 3:

> newzerobytefile

Here a zero byte file is created having the name “newzerobytefile”.

Alternatively, this command can overwrite a file that is already present with the same name. And it’ll make the file zero bytes in size.

The “>>” Operator

“>>” operator appends an already present file or creates a new file if that file name doesn’t exist in the directory. 

Example 1:

echo “Welcome to Linux” >> my_file_2.txt

The above command will create a file by the name “my_file_2.txt” in your current directory.

Once the command is executed, type:

ls

This will verify if the file has been created successfully.

Read the file by:

cat my_file_2.txt

Let’s alter the text, now, into:

echo “Learn latest tips about Linux” >> my_file_2.txt

Since you’re using a file that was created previously, to check all the modifications made by “>>”, run the following command:

cat my_file_2.txt

And you’ll see instead of overwriting the previously entered text, the “>>” operator has appended the text. 

Example 2:

echo "End of directory listing" >> allmyfiles.txt

The above command will add “End of directory listing” at the end of a file called “allmyfiles.txt”

Summary

Because of the operators, some commands may seem to be a bit tricky to understand for first-time Linux users. That’s why I’ve used more than one example to explain each of the operators.

So, what we learned is, the “>” is the output redirection operator used for overwriting files that already exist in the directory. While, the “>>” is an output operator as well, but, it appends the data of an existing file. Often, both of these operators are used together to modify files in Linux.

Now you know the difference of the operators. Hope you find the tutorial helpful.

What is input redirection in Linux?

Input/Output (I/O) redirection in Linux refers to the ability of the Linux operating system that allows us to change the standard input ( stdin ) and standard output ( stdout ) when executing a command on the terminal. By default, the standard input device is your keyboard and the standard output device is your screen.

Which operator is used to redirect standard output and error in Linux?

The redirection operator (command > file) only redirects standard output and hence, the standard error is still displayed on the terminal. The default standard error is the screen. The standard error can also be redirected so that error messages do not clutter up the output of the program.

What is standard input in Linux?

Once a command begins running, it has access to three files: It reads from its standard input file. By default, standard input is the keyboard. It writes to its standard output file.

What operator can be used to redirect standard input to a file while overwriting any existing file contents?

The command | tee file pattern (which includes the tee command) redirects the standard output of the command to a file and overwrites its contents. Then, it displays the redirected output in the terminal.