Linux Advanced - Input Output Redirection and Piping

Linux Advanced - Input Output Redirection and Piping

When you run any command on Linux system it takes some input from your terminal and give an output back to terminal. Basically it runs instructions given from the command line interface using switches to create the output back on the terminal.

The standard input (stdin) device is usually the keyboard and the standard output (stdout) device is the screen.

Why we need Input Output Redirection?

Let us understand this by taking a scenario. Let us assume we want to run cal (the calendar) command, it will print the output to the current terminal screen. But if we want to save this output to some file instead of printing out on screen we would need a way to redirect the command output to the desired file.

The same way if we run some command let's say head it needs an input to produce output. So we can either write input in the form of command directly or provide or redirect input from any other place or file.

cal.png

What is Input Output Redirection?

Whenever you run any command or program on a Linux system it has three streams opened ->

  1. One for input represented by stdin.
  2. One for output represented by stdout.
  3. One for printing error or debug messages represented by stderr.

pictorial.png

Let us understand them one by one.

Standard input (stdin) redirection

The standard input redirection (also represented by a less than < sign) stream takes data or input from a user to a program. Those programs receive input from a device for e.g. keyboard. In Linux standard inputs from files are also allowed.

For example:

Here cat command will take input from the file /etc/resolv.conf instead of from the keyboard.

stdin.png

In Linux the file descriptor 0 is associated with stdin. So the following also will produce the same output. cat 0< /etc/resolv.conf

stdinb-3.png

Standard input will be terminated once it reaches EOF (end-of-file). EOF means that there is no further data to be read.

Let us another example where we will count the number of lines in a file.

root@lco-linux-master:~# wc -l /etc/resolv.conf
8 /etc/resolv.conf

root@lco-linux-master:~# wc -l < /etc/resolv.conf
8

stdin-2.png

In the first command the wc program is aware of its input source which is coming from a file /etc/resolv.conf. In the second command, wc knows that the input is coming from standard input so it does not display file name.

Standard output (stdout) redirection

The standard output typically goes to the console. The standard output stream when not redirected, will output the text to the screen/terminal.

For e.g. The echo command displays any input argument passed to it on the command line.

root@lco-linux-master:~# echo "Learn Code Online"
Learn Code Online

To redirect stdout on Linux to a file, you have to use the “>” greater than operator.

For e.g.

root@lco-linux-master:~# ls -l

root@lco-linux-master:~# ls -l > ls_stdout.txt

root@lco-linux-master:~# cat ls_stdout.txt

stdout.png

This redirects the output not to the terminal, but rather to a new file ls_stdout.txt.

In Linux the file descriptor 1 is associated with stdout. So the following also will produce the same output. ls -l 1> ls_stdout.txt

stdout-2.png

Standard error (stderr) redirection

There are two ways a command could produce it's output. It could either a valid output or it could be an error message.

But both of them will be by default gets delivered to the same place as standard output, your terminal screen.

For example, if you run a wrong command or with bad parameters, you will see an error message on your screen and it has been processed via the file descriptor responsible for error messages (fd[2]).

You can redirect these error messages either to a text file or to a special file called /dev/null. /dev/null is like Recycle Bin in Windows operating system.

root@lco-linux-master:~# ls -l abc.txt
ls: cannot access 'abc.txt': No such file or directory

root@lco-linux-master:~# ls -l abc.txt 2> error.txt

root@lco-linux-master:~# cat error.txt
ls: cannot access 'abc.txt': No such file or directory

root@lco-linux-master:~# ls -l abc.txt 2> /dev/null

stderr.png

In Linux the file descriptor 2 is associated with stderr.

Type of Redirection Operations

We have learn above the redirection commands for each stream. When we do redirection to a file either the file will be overwritten or the content will get appended to it.

When you use single '>' sign the output file will always get overwritten with new content and if you use double '>>' signs it will append the content.

For e.g.

root@lco-linux-master:~# echo "Redirection Tutorial" > redirection.txt

root@lco-linux-master:~# cat redirection.txt
Redirection Tutorial

root@lco-linux-master:~# echo "By LearnCodeOnline" > redirection.txt

root@lco-linux-master:~# cat redirection.txt
By LearnCodeOnline

root@lco-linux-master:~# cat > redirection.txt

root@lco-linux-master:~# echo "Redirection Tutorial" > redirection.txt

root@lco-linux-master:~# cat redirection.txt
Redirection Tutorial

root@lco-linux-master:~# echo "By LearnCodeOnline" >> redirection.txt

root@lco-linux-master:~# cat redirection.txt
Redirection Tutorial
By LearnCodeOnline

In this example we are putting some content in a file named redirection.txt. When we use > it overwrites the content of the file (it can be destructive at times!) and when we use >> it appends the content to it.

overwrite_append.png

If the target file doesn't exist on the system, a new file with the same name will be created.

Using stdin and stdout simultaneously

At times we want to copy content of one file to another file in that case instead of using the cp command we can do it by using cat command along with stdin and stdout streams.

For e.g. We have a file names redirection.txt. We will copy content of this file to a new file named new_redirection.txt.

root@lco-linux-master:~# cat redirection.txt
Redirection Tutorial
By LearnCodeOnline

root@lco-linux-master:~# cat < redirection.txt > new_redirection.txt

root@lco-linux-master:~# cat redirection.txt
Redirection Tutorial
By LearnCodeOnline

root@lco-linux-master:~# cat new_redirection.txt
Redirection Tutorial
By LearnCodeOnline

redirection_together.png

Using the ampersand (&) with stdout and stderr

We use these two together when you want to store both error and standard output into a file, to processed later. Use the 2<&1 syntax with a preceding >.

For e.g.

Here we are listing /etc/fstab file and documents file/directory and storing both error and standard output into a file. We can also understand it as the error message generated by “2” gets merged with the current output “1“.

root@lco-linux-master:~# ls -l /etc/fstab documents >list 2>&1

root@lco-linux-master:~# cat list
ls: cannot access 'documents': No such file or directory
-rw-r--r-- 1 root root 643 May  7 09:50 /etc/fstab

You can use the &> as well as an alternate to redirect both the output and the errors.

root@lco-linux-master:~# ls -l /etc/fstab documents >& list

root@lco-linux-master:~# cat list
ls: cannot access 'documents': No such file or directory
-rw-r--r-- 1 root root 643 May  7 09:50 /etc/fstab

stdout_stderr_together.png

Pipes in Linux

The pipe command denoted by the symbol | allows you to send output of one command to another for further processing. It can redirect the standard output, input, or error of one process to another.

Syntax ->

Command 1 | Command 2 | …| Command-N

Let us understand this with an example. There is a file named /etc/fstab and we want to count the number of lines in that file. To display the content of a file we have cat command and to count word or line we have wc command. Now we have to use these two commands together in such a way so that we get the desired output.

Piping concept comes handy in such scenarios.

root@lco-linux-master:~# cat /etc/fstab | wc -l
13

wc.png

Let me take another example. I want to list out all the txt files in current working directory and save the output to a new file.

root@lco-linux-master:~# ls *.txt | cat > text_files_list

pipe2.png

That's all for this detailed guide which covers different aspects of handling file descriptor streams and redirection on Linux system .

I will summarize this topic with a tabular representation.

nutshell.png

Hope you like the article. Stay Tuned for more.

Thank you. Happy learning!

Did you find this article valuable?

Support Learn Code Online by becoming a sponsor. Any amount is appreciated!