Overview of I/O redirection in Linux

Share this post to your friends !

This entry is part 1 of 10 in the series Linux commands: Part 6 - Redirection
Overview of I/O redirection in Linux
5 (100%) 1 vote[s]

WriteBash - This article will give an overview of I/O redirection in Linux. We will learn what I/O redirection is?

Most programs handle its output in two ways: program results and error status. For example, when you type ls command on the terminal, you will get the results right on the screen. In case of an error, the error message also returns on the screen.

Everything is a file and what is stdout, stderr, stdin

Unix-based operating systems and systems, remember that a sentence that is often mentioned in documents is “everything is a file”.

For example, with the ls command on the terminal, the ls command actually sends the result to a file called standard output (stdout for short). And the ls command sends error messages to the file called standard error (stderr for short).

Default stdout file and stderr will link to the terminal screen. These two files will not save as disk files.

So you can understand, when you type the ls command, this command returns the results of two stdout and stderr files, which link to the screen. You can read the results on the screen, but you are actually reading the results in these two files.

In programs that allow data entry, it will have another file called standard input (abbreviated as stdin). By default, this file is linked to the keyboard, so what you type on the keyboard, the program will read.

I/O redirection in Linux

With the concept of stdin, stdout, stderr above. We can understand that by default, we will enter data from the keyboard and data will be returned to the screen.

Redirecting I/O is an action that allows you to change that. It is possible to decide where the input data comes from and where the output data will be sent.

overview-of-i-o-redirection-in-linux Overview of I/O redirection in Linux
Overview of I/O redirection in Linux.

Example redirect standard output

For example, you will now use the ls command to list the content in the /usr/local directory. By default the result will return to the screen.

linuxmint ~ # ls -l /usr/local
total 32
drwxr-xr-x 3 root root 4096 Oct 23 16:23 bin
drwxr-xr-x 2 root root 4096 Jun 27 2015 etc
drwxr-xr-x 2 root root 4096 Jun 27 2015 games
drwxr-xr-x 3 root root 4096 Aug 23 2016 include
drwxr-xr-x 9 root root 4096 Apr 17 2018 lib
lrwxrwxrwx 1 root root 9 Jun 3 2016 man -> share/man
drwxr-xr-x 2 root root 4096 Jun 27 2015 sbin
drwxr-xr-x 10 root root 4096 Sep 6 10:09 share
drwxr-xr-x 2 root root 4096 Jun 27 2015 src

Next, use the right arrow to redirect the output to a file named ls_stdout.txt.

linuxmint ~ # ls -l /usr/local > ls_stdout.txt

You can then use the cat command or the less command or the text editors to view the content of the file ls_stdout.txt which is the same when not redirected.

linuxmint ~ # cat ls_stdout.txt

Example redirect standard error

Now we will try to create an error message on the screen using ls on a non-existent folder.

linuxmint ~ # ls -l /bin/usr
ls: cannot access '/bin/usr': No such file or directory

To redirect the error message, we add the number 2 just before the redirection operator.

linuxmint ~ # ls -l /bin/usr 2> ls_stdout.txt

Now review the file ls_stdout.txt to see what the result is.

What is /dev/null

On Unix systems there is a file called /dev/null. This file is a system device called bit bucket. It allows entering data but does nothing.

What is the purpose of /dev/null?Sometimes, you don’t want to see the error messages output to the screen. So you will redirect the message to file /dev/null and the message will disappear.

Redirect standard input

About redirection standard input, I will have the next article to talk about commands directly related to it.

Conclution

With this article, hope you have a quick look at I/O redirection in Linux. Ending with this series, you will use a powerful pipeline in Linux.

Continue reading the seriesNext part: What is pipeline and how to use it »»
If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.