One thing that comes up for me often when working something out with one or more Linux commands is to have a way to write the standard output of what happens to a file rather than the console window. I guess if I wanted to I could just copy and paste the output to a text editor, but there must be a more professional way to do it in the command line right?
So in this post I will be writing a thing or two about redirection in Linux. and how it can be used with, or as a replacement to a Linux pipeline of two or more commands to and editor.
There are maybe a few basic things to cover first before getting into redirection. That is before getting into how to go about redirecting standard output of a command to a file it is first important to know a thing or two about what standard output is to begin with. There are also some related topics that I should at least touch base on before continuing on to redirection such as knowing what piping is, and how it can be used to pipe two or more commands together.
So in this section I will be going over some quick basics of what to know before getting into redirection.
One way to create standard output is to use the Linux Echo command, this command will just echo a string value given as an argument as standard output.
There are all kinds of commands that will produce standard output, it is the standard stream where the result of a command will be spit out to. With this simple hello world example of the Linux echo command the standard output of the command is ending up in the console, however that does not need to be the final place that it goes to. The standard output of this command could be piped to another command that would in turn place it somewhere other than the console depending on the nature of the command.
For example I can take the standard output of the echo command, and pipe it into the standard input of the nano text editor.
This will result in the string value that I have cerated with the Linux Echo command to be the opening text in the nano text editor. In the editor I can make any changes that I want to the text, and then save the file at any location within the editor. So then this is one way to go about creating a file with some output from a Linux command, however there is another way that one might like better in most situations and that is of course redirection which I will be getting to of course. First however I think that I should also touch base on standard input, and standard error a bit more before getting into some redirection examples.
When done press ctrl+d, and then the output should be something like this.
The standard input does not always have to be from the keyboard though, it can also be from another command, or it can be feed in from a file or some other kind of stream. Linux redirection can also be used as a way to redirect standard input to come into a command from a file rather than the keyboard or a command.
So for this section I will be starting out with redirection and the Linux echo command to just start out with the very basic of what redirection is all about. If you are not familiar with the echo command it is just a way to spit out text that I give via the command line as the output to the standard output of the console. This nature of the Linux echo command makes it a good command for working out very basic stuff with commands before moving on to something that actually solves a problem that I am having.
So to just simple redirect the text foo for example to a file rather than the standard output I just need to use a greater than symbol once between the command and then a path to the file that I want to create or write over again.
So then this is the basic idea of what redirection is I take the standard output and have it stored in a file in my home path rather than the standard output. With something like this if the file is not there it will be created, and if it is there the text foo will replace anything else that might be there.
Sometimes I might want to append what is in a file rather than just write over the file. To append I just need to use two greater than symbols rather than just one.
I can also redirect the standard input just like I am doing with the standard output. The only thing I need to do is just use a less than symbol rather than a greater than symbol. So Say I have a file that has some text and I want to use the base32 command to create a base32 encoding of that text file. There are a number of ways to do so with the base32 command and bash features.
In any case I would start with a text file called hello.txt that will contain some text
One way to create a base32 encoding of the file would be to pass a path to the file as an argument to base32
Another option would be to open the file with a command like cat, and then pipe the standard output of the cat command which would be the text of the file to the standard input of the base32 command.
However I do not have to depend on a command like cat to open the file first when it comes to using the content of a file as a standard input source for a command like base32. Yet another option would be to use standard input redirection to redirect the content of the hello.txt file to the standard input of the base32 command.
I could then use a combination of redirection of standard input, as well as output to create a hello.b32 file using just the base32 command and redirection.
It is possible to also redirect standard error streams from commands in the same manor as standard output. Just standard error can be redirected, or both the standard out and standard error together.
To both redirect the standard error, and standard output to a file there is using an ampersand and pointy brackets syntax that can be used to do so. Using just one pointy bracket will overwrite a file if it is there to begin with, and using two will append to a file that might be there.
For example say I have this output in a folder then calling the ls command for the current folder.
Then say I redirect the output of the log form of an ls command to a file called log.js. Again for the current path, but also for an invalid path name which will result in an error.
The content of sl.log then looks like this
So then this is generaly the best way to redirect all output both error and exspected to a single file.
Here is a real example that involves using Linux grep to create a list of blog post id numbers. That is I have a whole bunch of markdown files that have id numbers at the top of each file, and I want a list of file names and id numbers. I use the Linux grep command to create the list that I want, but now I need a way to save that list to a file rather than just have it in the console window. So one way to go about doing this would be to use Linux redirection.
So each markdown file has some info about the file at the top like this:
I can use grep to get a list of each file name, and the id field of each set of data at the top like this.
Which will result in the list that I want in post_id.txt.
I could then do additional checks to make sure that there are not any gaps, or more than one instance of the same id, but you get the basic idea. Linux redirection can be used in conjunction with the whole world of Linux commands including grep to create useful lists and reports.
To make it so the script will run as an independent command I use it with the nohup command, but doing so will result in logs being created for the standard output and error of the script. To prevent this I can use redirection to send all the output of the script to /dev/null.
The effect of this is that all output from my script will end up just being discarded, which is not a problem as log as the script is working fine. If I wanted to debug the script I would not start the script this way. However the basic idea is there, if I just want to discard output from a command, it can be redirected to /dev/null. As such the terminal window to which I start this will not have the output of that command in it, and when using the nohup command it will not end up in these files that will show up otherwise.
Linux redirection is yet another one of those things in Linux that I feel that I just need to take a moment to write a post on it in order to get everything solid with it, and to also have a resource that I can revise from time to time as I continue to learn more about how to go about taking things to the next level when it comes to becoming Linux competent.
Linux redirection can be used with or in some cases as a replacement for Linux pipelines where I am piping the standard output as the input of another command. In addition redirection can be used to redirect bolt the standard output as well as the standard input of commands.