IntroductionIn this lab we will be focusing on redirecting or transmitting information from one source to another with the help of specific tools. The Linux command line redirects the information through specific standard channels. The standard input (stdin or channel 0) of the command is considered to be the keyboard and the standard output (stdout or channel 1) is considered the screen. There is also another channel that is meant to redirect error output (stderr or channel 2) of a command or a program’s error messages. The input and/or output can be redirected. When running a command, sometimes we want to transmit certain information to the command or redirect the output to a specific file. Each of these functionalities will be discussed in the next two sections. I/O RedirectionI/O redirection enables the user to redirect information from or to a command by using a text file. As described earlier, the standard input, output and error output can be redirected, and the information can be taken from text files. Redirecting Standard OutputTo redirect standard output to a file, instead of the screen, we need to use the In order to see the contents of the file that we just created, we can use the The example below demonstrates the functionality of the operator. In the first instance, a new file is created containing the text “Hello World!”: $ echo "Hello World!" > text $ cat text Hello World! In the second invocation, the same file is overwritten with the new text: $ echo "Hello!" > text $ cat text Hello! If we want to add new information at the end of the file, we need to use the The first example shows the addition of the text. As it can be seen, the new text was added on the following line: $ echo "Hello to you too!" >> text $ cat text Hello! Hello to you too! The second example demonstrates that a new file will be created: $ echo "Hello to you too!" >> text2 $ cat text2 Hello to you too! Redirecting Standard ErrorIn order to redirect just the error messages, a user will need to employ the As explained, the channel for redirecting the standard error is channel 2. When redirecting the standard error, the channel must be specified, contrary to the other standard output where channel 1 is set by
default. For example, the following command searches for a file or directory named $ find /usr games 2> text-error /usr /usr/share /usr/share/misc ---------Omitted output---------- /usr/lib/libmagic.so.1.0.0 /usr/lib/libdns.so.81 /usr/games $ cat text-error find: `games': No such file or directory
For example, the following command will run without errors, therefore no information will be written in the file $ sort /etc/passwd 2> text-error $ cat text-error As well as the standard output, the standard error can also be appended to a file with the $ sort /etc 2>> text-error $ cat text-error sort: read failed: /etc: Is a directory $ sort /etc/shadow 2>> text-error2 $ cat text-error2 sort: open failed: /etc/shadow: Permission denied Using this type of redirection, only the error messages will be redirected to the file, the normal output will be written on the screen or go through standard output or stdout. There is one particular file that technically is a bit bucket (a file that accepts input and doesn’t do anything with it): Redirecting Standard InputThis type of redirection is used to input data to a command, from a specified file instead of a keyboard. In this case the $ cat < text Hello! Hello to you too! Redirecting standard input is usually used with commands that don’t accept file arguments.
The $ tr -d "l" < text Heo! Heo to you too! For more information, consult the man page of Here DocumentsUnlike the output redirections, the As can be seen in the example below, the operator is used to input data into the command, while the word after doesn’t specify the
file name. The word is interpreted as the delimiter of the input and it will not be taken in consideration as content, therefore $ cat << hello > hey > ola > hello hey ola Consult the man page of the CombinationsThe first combination that we will explore combines the redirection of the standard output and standard error output to the same file. The $ find /usr admin &> newfile $ cat newfile /usr /usr/share /usr/share/misc ---------Omitted output---------- /usr/lib/libmagic.so.1.0.0 /usr/lib/libdns.so.81 /usr/games find: `admin': No such file or directory $ find /etc/calendar &>> newfile $ cat newfile /usr /usr/share /usr/share/misc ---------Omitted output---------- /usr/lib/libmagic.so.1.0.0 /usr/lib/libdns.so.81 /usr/games find: `admin': No such file or directory /etc/calendar /etc/calendar/default Let’s take a look at an example using the $ cut -f 3 -d "/" newfile $ cat newfile share share share ---------Omitted output---------- lib games find: `admin': No such file or directory calendar calendar find: `admin': No such file or directory The To find more about the Command Line PipesRedirection is mostly used to store the result of a command, to be processed by a different command. This type of
intermediate process can become very tedious and complicated if you want the data to go through multiple processes. In order to avoid this, you can link the command directly via pipes. In other words, the first command’s output automatically becomes the second command’s input. This connection is made by using the $ cat /etc/passwd | less root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh : In the example above, the It is possible to use multiple pipes at the same time. The intermediate commands that receive input then change it and produce output are called filters. Let’s take the $ ls -l | head | wc -w 10 As mentioned earlier, by default, There is another command that displays the end of a file:
The Guided Exercises
Explorational Exercises
SummaryIn this lab you learned:
Commands used in this lesson:
Removes sections from each line of a file. cat Displays or concatenates files. find Searches for files in a directory hierarchy. less Displays a file, allowing the user to scroll one line at the time. more Displays a file, a page at the time. head Displays the first 10 lines of a file. tail Displays the last 10 lines of a file. sort Sorts files. wc Counts by default the lines, words or bytes of a file. Answers to Guided Exercises
Answers to Explorational Exercises
What operator is used for input redirection?Redirection is done using either the ">" (greater-than symbol), or using the "|" (pipe) operator which sends the standard output of one command to another command as standard input.
What command redirects the standard output to a file?Append Redirect shell command
The >> shell command is used to redirect the standard output of the command on the left and append (add) it to the end of the file on the right.
Which command reads from standard input?The tee command, used with a pipe, reads standard input, then writes the output of a program to standard output and simultaneously copies it into the specified file or files.
What operator is used to redirect standard output to a file instead of the screen?It is often useful to save the output (stdout) from a program to a file. This can be done with the redirection operator > . Redirection of this sort will create the named file if it doesn't exist, or else overwrite the existing file of the same name.
|