Overview of I/O redirection in Linux

by Daniel Pham
Published: Updated:
This entry is part 1 of 10 in the series Linux commands: Part 6 - Redirection

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.

Recommended Reading: Understand long format of “ls” command in Linux

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.

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.

$ 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.

$ 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.

$ 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.

Recommended Reading: Understand system directories on Linux systems

$ 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.

$ 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.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating
Series NavigationNext part: What is pipeline and how to use it »»

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

2
0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.