This tutorial explains I/O (Input/Output) redirection with examples. Learn what the STDIN, STDOUT and STDERR are and how they work in Linux along with common file redirectors such
as >, >>, 2>&1, <, /dev/null, /dev/tty1 and /dev/sda in detail.
This tutorial is the last part of the article \”Linux file system and shell explained with file redirectors\”. It explains following RHCSA/RHCE objective.
Use input-output redirection (>, >>, |, 2>, etc.)
Other parts of this tutorial are following.
This tutorial is first part of the article. It explains Linux file system structure (Linux directory structure) in detail along with naming convention used in kernel name.
This tutorial is the second part of the article. It explains shell and its function in detail along with Linux command types such as internal commands, external commands, non-privilege commands and privilege commands.
Input / Output (I/O) Redirection
STDIN, STDOUT and STDERR represent the devices from which shell takes input command and displays result and error respectively. Let’s take a simple example.
STDIN & STDOUT: – User types a command from keyboard (STDIN) at shell prompt and hit the Enter key. If command exists, shell executes that command and displays the result on monitor (STDOUT).
STDIN & STDERR: – User types a command from keyboard (STDIN) at shell prompt and hit Enter key. If command does not exist, shell displays the error on monitor (STDERR).
When we access a file, Linux creates an entry point in Kernel which is used to uniquely identify that file in running session.
This identifier is known as file descriptor. File descriptor is a non-negative integer value. First three file descriptors 0, 1 and 2 are reserved for STDIN, STDOUT and STDERR respectively.
File descriptors not only allow shell to accept input commands from any source but also allow shell to send the output or error of these commands on any destination. Unless we manually specify the device for STDIN, SDTOUT and STDERR, shell uses the default devices.
|File Descriptor||Name||Data Flow direction||Default Device|
|0||STDIN (Standard Input)||<||Keyboard|
|1||STDOUT (Standard Output)||>||Monitor|
|2||STDERR (Standard Error)||>||Monitor|
If non-standard source (STDIN) or destination (STDOUT/STDERR) is used, we have to specify that manually.
Let’s take an example. A script automatically executes, when a user is logged in, and sends its output to the log server.
In this case, since shell is receiving commands from the script instead of a standard input device, it must have to know where it should
send the output generated from those commands.
For input redirection < symbol is used while > symbol is used for output redirection. When we use input redirection symbol (<),
Linux replaces it with file descriptor and read the script and retrieves the commands as if they were typed from the keyboard.
I/O (Input/output) redirection practical examples
Access shell and run following commands
$ls $ls > test $cat test $echo \"this text will overwrite existing content\" $echo \"this text will overwrite existing content\" > test $echo \"this text will append existing content\" >> test $cat < test
Let’s understand each command in deail.
First command lists the content of current directory.
Second command also lists the content of current directory. But instead of displaying output on monitor (default standard output device), it sends that output in a file named test.
Third command displays the content of specified file on monitor.
Fourth command prints the specified string on monitor.
Fifth command also prints the specified string. But instead of printing it on standard output device (monitor),
it prints that in a file named test. Since the file test already contains data, its content will be overwritten.
We can verify this by running third command again.
Sixth command also prints the specified string in a file named test. But instead of overwriting, this command appends the file.
Seventh command takes its input from file instead of standard input device (keyboard).
Following figure illustrates above practice with output.
Common I/O file redirectors
|>||Store output in specified file. If file exist, content will be overwritten. If file does not exist, a new file will be created with specified name and output will be stored.|
|>>||Store output in specified file. If file exist, content will be appended. If file does not exist, a new file will be created with specified name and output will be stored.|
|2>&1||Send error messages and command output on same destination.|
|<||Read command from file instead of keyboard.|
|/dev/null||Send output to null. (Discard the output.)|
|/dev/tty1||Send output to terminal number one. (Require root permission)|
|/dev/sda||Send output to first hard disk (sda). (Require root permission)|
Pipes in Linux
Pipes make I/O redirection more flexible. It allows us to redirect the output of one command into other command as input.
Shell allows us to combine multiple commands using pipes. To combine commands use pipe (|) sign between them.
Let’s take an example. The cat command prints the contents of specified file on monitor. The wc command calculates the number of line,
word and characters from the specified file and prints the result on monitor. Both commands need source file to work.
Access shell and run following command.
$cat test | wc
This command redirects the output of first command (cat) in second command (wc) as input.
When using pipes, only the output of last command will be displayed on standard output device (monitor).
That’s all for this tutorial. In next tutorial we will learn another RHCSA/RHCE tutorial in detail with examples. If you like this tutorial, please don’t forget to share it with friends.
Full Version EX300 Dumps
Try EX300 Dumps Demo