In a previous post I showed that the input-output backbone (if I can say that) of Linux is well entrenched in the file system (FS). Now I would try to show how the standard input and output – by extension error – processing can be modified in Linux. That is the tweaking of the standard way Linux behave when it comes to input and output – through redirection.
Redirection changes the assignment of the standard input. You can use redirection to input command taken from file other than the keyboard (in Linux, literarily, everything is a file). Similarly the output of a command or error can be appended or written to a file instead of a monitor (which is anyway another file). The shell recognizes several special characters for this processes, such as “>”, “<”, and “>>”. We would be talking about three forms or kinds of redirection here:
This is where we are going to change how the Linux shell receives input from the traditional method which is the keyboard “<” sign is used to feed data into a command like this;
$ cat < file1
$ cat < textFile.txt
Here, the lesser than symbol, <, implies that input is coming from the file (textFile.txt) which is located on the hard disk. The cat command will take each line of the textFile.txt file and input and display it on the screen of the monitor. You can see that instead of using the keyboard (which is the standard method of entering input) you are using a file in its place – that you are not doing the typing.
Note that the direction of the sign alone can give you a clue to the type of redirection (‘<’) for input and “>” and “>>” for output. So there would be no need for trying to memorize!!!
Here also the standard or default method of rendering output is through the monitor or screen. There are usually situations where instead of displaying the content of a file on the screen and getting your command line clumsy or filled with text, you just bypass or ‘redirect’ the output to some file and saved on the hard disk. You can then open the file (immediately or later) to read the content or better still just print the file out that you can go through as hard copy. Let see how this can be done with some commands;
$ cat commands > outputFile
$ cat ls –al > outputFile
Here, the greater than symbol “>” implies redirection of the output of the ‘ls –al’ command to the file “outputFile”.
Take note that in output redirection, the file (outputFile) to which the output is redirected is first of all created, if it does not exist, before the command output is sent to it. But in a situation where the file already exist, its contents are deleted before the output is written to it (overwriting).
There is way to avoid this “overwriting” effect, instead of overwriting the output to the destination file you can use the append operator;
$ cat ls –al >> outputFile
The results of the command is appended (added) to the content of the outputFile instead of overwriting it. If the file has not been created, it would be created in the present working directory (that is the directory you are currently in). And if you don’t know you current working directory just type this on the command line;
One aspect of output-redirection I find personally relevant in using Linux is in the reading of man (manual) pages that come with Linux. I always prefer to redirect ‘man’ command output to text (.txt) files, maybe because I like having it in hard copy (printable) – anyway that’s just me. It is something like this;
$ man cat > cat_command_manual.txt
If you want the file to be saved in a location other than the present working directory, you simply supply the path when typing the redirection command.
$ man cat > /home/alexander/manuals/cat_command_manual.txt
You can use the path to save the file anywhere you desire on your hard disk.
You should be careful when using output redirection because it is possible to overwrite existing important system files, so avoid using system file names.
Error in Linux is also a form of output (yeah a very bad output every Linux user try to avoid), because the standard error file, the monitor, is also the standard output file. So error messages too can be redirected as a form of output from the monitor (screen). This command example illustrates this;
$ cat file1 > error_message_file
Let assume that file1 does not exist in the hard disk or on the current working directory. When the user executes the command [cat file1], Linux will generate an error message since the execution is unsuccessful. This message would otherwise be displayed on the monitor (the standard error file) instead it would be written to error_message_file.
As I always advice every Linux user, practice make perfect. Just take some time and practice and see how you can apply and extend some of the techniques outlined in this post.
Latest posts by ALEXANDER WAYNE OMOROKUNWA (see all)
- What is The Linux Swap Partition? - May 3, 2018
- 6 Commands you must know to be Productive on the terminal. - April 19, 2018
- Microsoft is Making love to Linux in the Windows store. - April 12, 2018