LINUX: The Ultimate Beginner’s Guide! (2015)
Chapter 5: The I/O Redirection
This chapter will explain the redirection mechanism for inputs, outputs, and errors. I/O (Input/Output) redirection is a mechanism originally developed for Unix systems.
Standard Inputs and Standard Outputs
Many Linux commands scan input (e.g. files or command attributes) and create output. In general, input is entered using a keyboard, and output is shown using a computer monitor. Keyboards are considered as standard input devices (also called “stdin”). Monitors and terminal windows are considered as standard output devices (also known as “stdout”).
Because Linux systems are versatile, however, you don’t have to apply these default settings. For instance, printers can serve as standard output devices for some networks.
The Operators Used for I/O Redirection
Redirecting Output Using “|” and “>”
In some cases, you need to save the output of a process as a separate file. Alternatively, you may need to run a command on the output of a completed process. This is called output redirection. This kind of redirection is accomplished using either the “|” or “>” operator. These operators transmit the output of a process to another process as input.
Here, you can concatenate files using the cat command and put them together as a standard output. When redirecting outputs into a new file, you will be creating this filename – or overwrite if it’s already there. That means you should be careful with I/O redirection. Here’s a screenshot:
Using redirection to save “nothing” into an existing file is similar to clearing out that file:
This procedure is known as truncation.
Using redirection to nonexistent files will generate new empty files. Here’s an example:
In a different situation, you might need to use an existing file as the input a process. If the process doesn’t accept files normally, you need to use the “<” operator. In the following example, input redirection is utilized to send a file to another user.
You won’t have to enter the complete address if the user (“mike,” for the current example) is an existing member of the local network. If you are going to reach someone over the internet, on the other hand, you have to supply the complete email address to send the message as an email.
How to Combine Redirections
In this section, you’ll learn how to combine the two types of redirection. First, the file (i.e. text.txt) is scanned for spelling errors. Then, the result is redirected to a log file named “error.”
The next screenshot shows all the commands that can be executed to check a different file using the less command.
Keep in mind that Linux systems are extremely case-sensitive. That means you should use the –i option when conducting case-insensitive network searches.
If you think you’ll need a piece of information in the future, you may save the output into a file:
Make sure that you are not using the names of active files. Using those names for redirection will overwrite the content of the existing files.
Currently, I/O is divided into three types . Each type has its own identifier, which is known as “file descriptor.” These are:
· Standard Inputs = 0
· Standard Outputs = 1
· Standard Errors = 2
For the descriptions below, if “<” is the initial character of the redirection operator, and the file descriptor is missing, the redirection process refers to standard inputs. If the initial character is “>,” on the other hand, the process refers to standard outputs. The practical examples given below will help you to understand this concept.
Sample command: - This will redirect both outputs and errors to the “dirlist” file.
Sample command: - This will redirect standard outputs to the “dirlist” file.
These sample commands can be exceptionally useful for computer programmers.
How to Analyze Errors
If a certain process creates many errors, you may use the following command:
That command allows you to examine the error-prone process thoroughly. Additionally, you can combine it with the make command when developing new computer programs. Here’s an example:
How to Separate Standard Outputs from Standard Errors
Computer programmers often need to separate outputs from errors. That means they use certain constructs to display outputs in a terminal window, and errors in another. You can run the tty command to identify the active pseudo terminal. Here’s the command:
How to Write Files and Outputs Simultaneously
If you want to copy inputs to standard outputs and other file/s, you may use the tee command. This command allows you to write outputs and files in just one go. You have to use the –a option to append information to existing files. In addition, this command can show and save process outputs.
To activate this tool, you should use a “pipe” (see below).
When computer programs perform tasks on input and save the result to standard outputs, it is known as filter. Filters are commonly used to restructure outputs. In this section, you’ll learn about the most important filters in Linux operating systems.
The GREP Command
You can use the grep command to scan output files thoroughly and search for patterns. Lines that contain a matching pattern will be copied to a standard output file. You can reverse this behavior by adding the –v option.
As an example, let’s assume that you need to know which entries in a particular directory have been edited in February. Here’s the command you should use:
Like other commands, grep is case-sensitive. You may use –i to ignore the difference between lower and upper case. Many GNU extensions also exist. For instance, --colour is an extension that can help you to highlight keywords when working on long lines. Meanwhile, --after-context is a GNU extension that checks and prints the total number of lines past the final matching line. You may also use the –r option to run the recursive grep command. This command scans the subdirectories of all the directories you’ve accessed during the current session.
If you want to add more details regarding character matches, you may use regular expressions. Read the documentations about grep so you can start using regular expressions on your searches.
How to Sort Outputs
The sort command allows you to arrange lines. By default, it organizes information in alphabetical order. Here’s a sample:
But sort can do other things. For instance, it can help you check file sizes. Using this command, you can arrange directory content based on file size. Here’s an example: