How To Use I/O Redirections in Linux

command < infile reads from infile command > outfile Create/overwrite outfile command >> outfile Append to outfile

In this article you will be acquainted with the concept of I/O redirections, and the different ways to use standard output, standard input and standard error.

What is I/O Redirection in Linux ?

Input Output Redirection is one of the most important Linux shell features that gave the Linux command line it’s flexibility and the possibility to still exist after all these years.

I/O Redirection is a concept based on efficiency to collect and process large data, check and configure system files, and also appending data to an existing or a new file. The term redirection means that a stream of data gets redirected from one or more processes into a file or another process.

It also means redirecting standard input from a file instead of the keyboard.

Basics of input output redirection in Linux

Redirection operator is basically a buffer or a block of data that have two files descriptors; one is used for reading, and the other for writing. As mentioned in the preceding paragraph, the <> symbols are the redirection operators; the greater than symbol > is used to send the output of a program (process) to a file as input. On the other hand, the less than symbol < is used to send the output of file to a program (process) as input.

  • standard output using the > : The left hand operand output is sent to the right hand operand, So the right hand operand takes input from the read end of the > symbol.
  • standard output using the < : The right hand operand output is sent to the left hand operand, So the left hand operand takes input from the write end of the < symbol.
Standard output > Standard input
Standard input < Standard output

Redirection are in fact an inter-process communication mechanism provided in Linux to allow programs (processes) and files to communicate with each other, by forwarding the output of a program/file to the input of the other program/file .

While running Linux, the majority of its commands read input from the keyboard, or writes output to the screen. For better understanding we should get more technical: Every time you type any command at a prompt and hit Enter, you are feeding data on stdin ( standard input or standard in). On the contrary, every time you execute a command that print some type of results; these results appear on stdout (standard output or standard out ).

After getting a grip on these two types of data streams, we should be ok moving forward to understand how to connect the stdout of one side to the stdin of another. Or in other words , the one side feeds the other side.

Syntax of redirections:

A redirection chain is a sequence of commands and files separated by the control operators ‘<>’

The format for a redirections are:

  • command < infile Reads from infile
  • command > outfile Create/overwrite outfile
  • command >> outfile Append to outfile

Pipes / Control operators:  > or  >> ,  <  or  <<

Using output redirection

In this section we will see various examples of redirecting standard out of a process to a file.

The output redirection operator

First thing, let’s open up a Terminal session then type the following command:

echo "Why so serious?"
Output
Why so serious?

As you can see the result (standard output) of the echo command is printed to the terminal. To redirect the echo’s output is by using the greater than symbol > just as the following:

echo "Why so serious?" > stdout_file.txt
cat stdout_file.txt
Output
Why so serious?

You can notice that the standard output is redirected to the stdout_file.txt rather than the terminal. Another thing to notice is that the output redirection operator will create a new file with the specified name if it doesn’t exist, but if it does exist redirection overwrites its contents.

Obviously you can redirect any command’s standard output to a file. Check the following:

ls /usr > stdout_file.txt
cat stdout_file.txt
Output
bin
games
include
lib
lib32
lib64
libexec
libx32
local
sbin
share
src

The append redirection operator

In case we didn’t want the redirection to overwrite the content of our files, we can use a double greater than symbol >> which is called the append redirection operator. This way the command’s stdout gets added to the output file. Check the file below.

cat stdout_file.txt
Output
A man who suffers before it is necessary, suffers more than is necessary.

Now let’s use the >> operator to add new content without deleting the previous one.

echo "Fear is pain arising from the anticipation of evil." >> stdout_file.txt
Output
A man who suffers before it is necessary, suffers more than is necessary.
cat stdout_file.txt
Output
Fear is pain arising from the anticipation of evil.

Redirecting directly from the keyboard

We can also use the cat command to redirect keyboard standard output directly to a file rather than the display.

cat > outfile
Hello this is edxd
cat outfile
Output
Hello this is edxd

As you might have guessed, this method is also possible using the >> symbol.

cat >> outfile
Hi this is not edxd
cat outfile
Output
Hello this is edxd
Hi this is not edxd

Using input redirection

In this section we will check various examples of commands receiving their input from a file. You should keep in mind that the only commands that get their input from the keyboard can have their input redirected.

Let’s check how we can use standard input redirection to feed the content of a text file to a command.

grep -w necessary < stdout_file.txt
Output
A man who suffers before it is necessary, suffers more than is necessary

The grep command above receives its input from right hand operand (stdout_file.txt) then returns the desired result.

Suppose we want to display the content of a very large text file to the terminal. After executing the cat command, you will have to scroll back to the top in order to start reading such output.

However, Linux offers us the possibility to be more practical by redirecting long output into the less command line utility; doing just that, we can view the contents of our large file line by line or one page at a time. Check the command mentioned below:

less < War_and_peace.txt

Error redirection output

In addition to stdin and stdout, there is a third data stream Standard error.

Standard error is the message you see on the terminal when you encounter an error. For example, when you get permission denied errors while searching for certain files. Check this other example of stderr output.

cat stdout_file stdout_file.txt > outfile
Output
cat: stdout_file: No such file or directory

As you will notice the standard error is still pointing to the terminal. To capture its output we should use this operator 2> just as so:

cat stdout_file stdout_file.txt > outfile 2> std_error
cat std_error
Output
cat: stdout_file: No such file or directory

The number 2 before the > symbol, is the file descriptor associated with the standard error output. You can also add the file descriptor (2) to the append redirection operator just as so:

cat stdout_file stdout_file.txt > outfile 2>> std_error

Another thing to consider, is handling the error messages without redirecting them to a file nor to the terminal, in other words, we want them discarded. For such case, we will redirect the stderr to: /dev/null (null device) which is an empty file (and always empty). Check the following:

cat stdout_file stdout_file.txt > outfile 2> /dev/null

How about redirecting both stdout and stderr to the same file? For such case we should add the

& symbol to the output/append operator. Follow the given command below:

cat stdout_file stdout_file.txt &> outfile
cat outfile
Output
cat: stdout_file: No such file or directory
A man who suffers before it is necessary, suffers more than is necessary.
Fear is pain arising from the anticipation of evil.

Note: we can also put > before &, they both work similarly (e.g. command >& out file )

Conclusion

In this article we’ve seen the tricks of redirecting stdout and stderr to separate text files and also we’ve seen how we can benefit from stdin to redirect text files’ content to a command as input.

Additionally we hinted how we can chain multiple command line utilities and files using redirection to get the desired results. You should keep in mind that a command that reads stdin or writes stdout / stderr can participate in Linux redirection. I hope you had fun learning about redirection.

21 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
How to Use the Kill Command in Linux
Read More

The Kill Command in Linux

Each Linux system runs several processes. Not every process behaves in the manner we want them to. Let’s…